我正在尝试从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());} } }
@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; }