【发布时间】: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")?