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