2020-2-24

JDBC

接口或类 作用
DriverManager 类 1) 管理和注册数据库驱动 2) 得到数据库连接对象
Connection 接口 一个连接对象,可用于创建 Statement 和 PreparedStatement 对象
Statement 接口 一个 SQL 语句对象,用于将 SQL 语句发送给数据库服务器
PreparedStatemen 接口 一个 SQL 语句对象,是 Statement 的子接口
ResultSet 接口 用于封装数据库查询的结果集,返回给客户端 Java 程序

注:从 JDBC3 开始,目前已经普遍使用的版本。可以不用注册驱动而直接使用。Class.forName 这句话可以省略。

得到 MySQL 的数据库连接对象

public class Demo3 {
 public static void main(String[] args) throws SQLException {
 //url 连接字符串
 String url = "jdbc:mysql://localhost:3306/day24";
 //属性对象
 Properties info = new Properties();
 //把用户名和密码放在 info 对象中
 info.setProperty("user","root");
 info.setProperty("password","root");
 Connection connection = DriverManager.getConnection(url, info);
 //com.mysql.jdbc.JDBC4Connection@68de145
 System.out.println(connection);
 }
}

顺序:

  • 1)注册和加载驱动(可以省略)
  • 2) 获取连接
  • 3) Connection 获取 Statement 对象
  • 4) 使用 Statement 对象执行 SQL 语句
  • 5) 返回结果集
  • 6) 释放资源

Statement 中的方法:

Statement 接口中的方法 描述
int executeUpdate(String sql) 用于发送 DML 语句,增删改的操作,insert、update、delete 参数:SQL 语句 返回值:返回对数据库影响的行数
ResultSet executeQuery(String sql) 用于发送 DQL 语句,执行查询的操作。select 参数:SQL 语句 返回值:查询的结果集

/**
* 创建一张学生表
*/
public class Demo4DDL {
 public static void main(String[] args) {
     //1. 创建连接
     Connection conn = null;
     Statement statement = null;
     try {
         conn = DriverManager.getConnection("jdbc:mysql:///day24", "root", "root");
         //2. 通过连接对象得到语句对象
         statement = conn.createStatement();
         //3. 通过语句对象发送 SQL 语句给服务器
         //4. 执行 SQL
         statement.executeUpdate("create table student (id int PRIMARY key auto_increment, " +
         "name varchar(20) not null, gender boolean, birthday date)");
         //5. 返回影响行数(DDL 没有返回值)
         System.out.println("创建表成功");
     } catch (SQLException e) {
         e.printStackTrace();
     }
     //6. 释放资源
     finally {
     //关闭之前要先判断
         if (statement != null) {
             try {
             statement.close();
             } catch (SQLException e) {
             e.printStackTrace();
             }
         }
         if (conn != null) {
             try {
             conn.close();
             } catch (SQLException e) {
             e.printStackTrace();
             }
         }
       }
    }
}

ResultSet 接口中的方法

ResultSet 接口中的方法 描述
boolean next() 1) 游标向下移动 1 行 2) 返回 boolean 类型,如果还有下一条记录,返回 true,否则返回 false
数据类型 getXxx() 1) 通过字段名,参数是 String 类型。返回不同的类型 2) 通过列号,参数是整数,从 1 开始。返回不同的类型

/**
* 查询所有的学生信息
*/
public class Demo6DQL {
     public static void main(String[] args) throws SQLException {
     //1) 得到连接对象
     Connection connection =
    DriverManager.getConnection("jdbc:mysql://localhost:3306/day24","root","root");
     //2) 得到语句对象
     Statement statement = connection.createStatement();
     //3) 执行 SQL 语句得到结果集 ResultSet 对象
     ResultSet rs = statement.executeQuery("select * from student");
     //4) 循环遍历取出每一条记录
     while(rs.next()) {
         int id = rs.getInt("id");
         String name = rs.getString("name");
         boolean gender = rs.getBoolean("gender");
         Date birthday = rs.getDate("birthday");
         //5) 输出的控制台上
         System.out.println("编号:" + id + ", 姓名:" + name + ", 性别:" + gender + ", 生日:" +birthday);
     }
     //6) 释放资源
     rs.close();
     statement.close();
     connection.close();
     }
}

数据库工具类 JdbcUtils

