JDBC连接数据库的几种方法
一、 最古老的方法(通过 Driver 接口直接连接数据库)
- 首先创建一个 Driver 实现类的对象
Driver dirver = new com.mysql.jdbc.Driver();
- 准备连接数据库的基本信息: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"); - 调用 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 接口实现)
- 定义数据库基本信息变量
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null; - 创建一个properties文件,用来存放数据库连接基本信息
driver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://127.0.0.1:3306/java_jdbc
user=root
password=123456 - 读取类路径下的jdbc.properties 文件
InputStream in =
getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(in); - 从properties文件获取数据库基本信息
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);
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).创建 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连接数据库的几种方法的更多相关文章
- C#连接数据库的四种方法(转)
C#连接数据库的四种方法 在进行以下连接数据库之前,请先在本地安装好Oracle Client,同时本次测试System.Data的版本为:2.0.0.0. 在安装Oracle Client上请注意, ...
- 转 mysql 远程连接数据库的二种方法
mysql 远程连接数据库的二种方法 一.连接远程数据库: 1.显示密码 如:MySQL 连接远程数据库(192.168.5.116),端口“3306”,用户名为“root”,密码“123456” ...
- Java通过JDBC连接数据库的三种方式!!!并对数据库实现增删改查
前言 java连接数据库完整流程为: 1,获得驱动(driver),数据库连接(url),用户名(username),密码(password)基本信息的三种方式. 2,通过获得的信息完成JDBC实现连 ...
- Qt连接数据库的两种方法
我曾经想过,无论在哪个平台下开发,都不要再接触SQL Server了,但显然不行.我们是来看世界的,不是来改变世界的,想通就好. 前两天,尝试了一下Qt下远程访问数据库.在macOS下,用Qt 5.1 ...
- JDBC连接数据库的四种方式:DriverManager,DataSource,DBCP,C3P0
方法1:使用java.sql.DriverManager类 驱动管理器类,用于管理所有注册的驱动程序. (注:DataSource 接口是 JDBC 2.0 API 中的新增内容,它提供了连接到数据源 ...
- mysql 远程连接数据库的二种方法
http://blog.csdn.net/freecodetor/article/details/5799550 一.连接远程数据库: 1.显示密码 如:MySQL 连接远程数据库(192.168.5 ...
- mysql 远程连接数据库的二种方法
一.连接远程数据库: 1.显示密码 如:MySQL 连接远程数据库(192.168.5.116),端口"3306",用户名为"root",密码"123 ...
- JDBC的概念、实现原理与连接数据库的几种方法
1.首先要知道jdbc(概念): 使用Java代码发送sql语句的技术就是jdbc技术(jdbc英文全称:Java DataBase Connectivity,java数据库连接).即jdbc是一个接 ...
- MySQL之连接数据库的两种方法
方法一: package DB; import java.sql.Connection; import java.sql.DriverManager; public class Conn { // 定 ...
随机推荐
- 寻找bug并消灭系列——记录在Android开发所遇到的bug(一)
之前使用了Android Studio的插件直接为button绑定了监听器,并实现onClick方法(我的onClick方法无论点击哪一个都是要实现setcontentview这个方法设置layout ...
- 剑指offer(纪念版)读书笔记【实时更新】
C++ 1.STL的vector每次扩充容量,新容量是前一次的两倍. 2.32位机指针大小为4个字节,64位机指针大小为8个字节. 3.当数组作为函数参数传递时,数组会自动退化成同类型指针. 4. & ...
- 图解clientWidth,offsetWidth,scrollWidth,scrollTop
新手看到这几个属性,很头疼,参考了网上一些文章,加上自己实践,给出对这几个属性的解释 我把代码贴上来,方便大家验证 在chrome浏览器中,不知为什么图片容器高度比图片高度多了4px,把图片设置为bl ...
- ES中const
前 言 EScript 上一次总结了,ES中let和var的区别,今天在带大家了解另一个声明关键词:const. const实际上保证的,并不是变量的值不得改动,而是变量指向的那个内存地址不得改 ...
- 系统出现异常: too many values to unpack (expected 2)
先感谢[ValueError: too many values to unpack](http://leonzhan.iteye.com/blog/1720315)系统出现异常:打开太多值(预期2)这 ...
- python中html解析-Beautiful Soup
1. Beautiful Soup的简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.pyt ...
- 第八章 关于SQL查询出错的一些问题
问题一:在使用MySQL使用传参查询并返回结果集时,没错,小伙伴们都知道少不了Statement接口和PreparedStatement对象.问题来了,有时竟然查询不了,Debug进去,发现执行的SQ ...
- 媲美jQuery的JS框架——AngularJS(一)
前言 相信原生JS大家都不陌生,至于为什么说原生,是因为在JS的基础上衍生出了很多的框架.有些框架的使用非常广泛,甚至已经达到了媲美原生的地步.在之前的文章中给大家介绍了jQuery这一介绍框架.今天 ...
- mac 通过brew安装php70 +php-fpm+ phalcon3.0.3
安装php7.0.15 brew install homebrew/php/php70 brew install homebrew/php/php70-mcrypt brew install home ...
- Hadoop2.7.3集群搭建
hadoop2.0已经发布了稳定版本了,增加了很多特性,比如HDFS HA.YARN等.最新的hadoop-2.4.1又增加了YARN HA 注意:apache提供的hadoop-2.4.1的安装 ...