I have a table with column id, i want to select max value of id and +1 to that value because i'm running a loop. But the sql stament not work, but why? i have test it with mysql comand and it work well! Thank you!
Error: Exception in thread "main" java.sql.SQLException: Before start of result set at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:959) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:862) at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:790) at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2469) at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2580) at MyPackage.TestMax.main(TestMax.java:23) C:\Users\lam\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)
> The second error when try to define my_max_val: Exception in thread
Exception in thread "main" java.sql.SQLException: Before start of result set at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:959) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:862) at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:790) at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2469) at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2580) at MyPackage.TestMax.main(TestMax.java:23) C:\Users\lam\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)
public static void main(String[] args) throws Exception {
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database","user","pass");
Statement myStmt = myConn.createStatement();
ResultSet myRs = myStmt.executeQuery("SELECT MAX(ID) FROM table");
int i = myRs.getInt("MAX(ID)")+1;
System.out.println(i);
}
The second code
public static void main(String[] args) throws Exception {
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database","user","pass");
Statement myStmt = myConn.createStatement();
ResultSet myRs = myStmt.executeQuery("SELECT MAX(ID) as my_max_val FROM table");
int i = myRs.getInt("my_max_val")+1;
System.out.println(i);
}