我正在开展一个学校项目,我必须与访问数据库进行交互。我正试图
SELECT Max(GameID) AS MaxID
FROM Games
但是,当我构建的Eclipse应用程序运行时,此查询仅在控制台中返回
SQL Exception: UCAExc:::4.0.3 Column not found: GameID
SQL State: S1000
Vendor Error: -421
我检查了访问数据库和列DEFINITELY EXISTS。我在访问数据库中运行了查询,它也在那里工作。我不确定我错过了什么,或者这是否可能。我怎样才能获得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
答案 0 :(得分:2)
我认为问题在于您正在检索的价值。正如您所提到的别名是MaxID,您应该从result_set获取MaxID而不是GameID
因此,它应该是
int gameID = rs.getInt("MaxID");
而不是
int gameID = rs.getInt("GameID");