java的jdbc

1.什么是JDBC

JDBC是一个独立于特定数据库管理系统、通用的SQL数据库存取和操作的公共接口,面向接口编程,为访问不同的数据库提供了一种统一的途径,为开发者屏蔽了一些细节问题,各个数据库厂商根据JDBC的规范制作的 JDBC 实现类的类库。主要面向两个层次,面向应用的api:供应应用程序开发人员使用和面向数据库的api:供开发商开发数据库驱动程序用

2.Driver接口:所有的jdbc驱动程序需要实现的接口

#config.properties
user=root
password=密码
url=jdbc:mysql://localhost:3306/table?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&useSSL=false&verifyServerCertificate=false
driverClass=com.mysql.cj.jdbc.Driver
<!--父pom.xml-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>cn.jdbc.test</groupId>
	<artifactId>jdbctest</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<!-- 必须为pom -->
	<packaging>pom</packaging>
	<name>jdbctest</name>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<!-- 依赖的自定义实现 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>mysql</groupId>
				<artifactId>mysql-connector-java</artifactId>
				<version>8.0.11</version>
			</dependency>
		</dependencies>
	</dependencyManagement>
</project>
 
<!--子pom.xml-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>cn.jdbc.test2</groupId>
	<artifactId>jdbctest2</artifactId>
	<name>jdbctest2</name>
	<description>jdbctest2</description>
	<parent>
		<groupId>cn.jdbc.test</groupId>
		<artifactId>jdbctest</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<!-- 相对该pom.xml的父pom.xml的位置 -->
		<relativePath>../jdbctestparent/pom.xml</relativePath>
	</parent>
	<dependencies>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
	</dependencies>
</project>
public class DriverTest1 {

	public static void main(String[] args) throws Exception {
		Connection connection = DriverTest1.getConnection("config.properties");
		System.out.println(connection);
	}

	// 适用于mysql的jdbc连接
	//@Test // test兼容性不好不能使用
	public static Connection getMysqlConnection() throws SQLException {
		// mysql5用的驱动url是com.mysql.jdbc.Driver,mysql6以后用的是com.mysql.cj.jdbc.Driver。
		// 版本不匹配便会报驱动类已过时的错误。 JDBC连接Mysql6
		// com.mysql.cj.jdbc.Driver需要指定时区serverTimezone,
		// 否则会报错如下
		/*
		 * Exception in thread "main" java.sql.SQLException: The server time
		 * zone value '̨±±±ê׼ʱ¼ä' is unrecognized or represents more than one
		 * time zone. You must configure either the server or JDBC driver (via
		 * the serverTimezone configuration property) to use a more specifc time
		 * zone value if you want to utilize time zone support. 时区不统一
		 */
		Driver driver = new Driver();
		/*
		 * mysql需要提供的一些信息的源码 public DriverPropertyInfo[] getPropertyInfo(String
		 * url, Properties info) throws SQLException { String host = "";//主机ip
		 * localhost (url中写了) String port = "";//端口3306(url中写了) String database
		 * = "";数据库 table (url中写了) String user = "";数据库账号 (properties需要提供)
		 * String password = "";数据库密码 (properties需要提供) url
		 * 格式:jdbc:mysql://localhost:3306/table 说明:jdbc:固定格式 mysql:协议
		 * //localhost:3306主机名和端口/test数据库名,后续还可连接如下
		 * mysql.url=jdbc:mysql://127.0.0.1:3306/table?serverTimezone=UTC&
		 * useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&
		 * useSSL=false&verifyServerCertificate=false&autoReconnct=true&
		 * autoReconnectForPools=true&allowMultiQueries=true
		 * serverTimezone=UTC:时区utc(世界标准时间),我过时间在其上加8个小时即可 有两种方法:方法1:在mysql中使用
		 * set global time_zone='+8:00'; 方法2:&serverTimezone=GMT%2B8 来设置为中国时区
		 * useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
		 * 处理与数据库字符集不统一导致的乱码问题, 这3个一般一起书写,含义如下
		 * 本质:useUnicode=true&characterEncoding=utf-
		 * 8的作用是指定character_set_client和character_set_connection的值,
		 * 在jdbc链接中使用characterSetResults=UTF-8,即可设置character_set_results的值
		 * 系统变量character_set_client:用来指定解析客户端传递数据的编码
		 * 系统变量character_set_connection:用来指定数据库内部处理时使用的编码
		 * 系统变量character_set_results:用来指定数据返回给客户端的编码方式 useSSL=false
		 * 不需要使用SSL连接,你需要通过设置useSSL=false来显式禁用SSL连接。 SSL:Secure Sockets
		 * Layer(安全套接层) verifyServerCertificate=false 服务证书
		 * 设置useSSL=false与verifyServerCertificate=false可以解决如下异常警告 Mon Aug 06
		 * 23:00:16 CST 2018 WARN: Establishing SSL connection without server's
		 * identity verification is not recommended. According to MySQL 5.5.45+,
		 * 5.6.26+ and 5.7.6+ requirements SSL connection must be established by
		 * default if explicit option isn't set. For compliance with existing
		 * applications not using SSL the verifyServerCertificate property is
		 * set to 'false'. You need either to explicitly disable SSL by setting
		 * useSSL=false, or set useSSL=true and provide truststore for server
		 * certificate verification.
		 * 
		 * autoReconnct=true 超时自动重连 autoReconnectForPools=true 针对数据库重连策略
		 * allowMultiQueries=true 允许批量更新
		 * 
		 * (后续mysql中说)
		 */
		Properties properties = new Properties();
		properties.put("user", "root");
		properties.put("password", "cgz12345678");
		Connection connect = driver.connect(
				"jdbc:mysql://localhost:3306/table?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8",
				properties);
		return connect;
	}

	public static Connection getConnection(String path) throws Exception {
		Properties properties = new Properties();
		properties.load(new FileInputStream(path));
		String driverClass=properties.getProperty("driverClass");
		// 为什么使用sql呢,因为这里使用的是jdbc规范,也就是说都要实现jdbc的Driver接口
		// 所以这里采用的是多态的方式
		java.sql.Driver driver = (java.sql.Driver) Class.forName(driverClass).newInstance();
		
		String url = properties.getProperty("url");
		Connection connect = driver.connect(url, properties);
		return connect;
	}

}

 未完待续

猜你喜欢

转载自www.cnblogs.com/gg128/p/9434345.html