i've read the mySQL guide from http://dev.mysql.com/doc/refman/5.0/en/connector-j-usagenotes-basic.html , from step 1(getGenerated) and 2(select last)
but still didn't get the value of generated primary key.
here's my portion of function using step 2, i remarked the step 1:
public void insertData(String name, String address, String city, String province, String postcode, String phonenum, String emailAddress, String username, String password, String birthDate, String birthMonth, String birthYear, String gender) {
try {
int autoIncKeyFromFunc = -1;
String query = "INSERT INTO user SET user=?,pass=?,lastlogin=now(),level=0,activated=0,user_email=?,code_activation=?";
PreparedStatement prepStmt = conn.prepareStatement(query,Statement.RETURN_GENERATED_KEYS);
prepStmt.setString(1, username);
prepStmt.setString(2, password);
prepStmt.setString(3, emailAddress);
prepStmt.setString(4, generateCode());
prepStmt.executeUpdate();
//ResultSet keys = prepStmt.getGeneratedKeys();
//keys.next();
//key = keys.getInt(1);
/*if (keys.next()) {
key = keys.getInt(1);
} else {
// throw an exception from here
}*/
rs = prepStmt.executeQuery("SELECT LAST_INSERT_ID()");
if (rs.next()) {
autoIncKeyFromFunc = rs.getInt(1);
} else {
// throw an exception from here
}
if (rs != null) {
rs.close();
}
Logger.getLogger(WebDatabase.class.getName()).log(Level.SEVERE, null, autoIncKeyFromFunc);
if (stmt != null) {
stmt.close();
}
} catch (SQLException ex) {
Logger.getLogger(WebDatabase.class.getName()).log(Level.SEVERE, null, ex);
}
}
really need help, it always returning null
Edited by: dewa5227 on Mar 30, 2009 5:41 AM
Edited by: dewa5227 on Mar 30, 2009 5:42 AM