Earth_idle 2023-09-07 15:22 采纳率: 75%
浏览 2

JDBC传数据时出错

用JDBC向MySQL数据库中插入数据时出错,我插入的是stu2的数据,实际却变成了stu3

  studentDao studentDao = new studentDao();
        Student stu = new Student(1,"hehe");
        Student stu1 = new Student(2,"wuhu");
        Student stu2 = new Student(3,"lala");
        Student stu3 = new Student(4,"qifei");

       //studentDao.addStudent(stu);
        studentDao.addStudent(stu2);

addStudent代码

 public static  boolean addStudent(Student stu){
        boolean flag = false;
        PreparedStatement pstmt = null;
        Connection conn = JDBCUtil.getConnection();

        String sql = "insert into students values(?,?)";

        try {
            pstmt = conn.prepareStatement(sql);

            pstmt.setInt(1,stu.getId());
            pstmt.setString(2,stu.getName());

            int count = pstmt.executeUpdate();
            if(count > 0){
                System.out.println("insert succesfuly!");
                flag = true;
            }
     } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.release(conn,pstmt);
        }

        return flag;
    }

同时在这之前我尝试同时添加stu2和stu3的数据之后报错
[](


```java

//studentDao.addStudent(stu2);
        studentDao.addStudent(stu3);

JDBCutil

```java
public class JDBCUtil {

    private static String url = "jdbc:mysql://localhost:3306/joe";
    private static String username = "root";
    private static String password = "123456";
    private static Connection conn = null;

    static {

        try {
             conn = DriverManager.getConnection(url, username, password);
             if (conn != null){
                 System.out.println("Connection Succeseful!");
             }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public static Connection getConnection(){
        return conn;
    }

    public static void release(Connection conn, PreparedStatement pstmt, ResultSet res){

        if(res != null){
            try {
                res.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            res = null;
        }

        release(conn,pstmt);

    }

    public static void release(Connection conn,PreparedStatement pstmt){

        if(pstmt != null){
            try {
                pstmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            pstmt = null;
        }

      if(conn != null){
          try {
              conn.close();
          } catch (SQLException throwables) {
              throwables.printStackTrace();
          }
          conn = null;
      }
    }

}

img

  • 写回答

2条回答 默认 最新

  • AllenGd 大数据领域优质创作者 2023-09-07 16:33
    关注

    连续插入stu2和stu3的数据时,stu2的数据并没有被成功插入,而是直接插入了stu3的数据。

    解决方法:在插入数据之前先执行一条删除语句,清空表中的数据。可以在addStudent方法中的try块的开头加入如下代码:

    String deleteSql = "delete from students";
    pstmt = conn.prepareStatement(deleteSql);
    pstmt.executeUpdate();
    
    评论

报告相同问题?

问题事件

  • 修改了问题 9月7日
  • 创建了问题 9月7日

悬赏问题

  • ¥15 内网办公电脑进行向日葵
  • ¥15 如何输入双曲线的参数a然后画出双曲线?我输入处理函数加上后就没有用了,不知道怎么回事去掉后双曲线可以画出来
  • ¥50 WPF Lidgren.Network.Core2连接问题
  • ¥15 soildworks装配体的尺寸问题
  • ¥100 有偿寻云闪付SDK转URL技术
  • ¥30 基于信创PC发布的QT应用如何跨用户启动后输入中文
  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M
  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建