I was preparing a simple INSERT query with PreparedStatement over MySQL. I understand the usage and difference between execute(), int executeUpdate, and ResultSet executeQuery() methods. I have used executeUpdate() with my INSERT query which should, execution, return the number of affected rows, as follows:
public boolean registerStudent()throws SQLException{
int i = 0;
boolean flag = false;
try{
String query = "INSERT INTO userinfo (`name`,`email`,`qualification`,`password`,`centre`,`registrationTime`,`registrationNumber`) VALUES (?,?,?,?,?,now(),?)";
pstmt = cn.prepareStatement(query);
pstmt.setString(1,getStudentname());
pstmt.setString(2,getEmail());
pstmt.setString(3,getQualification());
pstmt.setString(4,getPassword());
pstmt.setString(5,getCentre());
pstmt.setInt(6, getRegistrationCount());
i = pstmt.executeUpdate();
if(i==1){
flag = true;
return flag;
}
}
catch(Exception e){
e.printStackTrace();
}
return flag;
}
However, I am getting this SQLException :
java.sql.SQLException: Can not issue executeUpdate() for SELECTs
even though I am not using any SELECT query but an INSERT!
Is there any other mistake I am making? because my program seems to be running properly otherwise.
And here is my jdbc connection code written in a separate java class :
public class DBConnection {
static Connection cn;
public static Connection getConnection()throws SQLException, IOException{
try{
Class.forName("com.mysql.jdbc.Driver");
cn = DriverManager.getConnection("jdbc:mysql://localhost/sampleapp", "root", "");
}
catch(Exception e){
e.printStackTrace();
}
return cn;
}