如何使用JBDC修改数据

1.JDBC取得数据库Connection连接对象conn,

Connection conn=null;   //数据库连接对象

String strSql=null;    //sql语句对象

//更新学生数据操作
    public void update(Student student) {
        PreparedStatement pstm=null;    //预编译接口对象
        try {
            strSql="update student set name=?,phone=?,stuNo=?,brithday=? where id=? ";    //根据更新需求SQL语句
            pstm=conn.prepareStatement(strSql);        //连接数据库活动,提交SQL语句

    //设置参数
            pstm.setString(1, student.getName());
            pstm.setString(2, student.getPhone());
            pstm.setString(3, student.getStuNo());
            pstm.setString(4, student.getBirthday());
            pstm.setInt(5, student.getId());

    //执行SQL语句
            pstm.executeUpdate();


            System.out.println("更新数据成功。。。。。。。。。"+strSql);

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        
    }

猜你喜欢

转载自www.cnblogs.com/TangGe520/p/8939969.html