having problem to update database
i have a database which contains schedule table with employeeno & date as the primary key with the other column names as :9 AM, 10 AM etc....
when a record exist with the date and employeeno in the table, the save buton is suppose to update the schedule table..but instead it uses the insert statement. can anyone correct me if there's soemthing wrong with my coding? or does UPDATE statement only works when we have to update all the columns.. cos mine only updates like 1 column
here's the error message
java.sql.SQLException: General error
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6987)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3111)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:338)
at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(JdbcOdbcStatement.java:288)
at Schedule.saveRecord(Schedule.java:656)
at Schedule.verifyField(Schedule.java:570)
at Schedule.access$1700(Schedule.java:25)
at Schedule$ButtonFunction.actionPerformed(Schedule.java:850)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1778)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
//i have printout the statement used:
INSERT INTO Schedule (EmployeeNo, [Date], [8 AM]) VALUES ('E000008',#21 January 2007#,'A000002');
SELECT EmployeeNo, Date FROM Schedule WHERE EmployeeNo='E000008' AND Date=#21 January 2007#
//update statement:
null
and here's the code
//method: verify fields
privatevoid verifyField(){
if(employeeNoField.getText().equals("" ) || scheduleDateField.getText().equals("" ) ||
timeComboBox.getSelectedObjects().equals("Select") || appointCustNoField.getText().equals("")){
JOptionPane.showMessageDialog(null,
"Please check if compulsory fields have been filled",
"Unable to save record",
JOptionPane.ERROR_MESSAGE);
}
else saveRecord();
}
//***************************************************************************************
//method: save record
privatevoid saveRecord(){
// Append the text from fields to String
String eNo = employeeNoField.getText();
String sdate = scheduleDateField.getText();
String time = (String)timeComboBox.getSelectedItem();
String appcust = appointCustNoField.getText();
String empno =null;
String schedate =null;
String check1=null;
String check2=null;
String check3=null;
//Check: if data exist in database using the employeeNo
try{
if ( !employeeNoField.getText().equals("" ) && !scheduleDateField.getText().equals("" )){
statement = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String query ="SELECT EmployeeNo, Date FROM Schedule " +
"WHERE EmployeeNo='" + employeeNoField.getText() +
"' AND Date=#" + scheduleDateField.getText() +"#";
check2=query;
ResultSet rs = statement.executeQuery( query );
rs.next();
//append the employeeNo to a string
empno = rs.getString("EmployeeNo" );
java.util.Date date = rs.getDate("Date");
schedate = formatter.format(date);
statement.close();
}
else empno =""; schedate="";
}
catch ( SQLException sqlex ){
sqlex.printStackTrace();
System.out.println(check2);
}
// end of check
//Update data if employeeNo exist in database
if ( employeeNoField.getText().equals( empno ) && scheduleDateField.getText().equals(schedate)){
try{
statement = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String query ="UPDATE Schedule SET [" + time +"] = '" + appcust +
"' WHERE EmployeeNo='" + empno +
"' AND Date=#" + schedate +"#";
check3=query;
int result = statement.executeUpdate( query );
if ( result == 1 ){
JOptionPane.showMessageDialog(null,"Record saved");
statement.close();
saveButton.setEnabled(false);
}
else{
JOptionPane.showMessageDialog(null,
"Please check if compulsory fields have been filled",
"Unable to save record",
JOptionPane.ERROR_MESSAGE);
}
}
catch ( SQLException sqlex ){
sqlex.printStackTrace();
JOptionPane.showMessageDialog(null,
"Failed\n" + sqlex.toString(),
"Error Message",
JOptionPane.ERROR_MESSAGE);
System.out.println(check3);
}
catch (Exception ex ){
ex.printStackTrace();
JOptionPane.showMessageDialog(null,
"Failed\n" + ex.toString(),
"Error Message",
JOptionPane.ERROR_MESSAGE);
}
}
//Insert data into table
else{
try{
statement = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String insert ="INSERT INTO Schedule (EmployeeNo, [Date], ["+time+"]) VALUES " +"("+"'"+eNo+"'"+"," +"#"+sdate+"#"+"," +"'"+appcust+"'"+")" +";";
check1=insert;
int result = statement.executeUpdate( insert );
if ( result == 1 ){
JOptionPane.showMessageDialog(null,"Record saved");
statement.close();
saveButton.setEnabled(false);/* disable save button */
}
else{
JOptionPane.showMessageDialog(null,
"Please check if compulsory fields have been filled",
"Unable to save record",
JOptionPane.ERROR_MESSAGE);
saveButton.setEnabled(true);/* enable save button */
}
}
catch ( SQLException sqlex ){
sqlex.printStackTrace();
JOptionPane.showMessageDialog(null,
"Failed\n" + sqlex.toString(),
"Error Message",
JOptionPane.ERROR_MESSAGE);
System.out.println(check1);
System.out.println(check2);
System.out.println(check3);
}
catch ( Exception ex ){
ex.printStackTrace();
JOptionPane.showMessageDialog(null,
"Failed\n" + ex.toString(),
"Error Message",
JOptionPane.ERROR_MESSAGE);
}
}
}

