我在运行以下代码时遇到错误“找不到列”,即使该列存在于我的表中。我正在使用访问数据库,请求帮助
public class Trial1 {
public static void main (String[]args){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:SENSOR";
String user = "";
String pass = "";
Connection con = DriverManager.getConnection(url,user,pass);
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); //stmt代表資料庫連接成功
ResultSet rs = stmt.executeQuery("select MAX(LevelNum) from NList");
if (rs.next()){
int w = rs.getInt("LevelNum");
int x= 3;
double i = Math.pow(2, (w-x))-1;
System.out.printf("i is %f",i);}
stmt.close();
con.close();
}catch(Exception e)
{
System.out.println("Error" + e);
}
}
}
答案 0 :(得分:1)
假设错误是在您获得结果而不是执行查询时,您可能需要这样的内容
// ...
ResultSet rs = stmt.executeQuery("select MAX(LevelNum) as maxLevel from NList");
if (rs.next())
{
int w = rs.getInt("maxLevel");
// ... etc.
}
答案 1 :(得分:0)
试试这个......
class Trial1 {
public static void main (String[]args){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:SENSOR";
String user = "";
String pass = "";
Connection con = DriverManager.getConnection(url,user,pass);
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("select MAX(LevelNum) as LEVELNUM from NList");
if (rs.next()){
int w = rs.getInt("LEVELNUM");
int x= 3;
double i = Math.pow(2, (w-x))-1;
System.out.printf("i is %f",i);}
stmt.close();
con.close();
}catch(Exception e)
{
System.out.println("Error" + e);
}
}
答案 2 :(得分:0)
如果您执行查询,它将返回表中可用的最大值,如果不存在记录,则返回空值列名 MAX(LevelNum) 而不是 LevelNum。
在您的代码中,您使用语句获取返回值
rs.getInt("LevelNum")
而您应该使用 MAX(LevelNum) 或使用别名-
SELECT MAX(LevelNum) AS MAX_NUM FROM NList
rs.getInt("MAX_NUM")