时间:2021-07-01 10:21:17 帮助过:21人阅读
package cn.itcast.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; /** * update修改 * */ public class JdbcDemo3 { public static void main(String[] args) { Connection conn = null; Statement stmt = null; try { //1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2.获取数据库连接对象 conn = DriverManager.getConnection("jdbc:mysql:///myemployees", "root", "ROOT"); //3.定义SQL String sql ="update job_grades set highest_sal = ‘999999‘ where grade_level = ‘ma‘;"; //4.获取执行SQL的对象 stmt = conn.createStatement(); //5.执行SQL int count = stmt.executeUpdate(sql); //6.处理返回结果 System.out.println(count); if (count>0){ System.out.println("修改成功"); }else{ System.out.println("修改失败"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { //7.释放资源 if (stmt != null){ try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
package cn.itcast.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; /** * delete删除 * */ public class JdbcDemo4 { public static void main(String[] args) { //声明数据库连接对象 Connection conn = null; //声明数据库执行对象 Statement stmt = null; try { //1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2.获取数据库连接对象 conn = DriverManager.getConnection("jdbc:mysql:///myemployees", "root", "ROOT"); //3.定义SQL String sql ="delete from job_grades where grade_level = ‘ma‘;"; //4.获取执行SQL的对象 stmt = conn.createStatement(); //5.执行SQL int count = stmt.executeUpdate(sql); //6.处理返回结果 System.out.println(count); if (count>0){ System.out.println("删除成功"); }else{ System.out.println("删除失败"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { //7.释放资源 if (stmt != null){ try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
package cn.itcast.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; /* * DDL语句 * */ public class JdbcDemo5 { public static void main(String[] args) { //声明数据库连接对象 Connection conn = null; //声明数据库执行对象 Statement stmt = null; try { //1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2.获取数据库连接对象 conn = DriverManager.getConnection("jdbc:mysql:///myemployees", "root", "ROOT"); //3.定义SQL String sql ="create table stu (id int,name varchar(20));"; //4.获取执行SQL的对象 stmt = conn.createStatement(); //5.执行SQL int count = stmt.executeUpdate(sql); //6.处理返回结果 System.out.println(count); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { //7.释放资源 if (stmt != null){ try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
方法:executeQuery(String sql) 执行DQL语句(select)查询语句
修饰/返回值类型:ResultSet (结果集)
JDBC——Statement执行SQL语句的对象
标签:create 指针 注入 import pack delete date 资源 添加