2

我正在尝试从 JTable 中检索数据,然后使用它来更新保存在数据库中的 sql 表。但是,java给了我这个错误,

ORA-01861: literal does not match format string

我试图包含不同的字符串并且它有效,但对于这种情况,它没有。有人可以帮忙吗?这是我的代码,

for(int i=0; i<size; i++)
            {
                Object fname= searchResultTable.getModel().getValueAt(i,0);
                Object lname= searchResultTable.getModel().getValueAt(i,1);
                Object minit= searchResultTable.getModel().getValueAt(i,2);
                Object SSN  = searchResultTable.getModel().getValueAt(i,3);
                Object birthDate=searchResultTable.getModel().getValueAt(i,4);
                Object address = searchResultTable.getModel().getValueAt(i,5);
                Object sex = searchResultTable.getModel().getValueAt(i,6);
                Object salary = searchResultTable.getModel().getValueAt(i,7);
                Object superSSN = searchResultTable.getModel().getValueAt(i,8);
                Object dno = searchResultTable.getModel().getValueAt(i,9);




               // Statement S = conn.createStatement();
               // String query ="UPDATE EMPLOYEE SET fname='"+fname+"', lname='"+lname+"', minit='"+minit+"', ssn='"+SSN+"', bdate='"+birthDate+"', address='"+address+"', sex='"+sex+"', salary='"+salary+"', superssn='"+superSSN+"', dno='"+dno+"' WHERE ssn='"+ssn.get(i)+"' ";
               // query="UPDATE EMPLOYEE set fname='Ray',lname='King' where fname='Ray' and lname='King' ";

                 String query ="UPDATE EMPLOYEE SET fname=?, lname=?, minit=?, ssn=?, bdate=?, address=?, sex=?, salary=?, superssn=?, dno=? WHERE ssn=?";

                try{
                PreparedStatement S = conn.prepareStatement(query);
                S.clearParameters();
                S.setString(1,fname.toString());
                S.setString(2, lname.toString());
                S.setString(3, minit.toString());
                S.setString(4, SSN.toString());
                S.setString(5, birthDate.toString());
                S.setString(6,address.toString());
                S.setString(7, sex.toString());
                S.setString(8, salary.toString());
                S.setString(9, superSSN.toString());
                S.setString(10,dno.toString());
                S.setString(11, ssn.get(i));

                System.out.println(fname+", "+lname+","+salary);

                int count= S.executeUpdate();
                JOptionPane.showMessageDialog(this, "Employee Table Was Updated Successfully");
                }catch(SQLException sqle){System.out.println(sqle.getMessage());}



            }
        }
4

2 回答 2

4

这是因为您正在通过字符串设置日期列,而 Java 的默认日期字符串格式与 Oracle 所期望的不兼容。

改变

S.setString(5, birthDate.toString());

S.setDate(5, birthDate);

来解决这个问题。

于 2012-05-26T12:07:41.057 回答
0

@dasblinkenlight,您的方式无法编译。

这是一种将java格式转换为sql格式的方法,它肯定有效。

 public String dateToSQLFromat(String javaString)
    {

        String day="", month="", year="";

        int dash=0;
        for(int i=0; i<javaString.length(); i++)
        {

            if(javaString.charAt(i)=='-')
            {
                dash++;
                continue;
            }

            if(dash==0)
            {
            year= year+javaString.charAt(i);

            }
            else if(dash==1)
            month= month+javaString.charAt(i);
            else
            {

                if(javaString.charAt(i)==' ')
                    break;

               day= day+javaString.charAt(i);  
            }
        }


        int myMonth= Integer.parseInt(month);

        switch(myMonth)
        {
            case 1: month="JAN"; break;
            case 2: month="FEB";break;
            case 3: month="MAR"; break;
            case 4: month="APR"; break;
            case 5: month= "MAY"; break;
            case 6: month= "JUN"; break;
            case 7: month= "JUL"; break;
            case 8: month="AUG"; break;
            case 9: month="SEP"; break;
            case 10: month="OCT"; break;
            case 11: month="NOV"; break;
            case 12: month="DEC"; break;
            default: System.out.println("Invalid month!"); break;
        }


        return day+"-"+month+"-"+year;



        }
于 2012-05-26T13:07:54.310 回答