java - 奇怪的 SQLException : Column not found

标签 java mysql sqlexception

我在使用 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 enter image description here

以及表中的数据 enter image description here

我真的不知道这里发生了什么....任何人都可以帮忙吗?

编辑
我已经替换了 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)

最佳答案

如果你观察你的代码

try {
        time = rs.getString("Timestamp");
        System.out.println("time: " + time);
    } catch (Exception e) {
        System.out.println("Timestamp error: " + e);
        e.printStackTrace();
    }
}

您已使用这种格式的“时间戳”,但如果您将其更改为数据库中指定的“时间戳”,希望它能正常工作。

关于java - 奇怪的 SQLException : Column not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19672412/

相关文章:

java - 使用 Facebook 应用程序访问 token (URL 中的管道字符)

python - 错误 : "Driver not loaded" in PyQt5

mysql - 忠诚度计划数据库设计

java.sql.SQLException : Incorrect string value: '\xF0\x9F\x91\xBD\xF0\x9F...'

oracle9i - 调用存储过程时出现异常:Bigger type length than Maximum

索引::1 处缺少 IN 或 OUT 参数的 Java SQL 异常

java - 从网络获取对象队列并遍历上一个/下一个

java - APIv4 直方图 - 无法排序

Java 写入文件 - 访问被拒绝

mysql - MySQL 的 i18n(印度语)支持有什么问题吗?