Hi all,
I am trying to use a prepared statement for the first time and I am getting confused with parameters. The idea is that a GUI opens with 10 fields filled in, now somebody can update one or more fields and this will be updated to an Access Database table called empDetail.
I keep receiving a count field error,
I have posted this on another forum also but I am not sure if I am allowed to post the link to it here. I am looking at various examples online but cannot find a similar example.
there are examples with setting the parameters of one or two but not with getting them from user input.
My code may have a few problems but I am trying to understand the proper way to use them.
I have been told by my instructors to just not use it but I am sure this will come up in the exam.
my code is as follows(I will be changing the minor issues when I can get the statement to work.:
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
// ResultSet rs = null;
openDatabase();
//int EmpNo = Integer.parseInt(txtEmpNo.getText());
try{
con.setAutoCommit(false);
PreparedStatement pst = con.prepareStatement("UPDATE" +
" empDetail SET [Emp ID]=?" +
" [Emp Name]=?, 'Address'=?, 'Suburb'=?,'PostCode'=?," +
"'DOB'=?, [Home Phone]=?, 'Extension'=?, 'Mobile'=?," +
" 'Email'=? WHERE [Emp ID]=?");
pst.executeUpdate();
//pst.setInt(1,Integer.parseInt(txtEmpNo.getText()));
pst.setString(1,txtEmpNo.getText());
pst.setString(2,txtName.getText());
pst.setString(3,txtAddress.getText());
pst.setString(4,txtSuburb.getText());
pst.setString(5,txtPostCode.getText());
pst.setString(6,txtDOB.getText());
pst.setString(7,txtPhone.getText());
pst.setString(8,txtWorkExt.getText());
pst.setString(9,txtMobile.getText());
pst.setString(10,txtEmail.getText());
pst.setString(11, txtEmpNo.getText());
//pst.setInt(11,Integer.parseInt(txtEmpNo.getText()));
//rs = pst.executeQuery();
// while(rs.next()){
con.commit();
con.setAutoCommit(true);
msgMessage.showMessageDialog(this,"Record updated successfully\n");
// rs.close();
pst.close();
con.close();
}
catch(SQLException e){
msgMessage.showMessageDialog(this, e.toString());
}
closeDatabase();
Any help will be appreciated. The reason why I am re-posting is because I have to finish this today so If I cannot find a solution I will just use a SQL statement which I can get to work.
Thanks all