【问题标题】:prepared statement for insert in ms access DB用于在 ms 访问数据库中插入的准备好的语句
【发布时间】:2011-04-09 04:35:05
【问题描述】:

当我添加另外两个字段 date 和 birthdate 时,在我的 MS Access 数据库中插入数据时出现问题。它在插入语句中给出了语法错误。有人建议我使用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());
}

【问题讨论】:

标签: java jdbc


【解决方案1】:

放这个

System.out.println(qry);

在,下

String qry = "...";

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

【讨论】:

    【解决方案2】:

    当您使用如此长的语句时,涉及如此多的参数 - 几乎不可能不犯错误。 我强烈推荐使用preparedStatment。

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

    【讨论】:

      猜你喜欢
      • 2016-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多