当前位置:网站首页>How to solve the code error when storing array data into the database
How to solve the code error when storing array data into the database
2022-07-02 03:49:00 【CSDN Q & A】
How to solve the error of this code ?
import java.sql.Connection;import java.sql.DriverManager;//import java.sql.Statement;import java.util.ArrayList;import java.util.List;import java.sql.PreparedStatement;import java.sql.PseudoColumnUsage;//import java.sql.SQLException;class sql1{ int id; String name; int age; public sql1(int id, String name, int age) { super(); this.id=id; this.name=name; this.age=age; } @Override public String toString() { return "id=" + id + ", name=" + name + ", age=" + age ; }}public class ASDZ { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub List<sql1> yu=new ArrayList<>(); yu.add(new sql1(23,"ykg",12)); yu.add(new sql1(24,"ykg1",14)); yu.add(new sql1(25,"ykg2",17));// for(sql1 i:yu) {// System.out.println(i.toString());// } Class.forName("com.mysql.jdbc.Driver"); Connection con=null; //Statement stmt=null; PreparedStatement ps=null; con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ykg?useSSL=false","root","03579631"); //stmt= con.createStatement(); String sql="insert into yu values(?,?,?)"; ps=con.prepareStatement(sql); for(int i=0;i<yu.size();i++) { ps.setInt(1, yu.get(i).id); ps.setString(2, yu.get(i).name); ps.setInt(3, yu.get(i).age); ps.executeUpdate(sql); //Update } if(ps!=null) ps.close(); if(con!=null) con.close(); }}
Operation results and error reporting contents
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?)' at line 1
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:944)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3978)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3914)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2491)
at com.mysql.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1552)
at com.mysql.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2607)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1480)
at ASDZ.main(ASDZ.java:46)
边栏推荐
- [personal notes] PHP common functions - custom functions
- How about Ping An lifetime cancer insurance?
- Three ways for programmers to learn PHP easily and put chaos out of order
- Qt的网络连接方式
- js生成随机数
- Unity脚本的基础语法(6)-特定文件夹
- Flutter中深入了解MaterialApp,常用属性解析
- 【IBDFE】基于IBDFE的频域均衡matlab仿真
- In wechat applet, the externally introduced JS is used in xwml for judgment and calculation
- Introduction to Robotics II. Forward kinematics, MDH method
猜你喜欢
[personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network
Failed to upgrade schema, error: “file does not exist
C语言:逻辑运算和判断选择结构例题
The 6th Blue Bridge Cup single chip microcomputer provincial competition
初识string+简单用法(二)
软件测试人的第一个实战项目:web端(视频教程+文档+用例库)
潘多拉 IOT 开发板学习(RT-Thread)—— 实验1 LED 闪烁实验(学习笔记)
It took me only 3 months to jump out of the comfort zone and become an automated test engineer for 5 years
整理了一份ECS夏日省钱秘籍,这次@老用户快来领走
Flutter中深入了解MaterialApp,常用属性解析
随机推荐
The first game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
First acquaintance with string+ simple usage (II)
[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
Review materials of project management PMP high frequency examination sites (8-1)
蓝桥杯单片机省赛第十一届第二场
Suggestions on settlement solution of u standard contract position explosion
Homework in Chapter 3 of slam course of dark blue vision -- derivative application of T6 common functions
《MATLAB 神经网络43个案例分析》:第42章 并行运算与神经网络——基于CPU/GPU的并行神经网络运算
u本位合约爆仓清算解决方案建议
【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
Get started with Aurora 8b/10b IP core in one day (5) -- learn from the official routine of framing interface
Lost a few hairs, and finally learned - graph traversal -dfs and BFS
蓝桥杯单片机第四届省赛
Xlwings drawing
The second game of the 12th provincial single chip microcomputer competition of the Blue Bridge Cup
Influence of air resistance on the trajectory of table tennis
[personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network
5g era is coming in an all-round way, talking about the past and present life of mobile communication
Fourier series
UI (New ui:: MainWindow) troubleshooting