用于在ms访问DB中插入的预准备语句

时间:2011-04-09 04:35:06

标签: java jdbc

当我添加另外两个字段datebirthdate时,我在MS Access数据库中插入数据时遇到问题。它在insert语句中给出了语法错误。我被建议使用PreparedStatement,但我不清楚我在代码中需要做哪些更改。任何人都可以向我解释一下吗?

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

2 个答案:

答案 0 :(得分:0)

把这个

System.out.println(qry);

,在

String qry = "...";

然后将输出粘贴到MSAccess中。错误会更清楚一些。此外,我看到你将你的生日(brthd t)解析为int。它是否正确 ?

答案 1 :(得分:0)

当你使用如此长的参数时,涉及如此多的参数 - 几乎不可能不犯错误。 我非常推荐使用preparedStatment。

如果你仍坚持使用法令, 尝试将字符串打印到控制台,看看你的语句中是否缺少撇号或逗号。