Java - Insert Statement - My SQL




String rollno = "1";
String name = "ABC";
Connection con = getConnection();

String query = "insert into student (rollno, name) values (?,?)";
/*student is the table name, rollno and name are the field names*/

PreparedStatement preparedStatement = con.prepareStatement(query);

preparedStatement.setString(1,rollno);
preparedStatement.setString(2,name);

/*
*  String query = "insert into student (rollno, name) values ('1','ABC')";
*  PreparedStatement preparedStatement = con.prepareStatement(query);
*
*  NB: This is also correct. First format is used if the inputs are from user through JTextField.
*/

int noOfRowsUpdated = preparedStatement.executeUpdate();

Post a Comment

0 Comments