<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. android小知识之邮箱地址输入自动完成

    虽然不难,但是容易忘记,做个备忘吧 package com.guet.zhuge; import android.app.Activity; import android.os.Bundle; imp ...

  2. zookeeper 伪集群模式

    问题二:开发没有足够机器,一台机子上是否装三个zookeeper服务器集群. 问题解答: 这种安装模式只能说是一种伪集群模式.三个zookeeper服务器都安装在同一个服务器(platform)上,需 ...

  3. 51nod 1239 欧拉函数之和(杜教筛)

    [题目链接] https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239 [题目大意] 计算欧拉函数的前缀和 [题解] 我们 ...

  4. CF R303 div2 C. Woodcutters

    C. Woodcutters time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. API和DLL

    API API(Application Programming Interface,应用编程接口)其实就是操作系统留给应用程序的一个调用接口,应用程序通过调用操作系统的 API 而使操作系统去执行应用 ...

  6. XGPush集成(信鸽集成)demo

    #import "AppDelegate.h" #import "XGPush.h" #import "XGSetting.h" #defi ...

  7. php 图片等比缩放

    /** * @method 图片等比缩放 * @param string $srcImage 源图片路径 * @param string $toFile 目标图片路径 * @param integer ...

  8. 自己动手写 ASP.NET MVC 分页 part1

    学习编程也有一年半载了,从来没有自己动手写过东西,都是利用搜索软件找代码,最近偶发感慨,难道真的继续做码农??? 突发奇想是不是该自己动手写点东西,可是算法.逻辑思维都太弱了,只能copy网上的代码, ...

  9. bzoj 1057: [ZJOI2007]棋盘制作 单调栈

    题目链接 1057: [ZJOI2007]棋盘制作 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 2027  Solved: 1019[Submit] ...

  10. [LeetCode]题解(python):068-Text Justification

    题目来源: https://leetcode.com/problems/text-justification/ 题意分析: 输入一个字符串数组和一个规定长度L.将这个字符串数组的元素尽可能放到长度的L ...