JDBC

作者: AMD_Ryzen | 来源:发表于2020-03-27 14:34 被阅读0次

1 获取连接

1.1 方式一:硬编码

  1. 基本参数
  2. 加载驱动
  3. 获取连接
public void connection1() {//mysql可以省略注册驱动
    //1. 基本参数
    String driverName = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/jdbc_test?useSSL=false&serverTimezone=UTC";
    String user = "root";
    String password = "root";
    try {
        //2. 加载驱动
        Class.forName(driverName);
        //3. 获取连接
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

1.2 方式二:通过读取properties

  1. 读取.properties
  2. 获取参数
  3. 加载驱动
  4. 获取连接
public void connection2(){//将参数提取到.properties中
    //1. 读取.properties
    InputStream inputStream = ConnectionTest.class.getClassLoader().getResourceAsStream("JDBC.properties");
    Properties info = new Properties();
    try {
        info.load(inputStream);
    } catch (IOException e) {
        e.printStackTrace();
    }
    //2. 获取参数
    String driverName = info.getProperty("driverName");
    String url = info.getProperty("url");
    String user = info.getProperty("user");
    String password = info.getProperty("password");
    try {
        //3. 加载驱动
        Class.forName(driverName);
        //4. 获取连接
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

1.3 封装JDBCUtils

  1. 获取连接:同方式二
  2. 关闭连接:关闭所打开的连接
public class JDBCUtils {
    public static Connection getConnection(){
        //1. 读取.properties
        InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("JDBC.properties");
        Properties info = new Properties();
        try {
            info.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 2. 获取参数
        String driverName = info.getProperty("driverName");
        String user = info.getProperty("user");
        String password = info.getProperty("password");
        String url = info.getProperty("url");
        //3. 加载驱动
        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            //4. 获取连接
            Connection connection = DriverManager.getConnection(url, user, password);
            //5. 返回连接对象
            return connection;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static void close(Connection connection, PreparedStatement preparedStatement){
        close(connection, preparedStatement, null);
    }
    public static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){

        try {
            connection.close();
            preparedStatement.close();
            if(resultSet!=null){
                resultSet.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

2. 操作数据

PS:为了代码清晰简洁易看(懒得写),所有异常全部抛出,记得一定要自己try-catch

2.1 修改数据(增、删、改)

  1. 获取连接
  2. 预编译
  3. 填充占位符
  4. 执行
  5. 关闭连接
public void alter(String sql,Object ...args) throws SQLException {
    //1. 获取连接
    Connection connection = JDBCUtils.getConnection();
    //2. 预编译
    PreparedStatement preparedStatement = connection.prepareStatement(sql);
    //3. 填充占位符
    if (args!=null){
        for (int i = 0; i < args.length; i++) {
            preparedStatement.setObject(i+1, args[i]);
        }
    }
    //4. 执行
    preparedStatement.execute();
    //5. 关闭连接
    JDBCUtils.close(connection, preparedStatement);
}

2.2 查询数据

  1. 获取连接
  2. 预编译
  3. 填充占位符
  4. 执行(获取结果集)
  5. 获取元数据
  6. 封装为对象
  7. 返回对象
public <T> T select(Class<T> clazz,String sql,Object ...args) throws Exception {
    //1. 获取连接
    Connection connection = JDBCUtils.getConnection();
    //2. 预编译
    PreparedStatement preparedStatement = connection.prepareStatement(sql);
    //3. 填充占位符
    if(args!=null){
        for (int i = 0; i < args.length; i++) {
            preparedStatement.setObject(i+1, args[i]);
        }
    }
    //4. 执行(获取结果集)
    ResultSet resultSet = preparedStatement.executeQuery();
    //5. 得到元数据
    ResultSetMetaData metaData = resultSet.getMetaData();
    //6. 封装为对象
    T t = null;
    int columnCount = metaData.getColumnCount();
    if(resultSet.next()){//游标
        t = clazz.newInstance();
        for (int i = 0; i < columnCount; i++) {
            String columnLabel = metaData.getColumnLabel(i + 1);
            Object columnValue = resultSet.getObject(i + 1);
            Field declaredField = clazz.getDeclaredField(columnLabel);
            declaredField.setAccessible(true);
            declaredField.set(t,columnValue);
        }
    }
    //7. 关闭连接
    JDBCUtils.close(connection, preparedStatement,resultSet);
    //8. 返回对象
    return t;
}

2.3 批处理

  • 将每次执行改为按批执行

    1. 加入Batch
    2. 执行Batch
    3. 清空Batch
  • 提升效率可以将自动提交改为手动提交

public void insertBatchTest() throws SQLException {        
    long startTime = System.currentTimeMillis(); //计时开始
    //1. 获取连接
    Connection connection = JDBCUtils.getConnection();
    connection.setAutoCommit(false); //关闭自动提交
    //2. 预编译
    String sql = "insert into `user_info` (`user`) value (?)";
    PreparedStatement preparedStatement = connection.prepareStatement(sql);
    for(int i = 0; i =< 5000; i++){
        //3. 填充占位符
        preparedStatement.setString(1, '1');
        //4. 执行
        //4.1 加入Batch
        preparedStatement.addBatch();
        if(i % 500 == 0){
            //4.2 执行Batch
            preparedStatement.executeBatch();
            //4.3 清空Batch
            preparedStatement.clearBatch();
        }
    }
    connection.commit(); //手动提交
    //5. 关闭连接
    JDBCUtils.close(connection, preparedStatement);
    long endTime = System.currentTimeMillis(); //计时结束
    System.out.println(endTime-startTime); //打印耗时
}

3 事务

自动提交

  1. DDL
  2. 默认情况下的DML
  3. 断开连接
  1. 关闭自动提交(setAutoCommit)
  2. 成功后提交 (commit)
  3. 失败后回滚(rollback)
public void transactionTest() {
    Connection connection = JDBCUtils.getConnection();
    PreparedStatement preparedStatement1 = null;
    PreparedStatement preparedStatement2 = null;
    //张三给李四转100
    try {
        //1. 关闭自动提交
        connection.setAutoCommit(false);

        String sql1 = "UPDATE `user_info` SET `balance`=`balance`-100 WHERE `user`='zhangsan';";
        preparedStatement1 = connection.prepareStatement(sql1);
        preparedStatement1.execute();

        // 模拟出错
        //int i = 3/0;

        String sql2 = "UPDATE `user_info` SET `balance`=`balance`+100 WHERE `user`='lisi';";
        preparedStatement2 = connection.prepareStatement(sql2);
        preparedStatement2.execute();

        //2, 手动提交
        connection.commit();
        System.out.println("交易成功");
    } catch (Exception e) {
        e.printStackTrace();
        try {
            //3. 回滚
            connection.rollback();
            System.out.println("交易失败,回滚");
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    } finally {
        try {
            connection.setAutoCommit(true);
            if (connection!=null){
                connection.close();
            }
            if (preparedStatement1!=null){
                preparedStatement1.close();
            }
            if (preparedStatement2!=null){
                preparedStatement2.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

相关文章

  • JDBC

    JDBC原理: JDBC: 抽取JDBC工具类 : JDBCUtils JDBC控制事务:

  • JDBC 的使用

    JDBC JDBC什么是JDBCJDBC 的使用JDBC 的数据类型 什么是JDBC JDBC(Java Data...

  • Java和MySQL简建立连接

    JDBC JDBC插入多条数据 JDBC查询 JDBC动态传参 JDBC回滚 JDBC将数据库中的信息装入List...

  • JDBC

    JDBC JDBC:Java DataBase Connectivity JDBC的本质是什么?JDBC是SUN公...

  • java异常合集

    jdbc com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorExce...

  • Day05--JDBC

    JDBC: 抽取JDBC工具类:JDBCUtils JDBC控制事务

  • JDBC

    JDBC: 抽取JDBC工具类 : JDBCUtils JDBC控制事务:

  • JDBC编程目录

    JDBC编程目录 SQL基础JDBC基础JDBC进阶

  • JDBC和数据库连接池

    1.JDBC介绍1.1 1.1 JDBC介绍 2.JDBC之API2.1 2.1 JDBC之API 3.JDBC例...

  • # day10_JDBC基础

    一,JDBC概述 1.为什么要使用JDBC 1.1没有JDBC 1.2有了JDBC后 2.JDBC的概念 2.1....

网友评论

      本文标题:JDBC

      本文链接:https://www.haomeiwen.com/subject/ixuluhtx.html