一、 最古老的方法(通过 Driver 接口直接连接数据库)


  1. 首先创建一个 Driver 实现类的对象

            Driver dirver = new com.mysql.jdbc.Driver();
  2. 准备连接数据库的基本信息:url、user、password
         String url = "jdbc:mysql://127.0.0.1:3306/java_jdbc";
    Properties info = new Properties();
    info.put("user", "root");
    info.put("password", "123456");
  3. 调用 Driver 借口的 connect(url, info) 获取数据库连接
             Connection connection = dirver.connect(url, info);

  实例代码:

         @Test
public void testDriver() throws SQLException {
// 1. 创建一个 Driver 实现类的对象
Driver dirver = new com.mysql.jdbc.Driver(); // 2. 准备链接数据库的基本信息:url, user, password
String url = "jdbc:mysql://127.0.0.1:3306/java_jdbc";
Properties info = new Properties();
info.put("user", "root");
info.put("password", "123456"); // 3. 调用 Driver 借口的 connect(url, info) 获取数据库连接
Connection connection = dirver.connect(url, info);
System.out.println(connection);
}

二、编写一个通用的方法(通过Driver 接口实现


  1. 定义数据库基本信息变量

         String driverClass = null;
    String jdbcUrl = null;
    String user = null;
    String password = null;
  2. 创建一个properties文件,用来存放数据库连接基本信息
         driver=com.mysql.jdbc.Driver
    jdbcUrl=jdbc:mysql://127.0.0.1:3306/java_jdbc
    user=root
    password=123456
  3. 读取类路径下的jdbc.properties 文件
     InputStream in =
    getClass().getClassLoader().getResourceAsStream("jdbc.properties");
    Properties properties = new Properties();
    properties.load(in);
  4. 从properties文件获取数据库基本信息
     driverClass = properties.getProperty("driver");
    jdbcUrl = properties.getProperty("jdbcUrl");
    user = properties.getProperty("user");
    password = properties.getProperty("password");
  5. 通过反射创建 Driver 对象
     Driver driver = (Driver) Class.forName(driverClass).newInstance();
  6. 准备用户名和密码,并连接数据库
     Properties info = new Properties();
    info.put("user", user);
    info.put("password", password);
    Connection connection = driver.connect(jdbcUrl, info);

实例代码:

     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 的 connect 方法连接数据库.
Connection connection = driver.connect(jdbcUrl, info); return connection;
} @Test
public void testGetConnection() throws Exception {
System.out.println(getConnection());
}

三、通过DriverManager 驱动管理类获取数据库连接


  1. 准备数据库连接的四个字符串

    1).创建 properties 对象

 Properties properties = new Properties();

    2).获取jdbc.properties 对应的输入流

 InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");

    3).加载对应的输入流

     properties.load(inputStream);

    4).具体决定 user、password、url、driver四个字符串

 String driver = properties.getProperty("driver");
String jdbcUrl = properties.getProperty("jdbcUrl");
String user = properties.getProperty("user");
String password = properties.getProperty("password");

  2. 加载数据库驱动程序

 Class.forName(driver);

  3. 通过 Drivermanager 的 getConnection() 方法获取数据库连接

 return DriverManager.getConnection(jdbcUrl, user, password);

实例代码:

     @Test
public void testGetConnection23() throws Exception {
System.out.println(getConnection2());
}
public Connection getConnection2() throws IOException, ClassNotFoundException, SQLException{ Properties properties = new Properties(); InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties"); properties.load(inputStream); String driver = properties.getProperty("driver");
String jdbcUrl = properties.getProperty("jdbcUrl");
String user = properties.getProperty("user");
String password = properties.getProperty("password"); Class.forName(driver); return DriverManager.getConnection(jdbcUrl, user, password);
}

本文只是作者的笔记,只供参考,有错误欢迎指出!2017-10-28  11:14:03

