问题描述
当我再添加两个字段date
和birthdate
时,在将数据插入MS Access数据库时遇到问题.它在insert语句中给出语法错误.建议使用PreparedStatement
,但是我不清楚我必须对代码进行哪些更改.有人可以向我解释吗?
I have a problem while inserting data in my MS Access database when I add two more fields date
and birthdate
. It gives a syntax error in insert statement. I was suggested to use a PreparedStatement
, however it is not clear to me what changes I have to made in my code. Can anyone explain it to me?
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());
}
推荐答案
当您使用如此长的陈述时,涉及到如此多的参数-不犯错误几乎是不可能的.我强烈建议使用prepareStatment.
when you use such a long statment, involving so many parametrs - it is almost imposible not doing mistakes.i highly recomend using preparedStatment.
如果您仍然坚持使用陈述,尝试将字符串打印到控制台,看看语句中是否缺少撇号或逗号.
If you still insist using statment, try print the string to the console and see if you missing apostrophe or comma in your statment.
这篇关于准备在MS Access DB中插入的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!