Search code examples
javaswingms-access-2007database-connection

Update query is not working in java swing


I am working on a project "Hospital Management System" in java swing.

On update page there are various text field, text area, combo box and accordingly various columns in MS Access table named patient.

There are only three columns in the database which accepts numeric value and they are roomno, contact and patid fetching their values from textField_2, textField_3 and comboBox_2.

Now the problem is whenever I press Update button after filling all information it is showing an error message like

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.

While executing ps.executeUpdate().

I have checked it 100 time but it is still showing the error message. Please help and thanks in advance.

if (ae.getActionCommand()=="UPDATE")
        {
            str1=textField_2.getText();             // Room no.
            str2=textField_3.getText();             // Contact no.
            str3=""+comboBox_2.getSelectedItem();   //ID

            s1=textField.getText();     //name
            s2=textField_7.getText();   //address   
            s4=textArea.getText();      //history
            s3=""+comboBox.getSelectedItem();       // blood group
            s5=""+comboBox_1.getSelectedItem();     //type of room          

            s7=textField_4.getText();   //  date of addmission(doa)
            s8=textField_5.getText();   //  date of birth(dob)
            s9=textArea_2.getText();        //  current problem
            s10=textField_6.getText();  //  doctor
            if (str1.equals("") || str2.equals(""))
            {
                JOptionPane.showMessageDialog(null,"PLEASE COMPLETE THE FORM.");
            }
            else if (s1.equals("") || s2.equals("") || s4.equals("") || s5.equals("") || s6.equals("") || s7.equals("") || s8.equals("") || s9.equals("") || s10.equals(""))
            {
                JOptionPane.showMessageDialog(null,"PLEASE COMPLETE THE FORM.,");
            }
            else 
            {           
                try
                {                   
                    if (!(str1.matches("[0-9]+")) && !(str2.matches("[0-9]+")))
                    {
                        JOptionPane.showMessageDialog(null,"ROOM NO. AND CONTACT MUST CONTAIN DIGITS.");
                    }
                    else if (!(str1.matches("[0-9]+")))
                    {
                        JOptionPane.showMessageDialog(null,"ROOM NO. MUST CONTAIN DIGITS.");
                    }
                    else if (!(str2.matches("[0-9]+")))
                    {
                        JOptionPane.showMessageDialog(null,"CONTACT MUST CONTAIN DIGITS.");
                    }   
                    else if (str2.length() != 10)
                    {
                        JOptionPane.showMessageDialog(null,"PLEASE ENTER VALID MOBILE NUMBER.");
                    }
                    else 
                    {
                        Connection con;
                        PreparedStatement ps;
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        con=DriverManager.getConnection("jdbc:odbc:hos_man");

                        ps=con.prepareStatement("update patient set sname='"+s1+"', address='"+s2+"', bloodgrp='"+s3+"', history='"+s4+"', roomtype='"+s5+"', roomno="+str1+", contact="+str2+", doa='"+s7+"', gender='"+s6+"', dob='"+s8+"', problem='"+s9+"', doctor='"+s10+"' where patid="+str3);

                        ps.executeUpdate();  //ERROR MESSAGE

                        con.close();
                        ps.close();
                        JOptionPane.showMessageDialog(null,"DATA MODIFIED SUCCESSFULLY.");

                        textField_2.setText("");
                        textField_3.setText("");            

                        textField.setText("");
                        textField_7.setText("");            
                        textArea.setText("");           

                        comboBox.setSelectedItem("A-ve");
                        comboBox_1.setSelectedItem("Deluxe");                   

                        rdbtnMale.setSelected(true);            

                        textField_4.setText("");
                        textField_5.setText("");
                        textArea_2.setText("");
                        textField_6.setText("");
                        comboBox_2.setSelectedIndex(0);
                    }
                }               
                catch (Exception e)
                {
                    System.out.println("Exception="+e);
                }                               
            }                       
        }

Solution

  • PreparedStatement is used for giving dynamic parameter to query.Instead of using Prepared statement, use Statement.

    If you want to use Prepared Statement then

    ps=con.prepareStatement("update patient set sname=?, address=?, bloodgrp=?, history=?, roomtype=?, roomno=?, contact=?, doa=?.................);
    ps.setString(1,s1);
    
    ...
    ..
    .
    
    ps.executeUpdate();