JDBC连接数据库的几种方法的更多相关文章

  1. C#连接数据库的四种方法(转)

    C#连接数据库的四种方法 在进行以下连接数据库之前,请先在本地安装好Oracle Client,同时本次测试System.Data的版本为:2.0.0.0. 在安装Oracle Client上请注意, ...

  2. 转 mysql 远程连接数据库的二种方法

    mysql 远程连接数据库的二种方法   一.连接远程数据库: 1.显示密码 如:MySQL 连接远程数据库(192.168.5.116),端口“3306”,用户名为“root”,密码“123456” ...

  3. Java通过JDBC连接数据库的三种方式!!!并对数据库实现增删改查

    前言 java连接数据库完整流程为: 1,获得驱动(driver),数据库连接(url),用户名(username),密码(password)基本信息的三种方式. 2,通过获得的信息完成JDBC实现连 ...

  4. Qt连接数据库的两种方法

    我曾经想过,无论在哪个平台下开发,都不要再接触SQL Server了,但显然不行.我们是来看世界的,不是来改变世界的,想通就好. 前两天,尝试了一下Qt下远程访问数据库.在macOS下,用Qt 5.1 ...

  5. JDBC连接数据库的四种方式:DriverManager,DataSource,DBCP,C3P0

    方法1:使用java.sql.DriverManager类 驱动管理器类,用于管理所有注册的驱动程序. (注:DataSource 接口是 JDBC 2.0 API 中的新增内容,它提供了连接到数据源 ...

  6. mysql 远程连接数据库的二种方法

    http://blog.csdn.net/freecodetor/article/details/5799550 一.连接远程数据库: 1.显示密码 如:MySQL 连接远程数据库(192.168.5 ...

  7. mysql 远程连接数据库的二种方法

    一.连接远程数据库: 1.显示密码 如:MySQL 连接远程数据库(192.168.5.116),端口"3306",用户名为"root",密码"123 ...

  8. JDBC的概念、实现原理与连接数据库的几种方法

    1.首先要知道jdbc(概念): 使用Java代码发送sql语句的技术就是jdbc技术(jdbc英文全称:Java DataBase Connectivity,java数据库连接).即jdbc是一个接 ...

  9. MySQL之连接数据库的两种方法

    方法一: package DB; import java.sql.Connection; import java.sql.DriverManager; public class Conn { // 定 ...

随机推荐

  1. 6656 Watching the Kangaroo

    6656 Watching the KangarooDay by day number of Kangaroos is decreasing just liketiger, whale or lion ...

  2. 关于修改extmail附件大小限制的位置

    一.修改extmail的webmail.cf文件, SYS_MESSAGE_SIZE_LIMIT = 5242880 注意:以位为单位为5M字节. SYS_MESSAGE_SIZE_LIMIT = x ...

  3. Node.js之循环依赖

    在Node.js中有可能会出现循环依赖的问题,在此做一个简单的记录 假如有一个模块A: exports.loaded = false; const b = require('./b'); module ...

  4. 个人工作中ssd、audio python脚本总结

    1.os.system(cmd)或者os.popen(cmd)调用外部命令 cmd中需要注意特殊字符的转义功能,如: USBSTOR\DISK&VEN_GENERIC-&PROD_SD ...

  5. SqlServer和Oracle中一些常用的sql语句3 行列转换

    --217, SQL SERVER SELECT Cust_Name , MAX(CASE WHEN Order_Date ='2009-08-01' THEN AR END) "2009- ...

  6. 一道javascript面试题(闭包与函数柯里化)

    要求写一个函数add(),分别实现能如下效果: (1)console.log(add(1)(2)(3)(4)()); (2)console.log(add(1,2)(3,4)()); (3)conso ...

  7. winPcap编程之打开适配器并捕获数据包(四 转)

    在贴源码之前先介绍一个将要用到的很重要的函数--pcap_open(),下面是pcap_open()在remote-ex.h中的声明: pcap_t *pcap_open(const char *so ...

  8. 【解决方案】M2Crypto不支持python3

    问题现象:python3的环境下,导入M2Crypto模块报错 "ModuleNotFoundError: No module named 'M2Crypto",通过pip ins ...

  9. 双11电商剁手节,最全的H5互动营销案例合集

    距离双11不足一个月! 是否准备好为双十一疯狂剁手! 自从天猫2009年首创双11购物节以来双十一不仅成为了消费者的"剁手日" 更是每年企业营销的"狂欢节" 各 ...

  10. Delphi实现电脑端微信图片文件解密

    电脑端微信收到图片后是存在了“C:\Users\系统用户名\Documents\WeChat Files\微信帐号\Data”目录下的,但文件不能直接使用图片浏览器打开的,因为做了一些加密,之前有个朋 ...