Reputation: 1
I decided to add an "Add" button where Employee records are added to my Access Database, Employee table.
Whenever I clicked the "Add" button, an error message shows up saying "Syntax error in INSERT INTO statement". Can anybody help? Is there something wrong with my coding? Cause I really followed this video exactly, even though in the video, the person managed to successfully "Saved" the records to the database and I keep getting errors.
conn = Connect.ConnectDB();
String sql = "insert into Employee ("
+"Employee ID,"
+"Employee Name,"
+"Employee NICN,"
+"Employee Gender,"
+"Employee Contact Number,"
+"Employee Department)"
+"values("+txteid.getText()+ ",'"+txtename.getText()+"','"+txtenicn.getText()+"','"+txtegender.getText()+"','"+txtecnumber.getText()+"','"
+txtedept.getText()+"')";
try{
pst = conn.prepareStatement(sql);
pst.execute();
JOptionPane.showMessageDialog(null, "Added");
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
Upvotes: 0
Views: 104
Reputation: 69470
You use prepared statement so you must work with parameter
conn = getConnection();
String sql = "insert into Employee ("
+"Employee ID,"
+"Employee Name,"
+"Employee NICN,"
+"Employee Gender,"
+"Employee Contact Number,"
+"Employee Department)"
+"values(?,?,?,?,?,?)";
And then set the parameter:
pstmt.setInt(1, txteid.getText()); // set input parameter 1
pstmt.setString(2, txtename.getText()); // set input parameter 2
...
pstmt.executeUpdate(); // execute insert statement
Upvotes: 1