当前位置: 移动技术网 > IT编程>数据库>MSSQL > jdbc简单的操作流程(四)删除

jdbc简单的操作流程(四)删除

2020年07月27日  | 移动技术网IT编程  | 我要评论
 public static void main(String[] args) {
        Connection conn=null;
        PreparedStatement ps=null;
        try {
            //加载驱动
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            //获取链接
            conn=DriverManager.getConnection("jdbc:sqlserver://localhost;database=studb", "sa", "123456");
            //删除
            //编写sql语句
            String sql = "delete from stu where id=?";
            //获取PreparedStatement对象
            ps=conn.prepareStatement(sql);
            //给?赋值
            ps.setInt(1, 2);
            //执行
            int i=ps.executeUpdate();
            if(i>0){
                System.out.println("成功");
            }else{
                System.out.println("失败");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally{
            //关闭链接
            try {
                ps.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

本文地址:https://blog.csdn.net/weixin_44374620/article/details/107597254

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网