2

每当我运行以下代码时,我都会收到错误“找不到列”,即使该列存在于我的表中。我正在使用access数据库,请寻求帮助

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);
            }

}

}
4

3 回答 3

2

假设错误是当你得到结果而不是当你执行查询时,你可能需要这样的东西

// ...
ResultSet rs = stmt.executeQuery("select MAX(LevelNum) as maxLevel from NList");
if (rs.next())
{
    int w = rs.getInt("maxLevel");

    // ... etc.
}
于 2013-05-05T09:03:41.523 回答
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);
            }

}
于 2013-05-05T09:04:30.620 回答
0

如果您执行查询,它将返回表中可用的最大值,如果不存在列名为 MAX(LevelNum) 而不是LevelNum的记录,则返回 null 。

在您的代码中,您将使用语句获得返回值

rs.getInt("LevelNum") 

而您应该使用MAX(LevelNum)或使用别名-

SELECT MAX(LevelNum) AS MAX_NUM FROM NList

rs.getInt("MAX_NUM")
于 2021-07-11T11:34:50.773 回答