【发布时间】:2013-10-30 01:46:40
【问题描述】:
我在使用 JDBC 对数据库运行的函数上遇到了一个奇怪的 SQLException。 SQLException: 未找到列“消息”。
我的函数中有这个:
st = con.prepareStatement("SELECT NotificationID,UserIDFrom,UserIDTo,Message,Timestamp,isNotified FROM notification WHERE UserIDTo=? AND isNotified=?");
st.setInt(1, _UserID);
st.setBoolean(2, false);
System.out.println("st is: " + st);
rs = st.executeQuery();
我得到了那个错误,所以我在st.executeQuery() 之后添加了这个:
ResultSetMetaData meta = rs.getMetaData();
for (int index = 1; index <= meta.getColumnCount(); index++) {
System.out.println("Column " + index + " is named " + meta.getColumnName(index));
}
当我再次运行我的代码时,结果如下:
Column 1 is named NotificationID
Column 2 is named UserIDFrom
Column 3 is named UserIDTo
Column 4 is named Message
Column 5 is named TimeStamp
Exception in thread "main" java.sql.SQLException: Column 'Message' not found.
Column 6 is named isNotified
这是我的表格设计的屏幕截图,来自 MySQL Workbench
和表中的数据
我真的不知道这是怎么回事……有人可以帮忙吗?
编辑
我已经替换了SELECT 语句中的*,只是为了对我刚刚注意到的问题添加一些内容。
如果我从选择中删除Message 列,那么TimeStamp 列会出现相同的错误。如果我删除两列,则不会出现错误。
EDIT2
好的,这是我得到错误的部分,我得到消息和时间戳:
while (rs.next()) {
NotificationID = rs.getInt("NotificationID");
System.out.println("NotificationID: " + NotificationID);
SenderID = rs.getInt("UserIDFrom");
System.out.println("SenderID: " + SenderID);
From = findUserName(SenderID);
try {
body = rs.getString("Message");
System.out.println("body: " + body);
} catch (Exception e) {
System.out.println("Message error: " + e);
e.printStackTrace();
}
try {
time = rs.getString("Timestamp");
System.out.println("time: " + time);
} catch (Exception e) {
System.out.println("Timestamp error: " + e);
e.printStackTrace();
}
}
我在每列的 getString() 方法上收到错误TimeStamp 的 StackTrace(Message 相同):
java.sql.SQLException: Column 'TimeStamp' not found.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1167)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5733)
at NotifyMe_Server.Database.getUnNotified(Database.java:444)
at tests.Tests.main(Tests.java:39)
【问题讨论】:
-
桌上有触发器吗?
-
在 SQL 语句中手动设置 Int/Boolean 是否会报错?即,尝试“SELECT * FROM notification WHERE UserIDTo=1 AND isNotified=0”并在不设置 PreparedStatement 变量的情况下执行它。
-
能否在列名中包含一些空格或奇怪的字符?
-
@abmitchell 我已经按照你说的方式尝试过了,但我仍然遇到同样的错误
-
我建议您调试 jdbc 代码到引发异常的位置,并尝试跟踪您希望成功的比较失败。问题可能出在您的代码/配置中,但调试到 jdbc 将帮助您了解哪些问题没有排好。
标签: java mysql sqlexception