[Solved] MySQL + JAVA Exception: Before start of result set [duplicate]


In your code snippet you create PreparedStatements but you do not use them correctly. Prepared statements are meant to be used as a kind of ‘statement template’ which is bound to values before it executes. To quote the javadoc:

   PreparedStatement pstmt = con.prepareStatement(
                                 "UPDATE EMPLOYEES SET SALARY = ? WHERE ID = ?");
   pstmt.setBigDecimal(1, 153833.00)
   pstmt.setInt(2, 110592)

This has two big advantages over your current usage of PreparedStatement:

  • one PreparedStatement can be used for multiple executes
  • it prevents a possible SQL injection attack

The second one here is the biggie, if for instance your variables first and last are collected in a user interface and not reformatted, you run the risk of parts of SQL being input for those values, which then end up in your statements! Using bound parameters they will just be used as values, not part of the SQL statement.

solved MySQL + JAVA Exception: Before start of result set [duplicate]