// 核心语句int result = statement.executeUpdate("insert into emp2(ename,hiredate,sal) value('aa','2000-12-10',30000)");if(result >0){System.out.println("执行结束");}else{System.out.println("执行失败");}
// 核心代码int result = statement.executeUpdate("update emp2 set sal='8888' where ename='aa'");// int result = statement.excuteUpdate("delete from emp2 where ename='aa'");if(result>0){System.out.println("执行成功 result:"+result);}else{System.out.println("执行失败");}
packagecom.JDBC;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;importjava.sql.Statement;/**
* @program: 编程区
* @description: 如何连接本地数据库,并实现给数据库-emp2 表,更新数据 或者 删除数据
* @author: 白嫖怪AE
* @create: 2021-12-04 15:54
**/publicclass updateDemo {publicstaticvoidmain(String[] args){Connection connection =null;Statement statement =null;try{//1. 加载驱动Class.forName("com.mysql.cj.jdbc.Driver");//2. 获取连接String userName="root";String passWord="123456";//专门用来链接 mysql8的驱动地址String url="jdbc:mysql://localhost:3306/employees?serverTimezone=UTC";
connection =DriverManager.getConnection(url,userName,passWord);//3. 定义SQL,创建状态通道(进行SQL语句的发送)
statement = connection.createStatement();int result = statement.executeUpdate("update emp2 set sal='8888' where ename='aa'");// int result = statement.excuteUpdate("delete from emp2 where ename='aa'");if(result>0){System.out.println("执行成功 result:"+result);}else{System.out.println("执行失败");}}catch(ClassNotFoundException e){
e.printStackTrace();}catch(SQLException throwables){
throwables.printStackTrace();}finally{//5. 关闭资源try{if(connection !=null){
connection.close();}if(statement !=null){
statement.close();}}catch(SQLException throwables){
throwables.printStackTrace();}}}}