<span style="font-size:24px;">package src.com.jdbc.java;

import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties; import org.junit.Test; import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Driver; public class JDBCTest {
@Test
public void testGetConnection2() throws Exception{
System.out.println(getConnection2());
}
public Connection getConnection2() throws Exception{
//1、准备连接数据库的四个字符
//1)、创建properties对象
Properties properties=new Properties(); //2)、获取jdbc.properties对象
InputStream in=this.getClass().getClassLoader().getResourceAsStream("jdbc.properties"); //3)、加载2)对应的输入流
properties.load(in);
//4)、具体决定user,passwrod等四个字符串
String user=properties.getProperty("user");
String password=properties.getProperty("password");
String jdbcUrl=properties.getProperty("jdbcUrl");
String driver=properties.getProperty("driver"); //2、加载数据库驱动程序(对应的 Driver 实现类中有注册驱动的静态代码块.)
Class.forName(driver);
//3、通过DriverManager的getConnection()方法获取数据库的连接。
return (Connection) DriverManager.getConnection(jdbcUrl,user,password);
}
/**
* DriverManager 是驱动的管理类.
* 1、可以通过重载的getconnection()方法获取连接,
* 2、可以同时管理多个驱动程序;注册了多个数据库连接,则调用getconnection()方法
* 时传入的参数不同,即返回不同的数据库连接
* @throws IOException
* @throws ClassNotFoundException
* @throws SQLException
*
*/
@Test
public void testDriverManager() throws Exception{
//1、准备连接数据库的四个字符
String driverClass = null;
String jdbcUrl = null;
String user = null;
String 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");
//2、加载数据库驱动程序(对应的 Driver 实现类中有注册驱动的静态代码块.)
Class.forName(driverClass);
//3、通过DriverManager的getConnection()方法获取数据库的连接。
Connection connection=(Connection) DriverManager.getConnection(jdbcUrl,user,password);
System.out.println(connection);
}
@Test
public void testDriver() throws SQLException {
Driver driver = new com.mysql.jdbc.Driver();
String url = "jdbc:mysql://127.0.0.1:3306/cjl";
Properties info = new Properties();
info.put("user", "root");
info.put("password", "root123");
Connection connection = (Connection) driver.connect(url, info);
System.out.println(connection); } /**
* 编写一个通用方法:把数据库驱动Driver实现类的全类名、URL、user、password放入
* 一个配置文件中,通过修改配置文件的方式实现和具体的数据库解偶
*
* @throws Exception
*/
public Connection getConnection() throws Exception {
String driverClass = null;
String jdbcUrl = null;
String user = null;
String 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的connection方法获取数据库连接
Connection connection = (Connection) driver.connect(jdbcUrl, info);
return connection;
}
@Test
public void testGetConnection() throws Exception{
System.out.println(getConnection());
} }
</span>

jdbc.properties文件中的配置

driver=com.mysql.jdbc.Driver

jdbcUrl=jdbc:mysql://127.0.0.1:3306/cjl

user=root

password=root123

JDBC_获取数据库连接的更多相关文章

  1. <一>获取数据库连接

    一.JDBC_通过Driver接口获取数据库连接 1. Driver是一个接口:数据库厂商必须提供实现的接口,可以从其中 获取数据库连接. 2.JDBC URL由三部分组成,各部分用冒号隔开,格式:j ...

  2. JAVA jdbc获取数据库连接

    JDBC获取数据库连接的帮助类 import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManage ...

  3. 使用DriverManager获取数据库连接的一个小改进

    由于使用DriverManager获取数据库连接时,由于DriverManager实现类中有一段静态代码块,可以直接注册驱动,且可以同时管理多个驱动程序 所以当换数据库连接时需要指定不同的数据库,那么 ...

  4. 使用DriverManager获取数据库连接

    DriverManager 是驱动的管理类 * 1).可以通过重载的getConnection() 方法获取数据库连接,较为方便 * 2).可以同时管理多个驱动程序,若注册了多个数据库连接,则调用ge ...

  5. 通过Driver获取数据库连接

    先看一下文件,在当前包下有一个properties配置文件,在根目录下有一个lib文件夹,里面放的是mySql的驱动jar包 Driver :是一个接口,数据库厂商必须提供实现的接口,能从其中获取数据 ...

  6. 获取数据库连接对象Connection

    2018-11-04  19:50:52 开始写 public Connection getConn() {//返回类型为Connection try { Class.forName("co ...

  7. JDBC编程:获取数据库连接

    JDBC(Java Database Connectivity),即Java数据库连接.通过JDBC编程,可以使Java应用程序和数据库进行交互. JDBC驱动的方式有很多种,我们常用的驱动方式为:本 ...

  8. JDBC 学习笔记(四)—— JDBC 加载数据库驱动,获取数据库连接

    1. 加载数据库驱动 通常来说,JDBC 使用 Class 类的 forName() 静态方法来加载驱动,需要输入数据库驱动代表的字符串. 例如: 加载 MySQL 驱动: Class.forName ...

  9. 获取数据库连接的方式 & Statement操作数据库的弊端

    1.获取数据库连接的方式 TestConnection package com.aff.connection; import java.io.InputStream; import java.sql. ...

随机推荐

  1. Http record java

    http://httpunit.sourceforge.net/doc/servletunit-intro.html https://code.google.com/p/http-impersonat ...

  2. java selenium webdriver实战 helloWord

    第一步:建立Maven项目 Selenium 支持 maven 工程,这会让你的工作更加简便. 用 Eclipse 建个 Maven 的工程,建成后,修改 pom.xml <dependenci ...

  3. C++学习笔记6

    泛型算法 1. 算法怎样工作 每一个泛型算法的实现都独立于单独的容器.这些算法还是大而不全的,而且不依赖于容器存储的元素类型.为了知道算法怎样工作,让我们深入了解find 操作.该操作的任务是在一个未 ...

  4. c++程序内存泄露检測工具

    功能: 用于检測c++程序的内存泄露. 原理: 事实上非常easy,就是通过函数的重载机制,捕获应用程序的new, new[] , delete , delete[], malloc,calloc,f ...

  5. iOS开发-Protocol协议及委托代理(Delegate)传值

    前言:因为Object-C是不支持多继承的,所以很多时候都是用Protocol(协议)来代替.Protocol(协议)只能定义公用的一套接口,但不能提供具体的实现方法.也就是说,它只告诉你要做什么,但 ...

  6. video标签 拖动 转自w3school

    调整视频大小 播放 暂停 用js实现 详细参见http://www.w3school.com.cn/tiy/t.asp?f=html5_video_dom 图片的拖动详见http://www.w3sc ...

  7. AsyncSocket 使用

    今天使用AsyncSocket模拟及时通信,在这里记录一下,免得以后自己又犯相同的错误 1>创建客户端和服务器socket /** * 设置socket */ - (void)setupSock ...

  8. If the server requires more time, try increasing the timeout in the server editor

    双击服务器,在overview下的Timeouts中的Start选项,改成10000或者较大就可以了.防止服务器自启动频繁.

  9. codeforces 609F. Frogs and mosquitoes 二分+线段树

    题目链接 F. Frogs and mosquitoes time limit per test 2 seconds memory limit per test 512 megabytes input ...

  10. git版本控制的笔记

    一.配置你的身份,提交代码时git就可以知道是谁提交的了 git config --global user.name "Tony" git config --global user ...