Java Web学习12(JDBC)

一、JDBC的概念

JDBC(Java Database Connectivity) java应用程序和数据库的连接技术
JDBC是sun公司推出的一套用于访问不同数据库的规范或标准(一组接口或抽象类)、
java.sql.*

二、JDBC的好处

1、开发人员不用记多套APi,减轻了开发压力
2、维护性提高

三、JDBC的使用步骤

1、加载驱动(加载MySQL驱动)
将MySQL提供的一套连接的实现类加载到应用程序中
2、获取连接
3、访问数据库的数据,实现增删改查
4、关闭连接

四、获取mysql连接

方式一

package com.yinggu.demo1;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import org.junit.Test;

/**
 * 此类用于演示获取连接的几种方式
 * 
 * @author:黑猴子的家
 * @博客 :https://www.jianshu.com/u/37fd8e2dff4c
 * 
 * 连接url的格式: jdbc:mysql://主机名:端口号/库名 
 * 注意:如果是本机并且为默认端口号,则可以直接写成:
 *         jdbc:mysql:///库名
 * 
 *
 */
public class TestConnection2 {
	
	// 方式一:
	@Test
	public void test1() throws Exception {
		
		// 1.加载驱动
		Class.forName("com.mysql.jdbc.Driver");

		// 2.获取连接
		Connection connection = DriverManager.getConnection("jdbc:mysql:///girls?user=root&password=root");

		System.out.println(connection);

	}

}


方式二

	@Test
	public void test2() throws Exception {
		// 1.加载驱动
		Class.forName("com.mysql.jdbc.Driver");

		// 2.获取连接
		Connection connection = DriverManager.getConnection("jdbc:mysql:///girls", "root", "root");

		System.out.println(connection);

	}

方式三

code

	// 方式三:
	@Test
	public void test3() throws Exception { 
		// 1.加载驱动
		Class.forName("com.mysql.jdbc.Driver");

		// 2.获取连接
		Properties info = new Properties();
		info.load(new FileInputStream("src/db.properties"));
		
		Connection connection = DriverManager.getConnection("jdbc:mysql:///girls", info);

		System.out.println(connection);

	}

创建db.properties 文件

db.properties
user=root
password=root
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql:///girls
#url=jdbc:mysql://127.0.0.1:3306/girls

方式四

	// 方式四:通用的方式
	@Test
	public void test4() throws Exception {

		Properties info = new Properties();
		info.load(new FileInputStream("src/db.properties"));

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

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

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

		System.out.println(connection);

	}

方式五

import java.sql.Connection;
import java.sql.DriverManager;

public class TestConnection01 {
	public static void main(String[] args) throws Exception {
		Class.forName("com.mysql.cj.jdbc.Driver");
		String url = "jdbc:mysql://localhost:3306/girls?serverTimezone=UTC";
		Connection connection = DriverManager.getConnection(url, "root", "YingGu123456!");
		System.out.println(connection);
	}
}

尖叫提示:方式五主要是连接mysql8.0.19数据库

这个时区要设置好,不然会出现时差,
如果你设置serverTimezone=UTC,连接不报错,
但是我们在用java代码插入到数据库时间的时候却出现了问题。
比如在java代码里面插入的时间为:2018-06-24 17:29:56
但是在数据库里面显示的时间却为:2018-06-24 09:29:56
有了8个小时的时差
UTC代表的是全球标准时间 ,但是我们使用的时间是北京时区也就是东八区,领先UTC八个小时。
//北京时间东八区
serverTimezone=GMT%2B8
//或者使用上海时间
serverTimezone=Asia/Shanghai

驱动jar包

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.19</version>
</dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值