当前位置:网站首页>向数据库中存入数组数据,代码出错怎么解决
向数据库中存入数组数据,代码出错怎么解决
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)
边栏推荐
- Set vscode. When double clicking, the selected string includes the $symbol - convenient for PHP operation
- "Analysis of 43 cases of MATLAB neural network": Chapter 41 implementation of customized neural network -- personalized modeling and Simulation of neural network
- [Li Kou brush questions] 15 Sum of three numbers (double pointer); 17. Letter combination of phone number (recursive backtracking)
- Basic operations of MySQL database (based on tables)
- VS2010 plug-in nuget
- UI (New ui:: MainWindow) troubleshooting
- [designmode] builder model
- Jetpack之LiveData扩展MediatorLiveData
- JS generate random numbers
- Imageai installation
猜你喜欢
【力扣刷题】15.三数之和(双指针);17.电话号码的字母组合(递归回溯)
初识string+简单用法(二)
Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
知物由学 | 自监督学习助力内容风控效果提升
0基础如何学习自动化测试?按照这7步一步一步来学习就成功了
Nacos 配置中心整体设计原理分析(持久化,集群,信息同步)
蓝桥杯单片机省赛第七届
高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
[HCIA continuous update] overview of dynamic routing protocol
蓝桥杯单片机省赛第十一届第二场
随机推荐
The first game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
0 foundation how to learn automated testing? Follow these seven steps step by step and you will succeed
VS2010插件NuGet
One of the future trends of SAP ui5: embrace typescript
蓝桥杯单片机省赛第八届
[personal notes] PHP common functions - custom functions
潘多拉 IOT 开发板学习(HAL 库)—— 实验2 蜂鸣器实验(学习笔记)
蓝桥杯单片机省赛第五届
【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
Aaaaaaaaaaaa
MD5 of Oracle
MySQL index, transaction and storage engine
NLog使用
Basic syntax of unity script (7) - member variables and instantiation
Kotlin basic learning 14
Oracle的md5
Blue Bridge Cup single chip microcomputer sixth temperature recorder
[mv-3d] - multi view 3D target detection network
"Analysis of 43 cases of MATLAB neural network": Chapter 41 implementation of customized neural network -- personalized modeling and Simulation of neural network
集成底座方案演示说明