jdbc学习----1.内容概述

    技术2022-07-11  100

    JDBC

    java中的数据存取技术分为如下几类:

    JDBC直接访问数据库JDO(Java Data Object)第三方O/R工具,如:Hibernate,Mybatis等等

    JDBC是java访问数据库的基石,JDO、HIbernate、Mybatis等都是对JDBC的封装. JDBC(Java Database Connectivity)是一个独立于特定数据库管理系统,通用的sql数据库存取和操作的公共接口(一组API),定义了用来访问数据库的java类库(java.sql,javax.sql),使用这些类库可以以一种标注的方法、方便的访问数据库的数据. JDBC为访问不同的数据库提供了一种统一的途径,为开发者屏蔽了一些细节 JDBC的目标是使java程序员使用JDBC可以连接任何提供了JDBC驱动程序的数据库系统,这样就使得程序员无需对特地的数据库系统的特点有过多的了解,从而大大简化和加快了开发过程。 JDBC接口包括两个层次: 面向应用的APILJava API,抽象接口,供应用程序开发人员使用(连接数据库,执行sql语句,获得结果) 面向数据库的API:Java Driver APi,供开发商开发数据库驱动程序用 而我们只需要面向这一套接口编程,引入对应的数据库驱动即可.

    获取数据库连接

    driver接口介绍

    java.sql.driver接口是所有jdbc驱动程序需要实现的接口,这个接口是提供给数据库厂商使用的,不同数据库厂商提供不同的实现,在程序中不需要直接去访问实现了driver接口的类,而是由驱动程序管理类(java.sql.DriverManager)去调用这些Driver实现 连接要素: url,端口号,ip地址,用户名,密码,要链接的数据库,时区

    import com.mysql.cj.jdbc.Driver; import org.junit.Test; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; public class ConnectionTest { /** * jdbc:mysql:协议 * localhost:ip地址 * 3306:默认mysql的端口号 * test:指明要连接的数据库 * properties:封装用户名和密码 * useSSL=false&serverTimezone=UTC:mysql新版本需要设置时区,否则会抛出异常 * @throws SQLException */ @Test public void test1() throws SQLException { Driver driver = new Driver(); String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC"; Properties properties = new Properties(); properties.setProperty("user","root");//设置用户名 properties.setProperty("password","123456");//设置面 Connection connect = driver.connect(url, properties); System.out.println("获取到的连接:"+connect); connect.close(); } }

    结果:获取到的连接:com.mysql.cj.jdbc.ConnectionImpl@67784306

    /** * 反射获取对象 * @throws Exception */ @Test public void test2() throws Exception{ Class<Driver> clazz = (Class<Driver>) Class.forName("com.mysql.cj.jdbc.Driver"); Driver driver = clazz.getDeclaredConstructor().newInstance(); String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC"; Properties properties = new Properties(); properties.setProperty("user","root"); properties.setProperty("password","123456"); Connection connect = driver.connect(url, properties); connect.close(); System.out.println("获取到的连接:"+connect); } /** * 使用drivermanager替代driver * @throws Exception */ @Test public void test3() throws Exception{ Class<Driver> clazz = (Class<Driver>) Class.forName("com.mysql.cj.jdbc.Driver"); Driver driver = clazz.getDeclaredConstructor().newInstance(); String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC"; DriverManager.registerDriver(driver);//注册驱动可以省略 Connection connection = DriverManager.getConnection(url, "root", "123456"); System.out.println("获取到的连接:"+connection); connection.close(); }

    因为Driver会自动注册:下面是源码部分

    public class Driver extends NonRegisteringDriver implements java.sql.Driver { // // Register ourselves with the DriverManager // static { try { java.sql.DriverManager.registerDriver(new Driver()); } catch (SQLException E) { throw new RuntimeException("Can't register driver!"); } } /** * 将连接所需参数保存在配置文件 * @throws Exception */ @Test public void test4() throws Exception{ InputStream inputStream = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties"); Properties properties = new Properties(); properties.load(inputStream); String url = properties.getProperty("url"); String user = properties.getProperty("user"); String password = properties.getProperty("password"); Connection connection = DriverManager.getConnection(url, user, password); System.out.println("获取到的连接:"+connection); connection.close(); }
    Processed: 0.010, SQL: 9