public class JdbcUtils {
     //可以把几个字符串定义成常量:用户名,密码,URL,驱动类
     private static final String USER = "root";
     private static final String PWD = "root";
     private static final String URL = "jdbc:mysql://localhost:3306/day24";
     private static final String DRIVER= "com.mysql.jdbc.Driver";
     /**
     * 注册驱动
     */
     static {
         try {
            Class.forName(DRIVER);
         } catch (ClassNotFoundException e) {
            e.printStackTrace();
         }
     }
     /**
     * 得到数据库的连接
     */
     public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL,USER,PWD);
     }
     /**
     * 关闭所有打开的资源
     */
     public static void close(Connection conn, Statement stmt) {
         if (stmt!=null) {
             try {
                stmt.close();
             } catch (SQLException e) {
                e.printStackTrace();
             }
         }
         if (conn!=null) {
             try {
                conn.close();
             } catch (SQLException e) {
                 e.printStackTrace();
             }
         }
     }
     /**
     * 关闭所有打开的资源
     */
     public static void close(Connection conn, Statement stmt, ResultSet rs) {
     if (rs!=null) {
         try {
            rs.close();
         } catch (SQLException e) {
             e.printStackTrace();
         }
     }
     // 重载
     close(conn, stmt);
     }
}

PreparedSatement 的好处

  • prepareStatement()会先将 SQL 语句发送给数据库预编译。PreparedStatement 会引用着预编译后的结果。 可以多次传入不同的参数给 PreparedStatement 对象并执行。减少 SQL 编译次数,Statement则是每更改一下数据就会编译一下,更改id的值(select * from student where id =9),而prepareStatement则是编译一次,下次修改id的值则不会再次编译,提高效率。
  • 安全性更高,没有 SQL 注入的隐患。
  • 提高了程序的可读性

    PreparedStatement 中设置参数的方法 描述 描述
    void setDouble(int parameterIndex, double x) 将指定参数设置为给定 Java double 值。
    void setFloat(int parameterIndex, float x) 将指定参数设置为给定 Java REAL 值。
    void setInt(int parameterIndex, int x) 将指定参数设置为给定 Java int 值。
    void setLong(int parameterIndex, long x) 将指定参数设置为给定 Java long 值。
    void setObject(int parameterIndex, Object x) 使用给定对象设置指定参数的值
    void setString(int parameterIndex, String x) 将指定参数设置为给定 Java String 值。

    private static void login(String name, String password) throws SQLException {
    Connection connection = JdbcUtils.getConnection();
    //写成登录 SQL 语句,没有单引号
    String sql = "select * from user where name=? and password=?";
    //得到语句对象
    PreparedStatement ps = connection.prepareStatement(sql);
    //设置参数
    ps.setString(1, name);
    ps.setString(2,password);
    ResultSet resultSet = ps.executeQuery();
    if (resultSet.next()) {
    System.out.println("登录成功:" + name);
    }
    else {
    System.out.println("登录失败");
    }
    //释放资源,子接口直接给父接口
    JdbcUtils.close(connection,ps,resultSet);
    }

表与类的关系

public static void main(String[] args) throws SQLException {
 //创建学生对象
 Student student = new Student();
 Connection connection = JdbcUtils.getConnection();
 PreparedStatement ps = connection.prepareStatement("select * from student where id=?");
 //设置参数
 ps.setInt(1,2);
 ResultSet resultSet = ps.executeQuery();
 while (resultSet.next()) {
    //封装成一个学生对象
     student.setId(resultSet.getInt("id"));
     student.setName(resultSet.getString("name"));
     student.setGender(resultSet.getBoolean("gender"));
     student.setBirthday(resultSet.getDate("birthday"));
 }
 //释放资源
 JdbcUtils.close(connection,ps,resultSet);
 //可以数据
 System.out.println(student);
}

JDBC 事务的处理

API 介绍

Connection 接口中与事务有关的方法 说明
void setAutoCommit(boolean autoCommit) 参数是 true 或 false 如果设置为 false,表示关闭自动提交,相当于开启事务
void commit() 提交事务
void rollback() 回滚事务

public class Demo12Transaction {
 //没有异常,提交事务,出现异常回滚事务
 public static void main(String[] args) {
     //1) 注册驱动
     Connection connection = null;
     PreparedStatement ps = null;
     try {
         //2) 获取连接
         connection = JdbcUtils.getConnection();
         //3) 开启事务
         connection.setAutoCommit(false);
         //4) 获取到 PreparedStatement
         //从 jack 扣钱
         ps = connection.prepareStatement("update account set balance = balance - ? where
        name=?");
         ps.setInt(1, 500);
         ps.setString(2,"Jack");
         ps.executeUpdate();
         //出现异常
         System.out.println(100 / 0);
         //给 rose 加钱
         ps = connection.prepareStatement("update account set balance = balance + ? where
        name=?");
         ps.setInt(1, 500);
         ps.setString(2,"Rose");
         ps.executeUpdate();
         //提交事务
         connection.commit();
         System.out.println("转账成功");
     } catch (Exception e) {
        e.printStackTrace();
     try {
     //事务的回滚
         connection.rollback();
     } catch (SQLException e1) {
        e1.printStackTrace();
     }
     System.out.println("转账失败");
     }
     finally {
         //7) 关闭资源
         JdbcUtils.close(connection,ps);
     }
 }
}

猜你喜欢

转载自www.cnblogs.com/bestjdg/p/12361522.html