当前位置:网站首页>向数据库中存入数组数据,代码出错怎么解决
向数据库中存入数组数据,代码出错怎么解决
2022-07-02 03:41:00 【CSDN问答】
这个代码的错误怎么解决?
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(); }}运行结果及报错内容
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)
边栏推荐
- NLog使用
- Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
- Blue Bridge Cup SCM digital tube skills
- Basic syntax of unity script (7) - member variables and instantiation
- Nacos 配置中心整体设计原理分析(持久化,集群,信息同步)
- The fourth provincial competition of Bluebridge cup single chip microcomputer
- 高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
- Oracle common SQL
- What do you know about stock selling skills and principles
- [designmode] Prototype Pattern
猜你喜欢

《西线无战事》我们才刚开始热爱生活,却不得不对一切开炮

The second game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup

【IBDFE】基于IBDFE的频域均衡matlab仿真

0 foundation how to learn automated testing? Follow these seven steps step by step and you will succeed

Gradle foundation | customize the plug-in and upload it to jitpack

一文彻底理解评分卡开发中——Y的确定(Vintage分析、滚动率分析等)

Which of PMP and software has the highest gold content?

Eight steps of agile development process

MD5 of Oracle

High performance and low power cortex-a53 core board | i.mx8m Mini
随机推荐
Which of PMP and software has the highest gold content?
Unity脚本的基础语法(8)-协同程序与销毁方法
0基础如何学习自动化测试?按照这7步一步一步来学习就成功了
Kotlin基础学习 15
蓝桥杯单片机省赛第十一届
"Analysis of 43 cases of MATLAB neural network": Chapter 42 parallel operation and neural network - parallel neural network operation based on cpu/gpu
Oracle viewing locked tables and unlocking
《MATLAB 神經網絡43個案例分析》:第42章 並行運算與神經網絡——基於CPU/GPU的並行神經網絡運算
ThreadLocal详解
Jetpack's livedata extension mediatorlivedata
Class design basis and advanced
Unity脚本的基础语法(6)-特定文件夹
One of the future trends of SAP ui5: embrace typescript
Kotlin 基础学习13
The 10th Blue Bridge Cup single chip microcomputer provincial competition
毕设-基于SSM电影院购票系统
一文彻底理解评分卡开发中——Y的确定(Vintage分析、滚动率分析等)
JS generate random numbers
js生成随机数
Get started with Aurora 8b/10b IP core in one day (5) -- learn from the official routine of framing interface