JDBC初步——获取数据库连接

1、概述

  • 数据的持久化:
    (1)把数据保存到可掉电式存储设备中以供使用,而持久化大多数依靠数据库来完成
  • Java中的存储技术
    (1)JDBC直接访问数据库(是数据库访问的基石,下面的技术知识更好的封装了JDBC)
    (2)JDO(Java Data Object)技术
    (3)第三方O/R工具,如Hibernate、Mybatis等
  • JDBC介绍
    (1)可以理解成是一个独立于特定的数据库管理系统,通用的SQL数据库存储和公共接口(一组API),定义了访问数据库的标准Java类库(Java.sql、Javax.sql)
    jabc流程
  • JDBC编写步骤
    在这里插入图片描述

2、获取数据库连接(四个迭代版本)

方式一:显示出第三方数据库的API
    @Test
    public void testConnection1() throws SQLException {
    
    

        //提供java.sql.Driver接口实现类的对象
        Driver driver = new com.mysql.cj.jdbc.Driver();

        //提供url,指明具体要操作的数据
        String url = "jdbc:mysql://localhost:3306/xscj?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";

        //提供Properties,指明用户名和密码
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","123456");

        //调用driver的connect()方法获取连接
        Connection connection = driver.connect(url,info);

        System.out.println(connection);

    }
     

方式二:体现面向接口编程的思想
    @Test
    public void testConnection2() throws SQLException {
    
    

        //通过反射实例化Driver
        String className = “com.mysql.cj.jdbc.Driver”;
        Class clazz = Class.forName(className);
        Driver driver = (Driver)clazz.newInstacne();

        //提供url,指明具体要操作的数据
        String url = "jdbc:mysql://localhost:3306/xscj?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";

        //提供Properties,指明用户名和密码
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","123456");

        //调用driver的connect()方法获取连接
        Connection connection = driver.connect(url,info);

        System.out.println(connection);

    }	

方式三:使用DriverManager实现数据库连接,体会数据库连接的四要素
    @Test
    public void testConnection3() throws SQLException {
    
    

        //数据库连接的四要素
        String url = "jdbc:mysql://localhost:3306/xscj?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
		String user = "root";
		String password = "123456";
		String driverName = "com.mysql.cj.jdbc.Driver"

        //实例化Driver
        Class clazz = Class。forName(driverName);
        Driver driver = (Driver)clazz.newInstance();

		//注册驱动
		DriverManager.registerDriver(driver);

		//获取连接
        Connection connection = DriverManager.getConnection(url,user,password);

        System.out.println(connection);

    }	

方式四:不必使用显示的注册驱动,因为在DriverManager的源码中已经存在静态代码块,实现驱动的注册
    @Test
    public void testConnection4() throws SQLException {
    
    

        //数据库连接的四要素
        String url = "jdbc:mysql://localhost:3306/xscj?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
		String user = "root";
		String password = "123456";
		String driverName = "com.mysql.cj.jdbc.Driver"


		//加载驱动(实例化Driver以及注册驱动)
		Class。forName(driverName);

		//获取连接
        Connection connection = DriverManager.getConnection(url,user,password);

        System.out.println(connection);

    }	

3、数据库连接(最终版本)重点!!!

1.在src目录下配置文件jdbc.properties,优点:
(1)实现代码和数据的分离,修改配置信息的时候,省去重新编译的步骤

方式五:
    @Test
    public void testConnection4() throws SQLException {
    
    

        //加载配置文件
        InputStream is = ConnectionTest.class.getClassloader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(is);



		//读取配置信息
		String user = properties.getProperty("user");
		String password = properties.getProperty("password");
		String url = properties.getProperty("url");
		String driverClass = properties.getProperty("driverClass");

		//获取连接
        Connection connection = DriverManager.getConnection(url,user,password);

        System.out.println(connection);

    }	

	在配置文件中声明
	user=root
	password=123456
	url=jdbc:mysql://localhost:3306/xscj?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
	driverClass=com.mysql.cj.jdbc.Driver

猜你喜欢

转载自blog.csdn.net/qq_45151059/article/details/113775129