MySQL 插入查询中提到的 Java Array 元素未插入到表中。而是抛出错误
Posted
技术标签:
【中文标题】MySQL 插入查询中提到的 Java Array 元素未插入到表中。而是抛出错误【英文标题】:Java Array element mentioned in MySQL insertion query not getting inserted into table .Instead an error is being thrown 【发布时间】:2019-03-23 11:29:21 【问题描述】:我从用户那里获取值,例如:姓名、地址、电话号码、城市、目的地、日期
这些值中的每一个都存储在它们各自的数组中。在进入数组的下一次迭代之前,我想将数组元素插入到 mysql 表中 .我为此使用 JDBC。但是,我看到了 SQL 查询不正确的错误:
这是我的 SQL 查询:
String sql = "INSERT INTO `registration`(`phone`, `name`, `address`, `city`, `destination`, `date`) VALUES (100,name[i],address[i],city[i],destination[i],date[i])";
这是我正在执行 SQL 查询的相关代码部分
case 1:
System.out.println("Enter your Phone number:");
phone[i] = sc.nextInt();
sc.nextLine();
System.out.println("Enter your name:");
name[i] = sc.nextLine();
System.out.println("Enter your address:");
address[i] = sc.nextLine();
System.out.println("Enter your Pick up city:");
city[i] = sc.nextLine();
System.out.println("Enter your Destination:");
destination[i] = sc.nextLine();
System.out.println("Enter your Date:");
date[i] = sc.nextLine();
++i;
Connection conn = null;
Statement stmt = null;
try
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
//STEP 4: Execute a query
System.out.println("Inserting records into the table...");
stmt = conn.createStatement();
String sql = "INSERT INTO `registration`(`phone`, `name`, `address`, `city`, `destination`, `date`) VALUES (100,name[i],address[i],city[i],destination[i],date[i])";
stmt.executeUpdate(sql);
catch(SQLException se)
//Handle errors for JDBC
se.printStackTrace();
catch(Exception e)
//Handle errors for Class.forName
e.printStackTrace();
finally
//finally block used to close resources
try
if(stmt!=null)
conn.close();
catch(SQLException se)
// do nothing
try
if(conn!=null)
conn.close();
catch(SQLException se)
se.printStackTrace();
//end finally try
//end try
System.out.println("Goodbye!");
break;
这是显示的错误:
这是我在上面代码中的 SQL 查询
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '[i],address[i],city[i],destination[i],date[i])' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:942)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3966)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3902)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2526)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2673)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545)
at com.mysql.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1540)
at com.mysql.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2595)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1468)
at Airplane.main(Airplane.java:74)
我做错了什么?
【问题讨论】:
大错特错。 1)不要将表名和列名放在引号中。 2)查找如何在准备好的语句中添加参数。 【参考方案1】:首先,您最好使用PreparedStatement
来实现某种程度的类型安全和对SQL 注入的保护。 PreparedStatement
还允许以批处理模式插入多行,从而提高性能。
您只需在循环之外创建一次PreparedStatement
对象。
PreparedStatement stmt = conn.prepareStatement("INSERT INTO `registration`(`phone`, `name`, `address`, `city`, `destination`, `date`) VALUES (100,?,?,?,?,?)");
stmt.setString(1, name[i]);
stmt.setString(2, address[i]);
stmt.setString(3, city[i]);
stmt.setString(4, destination[i]);
stmt.setString(5, date[i]);
【讨论】:
以上是关于MySQL 插入查询中提到的 Java Array 元素未插入到表中。而是抛出错误的主要内容,如果未能解决你的问题,请参考以下文章