Spring及Spring boot java程序访问数据库 JDBC

JDBC——规范客户端程序如何来访问数据库的应用程序接口

What is JDBC?
Java™ database connectivity (JDBC) is the JavaSoft specification of a standard application programming interface (API) that allows Java programs to access database management systems. The JDBC API consists of a set of interfaces and classes written in the Java programming language.

Using these standard interfaces and classes, programmers can write applications that connect to databases, send queries written in structured query language (SQL), and process the results.

Since JDBC is a standard specification, one Java program that uses the JDBC API can connect to any database management system (DBMS), as long as a driver exists for that particular DBMS.

简单来说:Java需要和多种不同的数据库打交道,因此定义了一种标准连接数据库

JDBC是Sun公司为了能够让SQL访问统一的一套纯JAVA API设计的一套接口,这种接口是遵循了微软的ODBC API模式。其驱动实现是各家数据库供应商编写的,通过JDBC API可以通过驱动实现数据库通信。

作者:MaxZing
链接:https://www.jianshu.com/p/8b249f375167
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Java程序访问数据库过程中涉及的角色

  • JDBC接口 定义访问数据的规范
  • 针对不同数据库的JDBC驱动 实现了JDBC接口用于访问目标数据库
    在这里插入图片描述

图源来自网络,侵删

角色交互 —— 以访问mysql为例
public class JDBCDemo {

    public static void main(String[] args) {
        JDBCDemo example = new JDBCDemo();
        Role role = example.getRole(1L);
        System.out.printf("role_name => " + role.getRoleName());
    }

    public Role getRole(Long id) {
        Connection connection = this.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            // 操作 Connection,打开 Statement 对象
            ps = connection.prepareStatement("select id,role_name,note from t_role where id = ?");
            ps.setLong(1,id);
            // 通过 Statement 执行 SQL,返回结果到 ResultSet 对象
            rs = ps.executeQuery();
            // 通过 ResultSet 读取数据,然后通过代码转化为具体的 POJO 对象
            while (rs.next()){
                Long roleId = rs.getLong("id");
                String roleName = rs.getString("role_name");
                String note = rs.getString("note");
                Role role = new Role();
                role.setId(id);
                role.setRoleName(roleName);
                role.setNote(note);
                return role;
            }
        } catch (SQLException e) {
            Logger.getLogger(JDBCDemo.class.getName()).log(Level.SEVERE,null,e);
        } finally {
            this.close(rs,ps,connection);
        }
        return null;
    }
    private Connection getConnection(){
        // 使用 JDBC 编程需要连接数据库,注册驱动和数据库信息
        Connection connection = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/datasource?characterEncoding=utf8";
            String user = "root";
            String password = "";
            connection = DriverManager.getConnection(url,user,password);
        } catch (ClassNotFoundException | SQLException e) {
            Logger.getLogger(JDBCDemo.class.getName()).log(Level.SEVERE,null,e);
            return null;
        }
        return connection;
    }
    private void close(ResultSet rs, Statement stmt, Connection connection){
        // 关闭数据库相关资源
        try {
            if (rs != null && !rs.isClosed()){
                rs.close();
            }
        } catch (SQLException e) {
            Logger.getLogger(JDBCDemo.class.getName()).log(Level.SEVERE,null,e);
        }
        try {
            if (stmt != null && !stmt.isClosed()){
                stmt.close();
            }
        } catch (SQLException e) {
            Logger.getLogger(JDBCDemo.class.getName()).log(Level.SEVERE,null,e);
        }
        try {
            if (connection != null && !connection.isClosed()){
                connection.close();
            }
        } catch (SQLException e) {
            Logger.getLogger(JDBCDemo.class.getName()).log(Level.SEVERE,null,e);
        }
    }
}
1.DriverManager拿到Driver保存

Spring及Spring boot 第四章-第一节 java程序访问数据库 JDBC Driver接口及Driver实现类

1. DriverManager以url、user、password信息获取连接

过程参考:
Spring及Spring boot 第四章-第一节 java程序访问数据库 JDBC DriverManager

之后会发现通过DriverManager进行一些控制信息判断之后会使用我们的Driver实现类实现连接,(DriverManager不是工作组件,当然不会由它建立连接)。

在这里插入图片描述

这里是通过com.mysql.cj.jdbc.Driver实现类NonRegisteringDriver实现connect
在这里插入图片描述
这里是单数据源
在这里插入图片描述
根据host信息创建连接(ConnectionImpl对象)
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值