簡體   English   中英

更新查詢在Java Swing中不起作用

[英]Update query is not working in java swing

我正在用Java swing開發項目“醫院管理系統”。

在更新頁面上,MS Access表中有多個文本字段,文本區域,組合框以及相應的各列,名為patient

數據庫中只有三列接受數字值,它們是roomnocontactpatid它們從textField_2textField_3comboBox_2獲取它們的值。

現在的問題是,當我在填寫所有信息后按“ Update按鈕時,它會顯示一條錯誤消息,例如

java.sql.SQLException:[Microsoft] [ODBC Microsoft Access驅動程序]條件表達式中的數據類型不匹配。

在執行ps.executeUpdate()

我已經檢查了100次,但它仍顯示錯誤消息。 請幫助並提前致謝。

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);
                }                               
            }                       
        }

PreparedStatement用於為查詢提供動態參數。代替使用Prepared語句,而使用Statement。

如果要使用“准備好的語句”,則

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

...
..
.

ps.executeUpdate();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM