2012-08-04 70 views
0

我想通过java更新通过odbc驱动程序访问数据库。java - odbc - 插入语法错误

表名是form1的

我得到一个语法错误,当我执行以下命令:

updates="INSERT INTO form1 
(entrydate,name,gender,month,date,phno,emailid,facebookid,address,semester, 
bloodgroup,slno,college,department,liveprojects,trainings); 

values("+abc+",'"+t3.getText()+"','"+t4.getText()+"',"+def+","+def1+","+zzz+",'" 
     +t8.getText()+"','"+t9.getText()+"','"+t10.getText()+"',"+aaaa+",'" 
     +t12.getText()+"',"+xyz+",'"+t13.getText()+"','"+dd+"','sa','da')"; 

谢谢。

+3

那是什么';列名单后'在做什么? (请阅读关于sql注入和绑定参数。) – Mat 2012-08-04 10:42:04

+0

您不应该使用ODBC,而应使用真正的JDBC驱动程序。 – 2012-08-04 12:10:27

回答

0

尝试在values之前删除;

1

有两个问题同你说 - 一个真正的和潜在的一个:

  • 真实:在values关键字之前分号需要删除
  • 潜力:该语句需要转换使用带参数,否则任何字符串参数的主体中的单引号都会导致语法错误。

这里是你如何可以切换到参数化准备好的发言:

String updates="INSERT INTO form1 (entrydate,name,gender,month,date,phno,emailid,facebookid,address,semester,bloodgroup,slno,college,department,liveprojects,trainings) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; 
PreparedStatement psUpd = con.prepareStatement(updates); 
psUpd.setInt(1, abc); 
psUpd.setString(2, t3.getText()); 
psUpd.setString(3, t4.getText()); 
psUpd.setInt(4, def); 
psUpd.setInt(5, def1); // The types of parameters need to match the type of setXYZ 
... // Continue for the remaining parameters, then call 
psUpd.executeUpdate(); 
0

使用INSERT INTO form1 values(value1,value2......);直接。

2.如果你要坚持自己的格式...然后做以下更改。

-取出;以前values.

-要插入逗号,其更好地使用",",而不是','

0

分号(; )列名后是罪魁祸首。你正在通过这样做来终止声明。 只是删除了; 。你的代码将起作用。

+0

也可以使用准备报表。它不直接注入字符串中的值。 – 2012-08-04 10:56:33

1

第一个问题:

updates="INSERT INTO form1 
(entrydate,name,gender,month,date,phno,emailid,facebookid,address,semester, 
bloodgroup,slno,college,department,liveprojects,trainings); 

* 你与分离领域和价值;但它会是空白*

其次最好是使用prepareStatement

String sql ="INSERT INTO TableName 
(entrydate,name,gender,month,date,phno,emailid,facebookid,address,semester, 
bloodgroup,slno,college,department,liveprojects,trainings) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?); 

    PreparedStatement ps = con.prepareStatement(sql);  
    int index = 1; 
    ps.setInt(index++, abc); 
    ps.setString(index++, t3.getText()); 
    ps.setString(index++, t4.getText()); 
    ps.setInt(index++, def); 
    ps.setInt(index++, def1); // The types of parameters need to match the type of setXYZ 
    ... // Continue for the remaining parameters, then call 
    ps.executeUpdate(); 
+0

当PreparedStatement使用我得到的消息驱动程序不支持此功能....为什么我得到这个消息????我使用访问2003年和Odbc驱动程序 – 2012-08-05 10:06:39

+0

我用下面的代码,它的工作..感谢球员的帮助....干杯声明stmt = con.createStatement(); “插入到学生(slno,entrydate,姓名,性别,dob,phno,emailid,facebookid,地址,学期,bloodgroup,学院,部门,现场项目,培训)”+ “values(”+ serialno +“, “+ entdate +”, ' “+ nameofs +”', ' “+性别+”', “+ BOD +”, “+手机+”, ' “+ emaid +”', ' “+ femaid +”', ' “+地址+”', “+ SEM +”, ' “+ BG +”', ' “+ COLL +”', ' “+ depto +”', '的java', '的java')“; – 2012-08-05 10:17:25

0

试试这个.....

updates="INSERT INTO form1(entrydate,name,gender,month,date,phno,emailid,facebookid,address,semester,bloodgroup,slno,college,department,liveprojects,trainings) 
values('"+abc+"','"+t3.getText()+"','"+t4.getText()+"','"+def+"','"+def1+"','"+zzz+"','" +t8.getText()+"','"+t9.getText()+"','"+t10.getText()+"','"+aaaa+"','" 
    +t12.getText()+"','"+xyz+"','"+t13.getText()+"','"+dd+"','sa','da')";