【发布时间】:2017-11-25 17:03:15
【问题描述】:
我正在学习 Java JDBC,并且正在使用循环将我的数据存储到数据库 (mySQL db) 中。 当我存储个人的名字和姓氏时,它似乎工作正常,但是当我尝试插入电子邮件地址时,我收到以下错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以了解在 '@123.com)' 附近使用的正确语法
据我所知,除非我遗漏了一些非常明显的东西,否则我看不出我犯了什么语法错误?
我的代码如下:
import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
public class Driver {
public static void main(String[] args) {
try{
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:8889/demo","root","root");
Statement myStmt = myConn.createStatement();
ArrayList<String> firstNames = new ArrayList<String>(Arrays.asList("James", "John","Mark"));
ArrayList<String> lastNames = new ArrayList<String>(Arrays.asList("Handly", "licks","manford"));
ArrayList<String> emails = new ArrayList<String>(Arrays.asList("james@123.com", "John@45.co.uk","Markus@marc.com"));
for (int i = 0; i < firstNames.size(); i++){
String sql = "INSERT INTO EMPLOYEES (first_name,last_name,email) VALUES (" + firstNames.get(i)+ ", " + lastNames.get(i) + ", " + emails.get(i) + ");";
myStmt.executeUpdate(sql);
}
ResultSet myRes = myStmt.executeQuery("select * from Employees");
while(myRes.next()){
System.out.println(myRes.getString("first_name")+", "+ myRes.getString("last_name"));
}
}catch(Exception e){
e.printStackTrace();
}
}
}
当我尝试一次插入一个数据时,例如
String sql = "INSERT INTO EMPLOYEES (first_name,last_name,email) VALUES ('John','test','test@test.com')";
myStmt.executeUpdate(sql);
这工作正常,所以我对为什么没有正确传递数据感到困惑。 谢谢!
编辑: 感谢@scaisEdge 和@A.A 的反馈,虽然我正在寻找的修复确实有效,但使用字符串文字是一个BAD 的想法,因为你打开了SQL 注入。 反过来,我现在已经修改了我的代码,使用 Prepared statements (@A.A's answer),这很有效,而且问题少了很多!
新代码如下:
import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
public class Driver {
public static void main(String[] args) {
Connection myConn = null;
PreparedStatement myStmt = null;
ResultSet myRs = null;
try{
//1.get connection to db
myConn = DriverManager.getConnection("jdbc:mysql://localhost:8889/demo","root","root");
ArrayList<String> firstNames = new ArrayList<String>(Arrays.asList("James", "John","Mark"));
ArrayList<String> lastNames = new ArrayList<String>(Arrays.asList("Handly", "licks","manford"));
ArrayList<String> emails = new ArrayList<String>(Arrays.asList("james@123.com", "John@45.co.uk","Markus@marc.com"));
for (int i = 0; i < firstNames.size(); i++){
//Insert Query
myStmt = myConn.prepareStatement("INSERT INTO EMPLOYEES (first_name,last_name,email) VALUES (?,?,?)");
myStmt.setString(1,firstNames.get(i));
myStmt.setString(2,lastNames.get(i));
myStmt.setString(3,emails.get(i));
myStmt.execute();
}
ResultSet myRes = myStmt.executeQuery("select * from Employees");
while(myRes.next()){
System.out.println(myRes.getString("first_name")+", "+ myRes.getString("last_name"));
}
myConn.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
【问题讨论】:
-
您跳过字符串值的单引号
-
使用 PreparedStatement (docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html) 解决此类问题。它会自动为您添加引号并转义可能会破坏语句的内容。
-
我刚刚在寻找答案时偶然发现了该文档页面,并打算接下来尝试 :) 谢谢!