【问题标题】:How to Properly use Select Max() in Eclipse如何在 Eclipse 中正确使用 Select Max()
【发布时间】:2018-09-10 07:22:52
【问题描述】:

我正在从事一个学校项目,我必须与访问数据库进行交互。我正在尝试

SELECT Max(GameID) AS MaxID
FROM Games

但是,当通过我构建的 Eclipse 应用程序运行时,此查询仅在控制台中返回

SQL Exception: UCAExc:::4.0.3 Column not found: GameID
SQL State: S1000
Vendor Error: -421

我已经检查了 access 数据库和列 DEFINITELY EXISTS。我在访问数据库中运行了查询,它也在那里工作。我不确定我错过了什么或者这是否可能。如何获取最高值的游戏 ID?

这是与数据库的连接

ResultSet rs = null; //will hold record that get returned
Statement stmt = null; //will hold the SQL statement we want to run

try
{
    //2. Establish the connection
    Connection conn = DriverManager.getConnection("jdbc:ucanaccess://C:/Users/Public/ZaccaroBlottoDB.accdb");

    //3. Create the statement
    stmt = conn.createStatement();
    String theQuery = "SELECT Max(" 
            + "GameID)"
            + " As MaxID"
            + " FROM Games"
            + " WHERE (1=1)";

    //4. Execute the statement
    rs = stmt.executeQuery(theQuery);

    //5. Process the results
    while (rs.next())
    {
        int gameID = rs.getInt("GameID"); //note the type and the field name from the DB


        System.out.println(gameID);
        //addGameIDFTF.setText(Integer.toString(gameID +1));
    }//while

    //6. Close the Connection
    rs.close();
    conn.close();           
} 
catch (SQLException ex)
{
    System.out.println("SQL Exception: " + ex.getMessage());
    System.out.println("SQL State: " + ex.getSQLState());
    System.out.println("Vendor Error: " + ex.getErrorCode());
    ex.printStackTrace();
} //catch

【问题讨论】:

  • 哪一行给出了错误? rs = stmt.executeQuery(query)int gameID = rs.getInt("GameID")?

标签: java sql eclipse


【解决方案1】:

我认为问题在于您要检索的值。正如您提到的别名为 MaxID,您应该从 result_set 而不是 GameID 获取 MaxID

所以应该是

int gameID = rs.getInt("MaxID"); 

而不是

int gameID = rs.getInt("GameID"); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-05
    • 2012-06-15
    • 2013-02-23
    • 1970-01-01
    • 2019-09-23
    • 2019-02-14
    • 1970-01-01
    • 2013-02-18
    相关资源
    最近更新 更多