复习整理的DAY4_JDBC

我们经常说JDBC,那么JDBC到底是什么呢?

JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。通过JDBC,我们可以搭建更高级的工具和接口,完成我们编写的程序。

JDBC开发的一般步骤:

1.加载驱动Class.forName();

2.创建数据库连接

3.写SQL语句

4.创建Statement

5.执行SQL语句(1)更新(2)查询

6.关闭连接

比如:

public void test() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java?useUnicode=true&characterEncoding=UTF-8", "root", "123456");
            String sql = "SELECT `name`, age, gender FROM student WHERE id = ?";
            statement = connection.prepareStatement("sql");
            statement.setInt(1, 6);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            List<Student> list = new LinkedList<>();
            while (resultSet.next()) {
                // 每遍历一次,就拿出数据库查询结果里面的一行记录,就是一个学生的信息,
                // 就可以封装成一个Student对象
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                Student student = new Student(id, name, age, gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }


    }

注意几点:

1.可以用Statement,也可以用PreparedStatement,但是PreparedStatement是预编译的,对于批量处理可以大大提高效率,一般我们都使用PreparedStatement。两者在使用时要注意,Statement创建时调用的是connection的createStatement函数,没有参数,而PreparedStatement调用的是preparestatement(注意没有d),有参数sql,所以在ResultSet使用时,两者也是不同,PreparedStatement没有参数,Statement有参数sql。

2.关闭连接的顺序一般都是按照最先的最后这个顺序来,即先resultset,然后statement,最后才是connection。

3.异常处理虽然可以写在一个catch里,但是按标准一般都是分开写

4.在捕获异常时要注意空指针的情况,所以需要先判断是否为null,再进行捕获异常。

而为了提高代码利用率,接下来进行了更为具体的工具类封装:

创建JDBCUtil工具类

public class JDBCUtil {
    private JDBCUtil() {

    }
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws SQLException {

        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java?useUnicode=true&characterEncoding=UTF-8", "root", "123456");
        return connection;
    }
    public static void close(Connection connection, PreparedStatement statement, ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

}

具体调用

public void test() {
        int selectId = 5;
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            // 加载驱动Class.forName("");
            connection = JDBCUtil.getConnection();
            String sql = "SELECT id, `name`, age, gender FROM student WHERE id >= ?";
            
            statement = connection.prepareStatement(sql);
            // 填上?占位符的值
            statement.setInt(1, selectId);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            List<Student> list = new LinkedList<>();
            while (resultSet.next()) {
                // 每遍历一次,就拿出数据库查询结果里面的一行记录,就是一个学生的信息,
                // 就可以封装成一个Student对象
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                Student student = new Student(id, name, age, gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }
    }

这样在具体使用时,就不用重复写那些代码了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值