已有258人关注
Java从入门到精通(第3版)20.4.7添加修改删除数据
发表在Java答疑区 2017-03-21
是否精华
版块置顶:

老师好!

我用的是Mysql数据库,预处理添加数据的部分是不是有些语句是需要修改的?

我按照书上的代码运行,数据库能连接成功,也能查找数据,执行修改前的数据都能显示出来。

但从预处理添加数据部分就开始出现这种错误提示:

java.sql.SQLException: No value specified for parameter 4

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)

at com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2211)

at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2191)

at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2058)

at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013)

at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104)

at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998)

at Renewal.main(Renewal.java:40)

原代码是:

import java.sql.*;


public class Renewal {

static Connection con;

static PreparedStatement sql;

static ResultSet res;

static Statement stmt ;

public Connection getConnection(){

try{

Class.forName("com.mysql.jdbc.Driver");

con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test2?characterEncoding=utf8&useSSL=true", "root", "******");

System.out.println("数据库连接成功");

}catch(Exception e){

e.printStackTrace();

}

return con;

}

public static void main(String[] args) {

Renewal c = new Renewal();

con= c.getConnection();

try{

sql = con.prepareStatement("select * from tb_stu");

res = sql.executeQuery();

System.out.println("执行增加、修改、删除前的数据:");

while(res.next()){

String id = res.getString("id");

String name = res.getString("name");

String sex = res.getString("sex");

String birthday = res.getString("birthday");

System.out.print("编号:"+id);

System.out.print(" 姓名:"+name);

System.out.print(" 性别:"+sex);

System.out.print(" 生日:"+birthday);

System.out.println();

}

sql = con.prepareStatement("insert into tb_stu values(?,?,?,?)");

sql.setString(1, "彭丹");

sql.setString(2, "男");

sql.setString(3, "1990-12-12");

sql.executeUpdate();

sql = con.prepareStatement("update tb_stu set birthday=?where id = (select min(id) from tb_stu)");

sql.setString(1, "2012-12-02");

sql.executeUpdate();

stmt.executeUpdate("delete from tb_stu where id="+"(select min(id)from tb_stu)");

sql.setInt(1, 1);

sql.executeUpdate();

sql = con.prepareStatement("select*from tb_stu");

res = sql.executeQuery();

System.out.println("执行增加、修改、删除后的数据:");

while(res.next()){

String id = res.getString("id");

String name = res.getString("name");

String sex = res.getString("sex");

String birthday = res.getString("birthday");

System.out.print("编号:"+id);

System.out.print(" 姓名:"+name);

System.out.print(" 性别:"+sex);

System.out.print(" 生日:"+birthday);

System.out.println();

}

}catch(Exception e){

e.printStackTrace();

}

}


}


分享到:
精彩评论 5
根号申
学分:4736 LV12
TA的每日心情
2021-07-16 23:48:46
2017-03-22
沙发

sql = con.prepareStatement("insert into tb_stu values(?,?,?,?)");

sql.setString(1, "彭丹");

sql.setString(2, "男");

sql.setString(3, "1990-12-12");


你写了四个问号,所以报错了



incredibleje
学分:10 LV2
2017-03-22
板凳

根号申 发表于2017-03-22 11:53

sql = con.prepareStatement("insert into tb_stu values(?,?,?,?)");

sql.setString(1, "彭丹");

sql.setString(2, "男");

sql.setString(3, "1990-12-12");


你写了四个问号,所以报错了



不是,我开始是按书上三个问号,但是报了这个错误:

java.sql.SQLException: Column count doesn't match value count at row 1

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)

at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)

at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)

at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2501)

at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)

at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2079)

at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013)

at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104)

at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998)

at Renewal.main(Renewal.java:40)


根号申
学分:4736 LV12
TA的每日心情
2021-07-16 23:48:46
2017-03-24
地板

incredibleje 发表于2017-03-22 13:29

不是,我开始是按书上三个问号,但是报了这个错误:

java.sql.SQLException: Column count doesn't match value count at row 1

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)

at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)

at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)

at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2501)

at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)

at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2079)

at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013)

at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104)

at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998)

at Renewal.main(Renewal.java:40)


这个异常提示的是插入的数据与表的列数不符,你可以修改一下sql,例如


按照指定字段插入,只写入姓名、性别和生日这三个字段:insert into tb_stu(name,sex,birthday) values(?,?,?)


incredibleje
学分:10 LV2
2017-03-27
4L

还是报错啊啊啊啊

java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)

at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3327)

at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3312)

at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4027)

at Renewaltest.main(Renewaltest.java:42)


改后:

import java.sql.*;


public class Renewaltest {

static Connection con;

static PreparedStatement sql;

static ResultSet res;

static Statement stmt ;

public Connection getConnection(){

try{

Class.forName("com.mysql.jdbc.Driver");

con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test2?characterEncoding=utf8&useSSL=true", "root", "******");

System.out.println("数据库连接成功");

}catch(Exception e){

e.printStackTrace();

}

return con;

}

public static void main(String[] args) {

Renewal c = new Renewal();

con= c.getConnection();

try{

sql = con.prepareStatement("select * from tb_stu");

res = sql.executeQuery();

System.out.println("执行增加、修改、删除前的数据:");

while(res.next()){

String id = res.getString("id");

String name = res.getString("name");

String sex = res.getString("sex");

String birthday = res.getString("birthday");

System.out.print("编号:"+id);

System.out.print(" 姓名:"+name);

System.out.print(" 性别:"+sex);

System.out.print(" 生日:"+birthday);

System.out.println();

}

sql = con.prepareStatement("insert into tb_stu(name,sex,birthday) values(?,?,?)");

sql.setString(1, "彭丹");

sql.setString(2, "男");

sql.setString(3, "1990-12-12");

sql.executeUpdate();

sql = con.prepareStatement("update tb_stu set birthday=?where id =(select min(id) from tb_stu)");

sql.setString(1, "2012-12-02");

sql.executeUpdate();

stmt.executeUpdate("delete from tb_stu where id="+"(select min(id)from tb_stu)");

sql.setInt(1, 1);

sql.executeUpdate();

sql = con.prepareStatement("select*from tb_stu");

res = sql.executeQuery();

System.out.println("执行增加、修改、删除后的数据:");

while(res.next()){

String id = res.getString("id");

String name = res.getString("name");

String sex = res.getString("sex");

String birthday = res.getString("birthday");

System.out.print("编号:"+id);

System.out.print(" 姓名:"+name);

System.out.print(" 性别:"+sex);

System.out.print(" 生日:"+birthday);

System.out.println();

}

}catch(Exception e){

e.printStackTrace();

}

}


}


incredibleje
学分:10 LV2
2017-03-27
5L

根号申 发表于2017-03-24 16:59

这个异常提示的是插入的数据与表的列数不符,你可以修改一下sql,例如


按照指定字段插入,只写入姓名、性别和生日这三个字段:insert into tb_stu(name,sex,birthday) values(?,?,?)


还是报错,这是什么参数索引越界??????


java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)

at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3327)

at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3312)

at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4027)

at Renewaltest.main(Renewaltest.java:42)


修改后的代码在楼上

首页上一页 1 下一页尾页 5 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照