//1. 导入驱动jar包
//2.注册驱动
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
//3.获取数据库连接对象
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db4", "root", "root");
//4.定义sql语句
// String sql = "update account set balance = 2000 where id = 1";
//String sql = "update account set balance = 2000";
// String sql="insert into job values (10,'项目经理','管程序员写代码')";
String sql="delete from job where id=5";
//5.获取执行sql的对象 Statement
stmt = conn.createStatement();
//6.执行sql
int count = stmt.executeUpdate(sql);
//7.处理结果
if(count>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
System.out.println(count);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//8.释放资源
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
今天学的内容很多,虽然掌握了基本知识点,但还有许多不足需要努力 |
|