jdbc-1-入门

介绍
  • jdbc:java database connectivity,是java规范中描述客户端如何访问数据库的接口
    • 注意:jdbc面向的关系型的
  • 包括:
    • java api:位于java.sql和javax.sql包中
    • database api:规范各个数据库的接入标准
预备
  • 下载mysql workbench的mac版本
  • 数据库url:
    • 示例1:Jdbc:mysql://localhost:3306/test?key1=param1&key2=param2
    • 示例2:jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=gbk
Jdbc:连接协议
     mysql:连接子协议
     Localhost:3306:连接地址
     test:数据库名称
     &:存在编码问题,用该编码代替”&“
     ?号后面的都是连接时的参数,加上时,会覆盖默认的连接参数
  • url参数文档:
    • https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html
    • 常用的url参数有:
User:用户名
password:用户密码
connectTimeout:socket连接超时时间
maxReconnects:重新连接时间
useSSL:是否使用useSSL,默认为false
characterEncoding:编码格式,一般为utf-8
serverTimezone:设置MySQL的时区
  • mysql驱动
    • 注意:8.x版本的mysql驱动有非常大的变化,需要注意

连接案例
package connection;

import org.junit.Test;

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 First {

    private static String url = "jdbc:mysql://localhost:3306/test";
    private static String user = "root";
    private static String password = "123456";

    /**
     * 方式1:直接声明驱动和连接属性
     */
    @Test
    public void testGetConnection01() throws SQLException {
        // 1. 声明驱动
        Driver driver = new com.mysql.jdbc.Driver();
        // 2. 连接db,参数:
        //  url、
        //  Properties info:连接属性,至少得包括user、password两个key
        Properties info = new Properties();
        info.setProperty("user", user);
        info.setProperty("password", password);
        // 3. 开始连接
        Connection connect = driver.connect(url, info);
        System.out.println("new得到连接:" + connect);
    }

    /**
     * 方式2:使用反射机制生成
     */
    @Test
    public void testGetConnection02() throws Exception {
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver)aClass.newInstance();
        Properties info = new Properties();
        info.setProperty("user", user);
        info.setProperty("password", password);
        Connection connect = driver.connect(url, info);
        System.out.println("反射得到连接:" + connect);
    }

    /**
     * 方式3:使用DriverManager-详细版
     */
    @Test
    public void testGetConnection03() throws Exception {
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver)aClass.newInstance();
        // 注册Driver
        DriverManager.registerDriver(driver);

        Connection connection = DriverManager.getConnection(url, user, password);

        System.out.println("DriverManager得到连接:" + connection);
    }
    /**
     * 方式4:使用DriverManager-精简版
     *  但实际上,使用过程中,最好还是声明一下具体的驱动类,因为非mysql的可能并不一定是这么实现的
     */
    @Test
    public void testGetConnection04() throws Exception {
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("简化版DriverManager得到连接:" + connection);
        /**
         * 因为mysql的Driver有个静态代码块,在类加载的时候,就已经注册了DriverManager:
         *     static {
         *         try {
         *             java.sql.DriverManager.registerDriver(new Driver());
         *         } catch (SQLException E) {
         *             throw new RuntimeException("Can't register driver!");
         *         }
         *     }
         */
    }

    /**
     * 方式5:使用DriverManager+配置方式
     *  1. 配置与代码分离,实现解耦
     *  2. 
     */
    @Test
    public void testGetConnection05() throws Exception {
        InputStream resourceAsStream = First.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(resourceAsStream);

        Class<?> driverClass = Class.forName(properties.getProperty("driverClass"));
        Connection connection = DriverManager.getConnection(properties.getProperty("url"),
                properties.getProperty("user"),
                properties.getProperty("password"));
        System.out.println("配置文件版的DriverManager得到连接:" + connection);
    }
}

  • 输出:
    在这里插入图片描述
参考
  • 尚硅谷jdbc教程
  • 延伸:
    • 以前的SSH框架:structs、spring、Hibernate
    • 现在的SSM框架:spring、springmvc、mabatis
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值