首页 技术 正文
技术 2022年11月10日
0 收藏 376 点赞 2,356 浏览 1945 个字
package day_18;
import jdk.internal.util.xml.impl.Input;
import org.junit.Test;import java.io.InputStream;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
import java.util.logging.Logger;/**
* Driver 只是一个接口,数据库厂商必须提供的接口,能从中获取数据库连接
* 一:加载方法
* 1.加入mysql 驱动
* 2.解压 mysql-connector-java-5.1.7.zip ,复制jar文件并添加进工程中
* 3.Driver() throws Exception *connection

  public interface Connection
  extends Wrapper, AutoCloseable与特定数据库的连接(会话)。 执行SQL语句并在连接的上下文中返回结果。

Connection对象的数据库能够提供描述其表,其支持的SQL语法.
*/
public class test1 {
@Test
public void testDriver() throws Exception{
///1.创建一个Driver 实现类的对象
Driver driver = new com.mysql.jdbc.Driver();
String url="jdbc:mysql://localhost:3306/books"; //数据库所在的主机IP或者localhost
//2.准备连接数据库的基本信息:url,user,password
Properties info=new Properties();
info.put("user", "root");
info.put("password", "123456");
//3.调用Driver接口的 connect(url,info) 获取数据库连接
Connection connection=driver.connect(url,info);
System.out.println(connection);
//连接成功:输出:com.mysql.jdbc.JDBC4Connection@27ddd392
}
/**二:通用的方法
* 编写一个通用的方法,在不修改源程序的情况下,可以获取任何数据库的连接
* 解决方案:
* 把数据库驱动driver 实现类的全类名、url、user、password放入一个配置文件中
* 通过修改配置文件的方法 实现和具体的数据库解耦。
*/
@Test //显示正常:com.mysql.jdbc.JDBC4Connection@19e1023e
public void testGetConnection() throws Exception{
System.out.println(getConnection());
} public Connection getConnection() throws Exception{
String driverClass=null,jdbcUrl=null,user=null,password=null;
//读取类路径下的jdbc.properties 文件
InputStream in=
getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties =new Properties();
properties.load(in);
driverClass =properties.getProperty("driver");
jdbcUrl=properties.getProperty("jdbcUrl");
user = properties.getProperty("user");
password = properties.getProperty("password"); //运用反射新建一个通用的 driver对象
Driver driver = (Driver)Class.forName(driverClass).newInstance(); Properties info=new Properties();
info.put("user", user);
info.put("password", password); //通过Driver 的connect方法获取数据库的连接
Connection connection=driver.connect(jdbcUrl, info);
return connection;
}
}

通用的数据库连接方法需要新建:

jdbc.properties (直接建立在SRC工程下)

JDBC课程1-实现Driver接口连接mysql数据库、通用的数据库连接方法(使用文件jdbc.properties)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,495
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,909
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,741
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,496
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,134
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298