Java_jdbc 基础笔记之一 数据库连接
方式一:
1、创建一个Driver实现类的对象
2、准备连接数据库的基本信息:url,user,password
3、调用Driver接口的connect(url,info)获取数据库连接
* Driver 是一个接口:数据库厂商必须提供实现的接口,能从其中获取数据库的连接。 1.加入mysql驱动
* 1)解压mysql-connector-java-5.1.7.zip
* 2)在当前项目下新建lib目录
* 4)右键bulid-path,add to
* buildpath 加入到类路径下
@Test
public void testDriver() throws SQLException {
// 1 创建Driver实现类的对象
Driver driver = new Driver();
// 2、准备连接数据库的基本信息:url,user,password
String url = "jdbc:mysql://127.0.0.1:3306/test";
Properties properties = new Properties();
properties.put("user", "root");
properties.put("password", "admin");
// 3、调用Driver接口的connect(url,properties)获取数据库连接
Connection conn = driver.connect(url, properties);
System.out.println(conn);
}
方式二:
/**
* 编写一个通用的方法,在不修改源程序的情况下,可以获取任何数据库的连接
* 解决方案:把数据库驱动Driver实现类的全类名、url、user、pasword 放入一个配置文件中,
* 通过修改配置文件的方式实现和具体的数据库的解耦
*
* @throws IOException
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*
*/ public Connection getConnection() throws Exception {
String driverClass = null;
String urljdbc = null;
String user = null;
String password = null;
// 读取类路径下的jdbc.properties文件
InputStream is = getClass().getClassLoader().getResourceAsStream(
"jdbc.properties");
Properties properties = new Properties();
properties.load(is);// .....加载进来
driverClass = properties.getProperty("driver");
urljdbc = properties.getProperty("urljdbc");
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(urljdbc, info);
return connection; } @Test
public void testConnection() throws Exception {
System.out.println(getConnection());
}
转: https://blog.csdn.net/YL1214012127/article/details/48210857
Java_jdbc 基础笔记之一 数据库连接的更多相关文章
- Java_jdbc 基础笔记之八 数据库连接(写一个查询Student对象的方法)
public Student getStudent(String sql, Object... args) { // 查询Student对象 Student stu = null; Connectio ...
- Java_jdbc 基础笔记之七 数据库连接(方法升级)
之前的更新方法 public static void update(String sql) { Connection conn = null; Statement statement = null; ...
- Java_jdbc 基础笔记之六 数据库连接 (PreparedStatement)
reparedStatement 是 Statement 的子接口 * ①需要预编译 SQL 语句:PreparedStatement ps = conn.preparedStatement(sql) ...
- Java_jdbc 基础笔记之五 数据库连接 (ResultSet)
/** * ResultSet: 结果集. 封装了使用 JDBC 进行查询的结果. * 1. 调用 Statement 对象的 executeQuery(sql)可以得到结果集. * 2. Resul ...
- Java_jdbc 基础笔记之四 数据库连接 (通用更新方法)
/** * 写一个通用的更新方法 包括 INSERT. DELETE.UPDATE * 使用工具类 * @param sql */ public void update(String sql){ Co ...
- Java_jdbc 基础笔记之三 数据库连接 (Statement)
/** * 通过JDBC向之指定的数据表中插入一条记录 1 Statement :用于执行SQL语句的对象 * ==>通过Connection的createStatement()方法来获取 == ...
- Java_jdbc 基础笔记之二 数据库连接
/** * DriverManager 类是驱动程序管理器类 * 1)可以通过重载的getConnection()方法获取数据库的连接,较为方便 * 2)可以同时管理多个驱动程序:若注册了多个数据库连 ...
- Java_jdbc 基础笔记之十五 数据库连接(取得数据库自动生成的主键)
public class testGetKeyValue { /** * 取得数据库自动生成的主键 */ @Test public void testGeneratedKeys() { Connect ...
- Java_jdbc 基础笔记之十四 数据库连接(元数据)数据库信息及连接信息
public class MetaDatatest { /** * DatabaseMetaData 是描述 数据库的元数据对象 可以由Connection得到 */ @Test public voi ...
随机推荐
- springboot2.1.3 + redisTemplate + Lock 操作 redis 3.0.5
近期在整合springboot + redis 的功能,本来想用原生的jedit api,最后想想有点 low,搜了一把,boot已经提供给我们操作的方法,那就是 使用 redisTemplate 或 ...
- mac 下enable mysql的load data in file
1)使用root用户登录mysql 2)将 local_infile 变量设置为true SET GLOBAL local_infile = true; 3)重启数据库 在系统偏好设置中找到MySql ...
- SpringBoot找不到html资源的原因
SpringBoot在写完Controller之后直接启动访问,但是找不到相对应的页面资源,报404错误. 我的Controller编写没有错误,html文件也放在了templates文件夹下,但是为 ...
- 软件测试_Loadrunner_性能测试_服务器资源监控
使用Loadrunner进行Windows服务器性能监控 将装有Loadrunner的机器叫做监控端,被监控资源的服务器叫做被监控端 一.前置环境设置 1. 设置被监控端管理员账户可用:我的电脑右键- ...
- Vuex基础 -01 -实现简易计数器 -支持 加数/ 减数/ 奇数再加/ 异步加法(setTimeout 1000ms) -单组件演示语法
Vuex 的结构图 工程组织 Vuex的核心管理程序 store.js /* vuex的核心管理程序 */ import Vue from 'vue' import Vuex from 'vuex' ...
- Oracle字符串中包含数字、特殊符号的排序
问题描述: 某小区,需要按照小区.楼栋.单元号.房间号进行排序,但是按照地址描述排序时,因为字符串中包含数字,所以造成了如下的结果, 1号楼之后应该是2号楼,但是查询结果却是10号楼 . 尝试解决 使 ...
- LA 3704细胞自动机——循环矩阵&&矩阵快速幂
题目 一个细胞自动机包含 $n$ 个格子,每个格子的取值为 $0 \sim m-1$.给定距离 $d$,则每次操作是将每个格子的值变为到它的距离不超过 $d$ 的所有格子的在操作之前的值的和除以 $m ...
- Linux 颜色设置
echo显示带颜色,需要使用参数-e格式如下:echo -e "\033[字背景颜色;文字颜色m字符串\033[0m"例如: echo -e "\033[41;37m T ...
- qDeleteAll与clear
qDeleteAll:专门用于指针容器,对容器或者迭代器中的每个对象进行delete操作,而不是从容器中移除对象.源代码如下: void qDeleteAll(ForwardIterator begi ...
- Navicat连接mysql报错1251 -client does not support authentication protocol
原文https://blog.csdn.net/qq_35654080/article/details/82588188 详解请参考https://blog.csdn.net/chszs/articl ...