当前位置:网站首页>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)
边栏推荐
- BiShe cinema ticket purchasing system based on SSM
- Object oriented thinking
- Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?
- Kotlin basic learning 17
- VS2010 plug-in nuget
- 《MATLAB 神经网络43个案例分析》:第42章 并行运算与神经网络——基于CPU/GPU的并行神经网络运算
- 【人员密度检测】基于形态学处理和GRNN网络的人员密度检测matlab仿真
- 接口调试工具模拟Post上传文件——ApiPost
- 蓝桥杯单片机省赛第九届
- 初识string+简单用法(二)
猜你喜欢

集成底座方案演示说明

In the era of programmers' introspection, five-year-old programmers are afraid to go out for interviews

"Analysis of 43 cases of MATLAB neural network": Chapter 41 implementation of customized neural network -- personalized modeling and Simulation of neural network

NLog使用

一天上手Aurora 8B/10B IP核(5)----从Framing接口的官方例程学起

《动手学深度学习》(二)-- 多层感知机

Flutter中深入了解MaterialApp,常用属性解析

Failed to upgrade schema, error: “file does not exist
![[designmode] Prototype Pattern](/img/ee/c4e48c2ce8ff66f50f0bf13e5a0418.png)
[designmode] Prototype Pattern

【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
随机推荐
Analyse de 43 cas de réseaux neuronaux MATLAB: Chapitre 42 opérations parallèles et réseaux neuronaux - - opérations parallèles de réseaux neuronaux basées sur CPU / GPU
滴滴开源DELTA:AI开发者可轻松训练自然语言模型
The 6th Blue Bridge Cup single chip microcomputer provincial competition
树莓派GPIO引脚控制红绿灯与轰鸣器
Visual slam Lecture 3 -- Lie groups and Lie Algebras
蓝桥杯单片机数码管技巧
高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
The 5th Blue Bridge Cup single chip microcomputer provincial competition
Where can I buy cancer insurance? Which product is better?
蓝桥杯单片机省赛第七届
Class design basis and advanced
Qt的网络连接方式
Vite: configure IP access
Lost a few hairs, and finally learned - graph traversal -dfs and BFS
Oracle common SQL
Object oriented thinking
《动手学深度学习》(二)-- 多层感知机
蓝桥杯单片机省赛第十二届第二场
【无线图传】基于FPGA的简易无线图像传输系统verilog开发,matlab辅助验证
整理了一份ECS夏日省钱秘籍,这次@老用户快来领走