微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

0811JDBC随笔

1.JDBC体系系统 一组规范:接口

  • JDBC接口(API)包括两个层次:
    • 面向应用的API:Java API,抽象接口,供应用开发人员使用(连接数据库,执行sql语句,获得结果)
    • 面向数据库的API:Java Driver API,供开发商开发数据库驱动程序

JDBC是sun公司提供一套用于数据库操作的接口,java程序员只需要面向这套接口编程即可。
不同的数据库厂商,需要针对这套接口,提供不同的实现集合,即为不同数据库的驱动

package com.aiguigu.connection;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.sqlException;
import java.util.Properties;

public class ConnectionTest {
    @Test
    public void testConnection1() throws sqlException {
        Driver driver = new com.MysqL.cj.jdbc.Driver();

        //jdbc:MysqL协议
        //local:IP地址
        //3306:MysqL端口号
        //test:test数据库
        String url = "jdbc:MysqL://localhost:3306/MysqL?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
        //将用户名密码封装在properties中
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","1234");

        Connection conn = driver.connect(url, info);

        System.out.println(conn);
    }

    //方式二:对方式一的迭代 在如下的方式中不出现第三方API,使程序具有更好的可移植性
    @Test
    public void testConnection2() throws Exception {
        //获取Driver实现类对象,使用反射
        Class<?> clazz = Class.forName("com.MysqL.cj.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();

        //2.提供要连接的数据库
        String url = "jdbc:MysqL://localhost:3306/MysqL?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";

        //提供要连接的用户名密码
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","1234");

        //获取连接
        Connection conn = driver.connect(url, info);
        System.out.println(conn);
    }

    @Test
    //方式三:使用DriverManager替换Driver
    public void testConnection3() throws Exception {
        //1.获取Drive实现类的对象
        Class<?> clazz = Class.forName("com.MysqL.cj.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();
        //2提供另外三个连接的基本信息
        String url = "jdbc:MysqL://localhost:3306/MysqL?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
        String user = "root";
        String password = "1234";

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

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

        System.out.println(connection);
    }
    @Test
    //方式三:使用DriverManager替换Driver
    public void testConnection4() throws Exception {
        //1.提供另外三个连接的基本信息
        String url = "jdbc:MysqL://localhost:3306/MysqL?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
        String user = "root";
        String password = "1234";
        //2.获取Drive实现类的对象
        Class.forName("com.MysqL.cj.jdbc.Driver");
        //相较于方式三可以省略如下操作:
//        Driver driver = (Driver) clazz.newInstance();
//        //注册驱动
//        DriverManager.registerDriver(driver);


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

        System.out.println(connection);
    }
    //方式五:将数据库连接的四个基本信息声明在配置文件中通过读取配置文件获取连接
    @Test
    public void getConnection5() throws Exception {
        //1.读取配置文件中4个基本信息
        InputStream is = ConnectionTest.class.getClassLoader().getResourceAsstream("jdbc.properties");

        Properties pros = new Properties();
        pros.load(is);

        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");

        //加载驱动
        Class.forName(driverClass);

        //获取连接
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);

    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