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);
}
}
来源:https://www.cnblogs.com/heyiyuanqi/p/16578096.html
本站部分图文来源于网络,如有侵权请联系删除。