我越来越对我对运行使用JDBC数据库的函数一个奇怪的SQLException。 的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
这里是我的表的设计截图,从我的SQL工作台
而在表中的数据
我真的想不通这是怎么回事一个在这里....任何人都可以帮帮忙?
编辑
我已经取代了*
在SELECT
声明只是添加一些,我才注意到这个问题。
如果我从选择删除Message
列然后我得到了TimeStamp
列同样的错误。如果我删除这两列,那么我就不会有错误。
EDIT2
OK,这是我得到的错误的部分,我得到两个关于信息和时间戳:
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
(同为Message
):你isNotified的
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)
你有没有触发器在桌子上? – BevynQ
如果您在SQL语句中手动设置Int/Boolean,是否会出现错误?即,尝试“选择*从通知WHERE UserIDTo = 1 AND isNotified = 0”并执行它,而不设置PreparedStatement变量。 – hotforfeature
可以在列名中包含一些空格或奇怪的字符吗? – Taylor