JDBC从Access表中选择最大值(JDBC selecting the Max value fr

2019-09-02 00:21发布

我有错误“列未找到”任何时候我运行,即使列我表中存在以下代码。 我使用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);
            }

}

}

Answer 1:

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

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

    // ... etc.
}


Answer 2:

试试这个...

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

}


文章来源: JDBC selecting the Max value from an Access table