2011-04-09 118 views
0

當我添加兩個字段datebirthdate時,我在將數據插入MS Access數據庫時遇到問題。它在插入語句中給出了語法錯誤。我被建議使用PreparedStatement,但是我不清楚我在代碼中所做的更改。任何人都可以解釋給我嗎?在ms訪問數據庫中插入準備好的語句

int regno= Integer.parseInt(cbregn.getSelectedItem().toString()); //regno=pkey 
String nm= cbnm.getSelectedItem().toString(); 
String place=tfplace.getText(); 
String kul=tfkul.getText(); 
String gotra=tfgotra.getText(); 
String kswami=tfswami.getText(); 
String raddr=taraddr.getText(); 
int pincode=Integer.parseInt(tfpcd.getText());//taken datatype number for pincode 
int stdcd=Integer.parseInt(tfstdcode.getText());//taken datatype number for stdcode 
int tele=Integer.parseInt(tftele.getText());//taken datatype number for teleph no 
int mno=(int) Long.parseLong(tfmno.getText());//taken datatype number for mobileno 
String email=tfemail.getText(); 
String website=tfweb.getText(); 
String education=tfedu.getText(); 
String branch=tfbrch.getText(); 
int brthdt=Integer.parseInt(tfbdt.getText()); 
String bloodgroup=(String)cbbldgrp.getSelectedItem(); 

try 
{ 
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
    Connection con=DriverManager.getConnection("jdbc:odbc:wanisamajDB"); 
    Statement stmt=con.createStatement(); 
    String qry= "INSERT INTO Registration1(RegistrationNo,SeniorPerson,NativePlace,Kul,Gotra,KulSwami,ResidensialAddress,PinCode,STDcode,TelephoneNo,MobileNo,Email,Website,Education,Branch,BloodGroup) VALUES('"+regno+"','"+nm+"','"+place+"','"+kul+"','"+gotra+"','"+kswami+"','"+raddr+"','"+pincode+"','"+stdcd+"','"+tele+"','"+mno+"','"+email+"','"+website+"','"+education+"','"+branch+"','"+bloodgroup+"')"; 
    // String qry= "INSERT INTO Registration1(RegistrationNo,SeniorPerson,NativePlace,Kul,Gotra,KulSwami,ResidensialAddress,PinCode,STDcode,TelephoneNo,MobileNo,Email,Website,Education,Branch,BloodGroup,Date,BirthDate) VALUES('"+regno+"','"+nm+"','"+place+"','"+kul+"','"+gotra+"','"+kswami+"','"+raddr+"','"+pincode+"','"+stdcd+"','"+tele+"','"+mno+"','"+email+"','"+website+"','"+education+"','"+branch+"','"+bloodgroup+"','"+date+"','"+brthdt+"')"; 
    stmt.executeUpdate(qry); 
    JOptionPane.showMessageDialog(null,"RECORD IS SAVED SUCCESSFULLY "); 
    con.close(); 
} 
catch(SQLException eM) { 
    System.out.println(" "+eM); 
    JOptionPane.showMessageDialog(null,"RECORD IS NOT SAVED"); 
} 
catch(Exception et) 
{ 
    System.out.println("error:"+et.getMessage()); 
} 
+1

這是最近流行的問題http://stackoverflow.com/questions/5570897/error-in-insert-into-statement – Sean 2011-04-09 05:18:57

回答

0

將這個

System.out.println(qry); 

,下

String qry = "..."; 

然後粘貼到你的MSACCESS輸出。錯誤會更清楚一點。此外,我看到你解析你的生日(brthd t)到int。它是否正確 ?

0

當你使用這麼長的陳述時,涉及到很多參數 - 這幾乎是不可能的,不會犯錯誤。 我高度推薦使用preparedStatment。

如果你仍然堅持使用陳述, 嘗試打印字符串到控制檯,看看你是否在你的陳述中缺少撇號或逗號。