JDBC技术(一):JDBC概念、如何通过JDBC连接MySQL数据库

本文主要内容:JDBC概念、如何通过JDBC连接MySQL数据库

JDBC

Java Database Connectivity,简称JDBC,是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法

JDBC连接MySQL数据库的几种方式

方式一:创建Driver()对象
	@Test
	public void test1() throws SQLException {
    	Driver driver = new com.mysql.cj.jdbc.Driver();
   		 //jdbc:mysql:协议 localhost:ip地址 3306:端口号 mt:数据库名 ?serverTimezone=UTC设置时区,否则报错
    	String url = "jdbc:mysql://localhost:3306/mt?serverTimezone=UTC";
    	//将用户名和密码封装在Properties中
    	Properties info = new Properties();
    	info.setProperty("user", "root");
    	info.setProperty("password", "写你自己数据库密码");
    	Connection conn = driver.connect(url, info);
    System.out.println(conn);
}

方式二: 通过反射获取Driver()对象,好处是不会出现第三方API
	@Test
	public void test2() throws Exception {
    	//1. 获取Driver实现类的对象: 使用反射
    	Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
    	Driver driver = (Driver) clazz.newInstance();

    	//2. 提供要连接的数据库
    	String url = "jdbc:mysql://localhost:3306/mt?serverTimezone=UTC";

    	//3. 提供要连接的用户名和密码
    	Properties info = new Properties();
    	info.setProperty("user", "root");
    	info.setProperty("password", "写你自己数据库密码");

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

方式三:使用DriverManger替代Driver
	@Test
	public void test3() throws Exception {
    	//1. 获取Driver的实现类对象
    	Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
    	Driver driver = (Driver) clazz.newInstance();

    	//2. 提供连接信息
    	String url = "jdbc:mysql://localhost:3306/mt?serverTimezone=UTC";
    	String user = "root";
    	String password = "写你自己数据库密码";

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

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

方式四:可以只加载驱动,不再显示注册驱动
	@Test
	public void test4() throws Exception {
    	//1. 提供连接信息
    	String url = "jdbc:mysql://localhost:3306/mt?serverTimezone=UTC";
    	String user = "root";
    	String password = "写你自己数据库密码";

    	//2. 加载Driver
    	Class.forName("com.mysql.cj.jdbc.Driver");

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

方式五:将数据库连接需要的4个基本信息声明在配置文件中
	@Test
	public void test5() throws Exception {
    	//1. 读取配置文件中的基本信息
   	 InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
    	Properties pros = new Properties();
    	if (is != null) {
        	pros.load(is);
    	}

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

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

        //3. 获取连接
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
	}
}
	jdbc.properties格式如下:
		user=root
		password=你自己的数据库密码
		url=jdbc:mysql://localhost:3306/mt?serverTimezone=UTC
		driverClass=com.mysql.cj.jdbc.Driver
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值