脚本宝典收集整理的这篇文章主要介绍了JDBC-1(概述&建立),脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
基于宋红康老师所讲JDBC所作笔记
主要应用:将内存中的数据存储在关系型数据库中(也可存储于磁盘文件、XML数据文件中)
String url = "jdbc:mysql://localhost:3306/user_db?serverTimezone=UTC";
注:jdbc:mysql://localhost:3306/atguigu?useUnicode=true&characterEncoding=utf8(如果JDBc程序与服务器端的字符集不一致,会导致乱码,那么可以通过参数指定服务器端的字符集)
public class Connectiontest {
@Test
public void testConnection1() throws SQLException {
Driver driver = new com.mysql.cj.jdbc.Driver();
//jdbc:mysql 协议
//localhost: ip地址
//3306:默认mysql端口号; test:数据库
String url = "jdbc:mysql://localhost:3306/user_db?serverTimezone=UTC"; //统一资源定位符
//将用户名和密码封装在此
PRoPErties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "root");
Connection connection = driver.connect(url, info);
System.out.println(connection);
}
}
@Test //2禁止第三方迭代
public void testConnection2() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
//1.用反射实现获取Driver实现类对象
Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) clazz.newInstance();
//2.提供要连接的数据库
String url = "jdbc:mysql://localhost:3306/user_db?serverTimezone=UTC";
//3.提供连接需要的用户名和密码
Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "root");
//4.获取链接
Connection connection = driver.connect(url, info);
System.out.println(connection);
}
@Test //3使用DriverManager替代Driver
public void testConnection3() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
//1.使用DriverManager替代Driver
Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) clazz.newInstance();
//2.提供要连接的数据库
String url = "jdbc:mysql://localhost:3306/user_db?serverTimezone=UTC";
//3.提供连接需要的用户名和密码
Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "root");
//注册驱动
DriverManager.registerDriver(driver);
//获取连接
//4.获取链接
Connection connection = DriverManager.getConnection(url, info);
System.out.println(connection);
}
@Test //4 相较于方式三省略类的显式加载流程(与MySQL Driver实现类静态代码块有关)
public void testConnection4() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
//1.获取三个连接的基本信息
String url = "jdbc:mysql://localhost:3306/user_db?serverTimezone=UTC";
String user = "root";
String password = "root";
//2.加载Driver(Driver提供了一个静态代码块,加载时自动执行)
Class.forName("com.mysql.cj.jdbc.Driver");
//3.获取连接
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection.toString());
}
}
@Test //5 将数据库连接的基本信息配置在配置文件中,通过读取配置文件的方式获取连接
public void testConnection5() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
//1. 读取配置文件的4个配置信息
InputStream inputStream = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties prop = new Properties();
prop.load(inputStream);
String user = prop.getProperty("user");
String password = prop.getProperty("password");
String url = prop.getProperty("url");
String driverClass = prop.getProperty("driverClass");
//2.加载驱动
Class.forName(driverClass);
//3.启动连接
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
url=jdbc:mysql://localhost:3306/user_db?serverTimezone=UTC
user=root
password=root
driverClass=com.mysql.cj.jdbc.Driver
优势:
以上是脚本宝典为你收集整理的JDBC-1(概述&建立)全部内容,希望文章能够帮你解决JDBC-1(概述&建立)所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。