gpt4 book ai didi

java - 如何在 Eclipse 中正确使用 Select Max()

转载 作者:行者123 更新时间:2023-11-30 02:12:55 25 4
gpt4 key购买 nike

我正在开展一个学校项目,我必须与 Access 数据库进行交互。我正在尝试

SELECT Max(GameID) AS MaxID
FROM Games

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

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

我已经检查了访问数据库并且该列确实存在。我在访问数据库中运行了查询,它也在那里工作。我不确定我错过了什么或者这是否可能。我怎样才能获取gameID的最高值?

这是与数据库的连接

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

最佳答案

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

因此,应该是

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

而不是

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

关于java - 如何在 Eclipse 中正确使用 Select Max(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49588189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com