Skip to content Skip to sidebar Skip to footer

Jdbc Selecting The Max Value From An Access Table

I got error 'Column not found' any time i run the following code even though the column exist in my table. Am using access database, Appealing for help please public class Trial1 {

Solution 1:

Assuming the error is when you get the result rather than when you execute the query, you probably need something like this instead

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

    // ... etc.
}

Solution 2:

Try this...

classTrial1 {
publicstaticvoidmain(String[]args){

            try{
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
                Stringurl="jdbc:odbc:SENSOR";
                Stringuser="";
                Stringpass="";
                Connectioncon= DriverManager.getConnection(url,user,pass);
                Statementstmt= con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);                          


                ResultSetrs= stmt.executeQuery("select MAX(LevelNum) as LEVELNUM from NList");
                 if (rs.next()){

                    intw= rs.getInt("LEVELNUM");
                   int x= 3;                             

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

}

Solution 3:

If you execute your query it will return max value available in table or null if no record exists with column nameMAX(LevelNum)instead of LevelNum.

And in your code you are getting returned value using statement

rs.getInt("LevelNum") 

whereas you should use MAX(LevelNum) or use aliasing like-

SELECTMAX(LevelNum) AS MAX_NUM FROM NList

rs.getInt("MAX_NUM")

Post a Comment for "Jdbc Selecting The Max Value From An Access Table"