Java 如何在Eclipse中正确使用Select Max()
我正在做一个学校项目,我必须与access数据库进行交互。我正试图Java 如何在Eclipse中正确使用Select Max(),java,sql,eclipse,Java,Sql,Eclipse,我正在做一个学校项目,我必须与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 我已经检查了access数据库,该列肯定存在。我在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
我已经检查了access数据库,该列肯定存在。我在access数据库中运行了查询,它也在那里工作。我不确定我错过了什么或者这是否可能。如何获取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
我认为问题在于您正在检索的值。正如您提到的别名MaxID,您应该从result_set而不是GameID获取MaxID 因此,它应该是
int gameID = rs.getInt("MaxID");
而不是
int gameID = rs.getInt("GameID");
哪一行给出了错误
rs=stmt.executeQuery(查询)
或intgameid=rs.getInt(“gameID”)
?