获取数据库链接Junit

简介:
+关注继续查看

package com.xt.test;


import java.io.InputStream;

import java.sql.SQLException;

import java.util.Properties;


import org.junit.Test;


import com.mysql.jdbc.Connection;

import com.mysql.jdbc.Driver;

/**

 * 测试连接数据库,获取数据库链接

 * 其中有读取资源文件的方法。

 * @author LZF

 */

public class TestDBConnection {

/**

 * 直接硬编码连接mysql

 * @throws SQLException

 */

@Test

public void testJdbc() throws SQLException{

Driver driver=new com.mysql.jdbc.Driver();

String url="jdbc:mysql://127.0.0.1:3306/test";

Properties info=new Properties();

info.put("user", "root");

info.put("password", "");

Connection connection=(Connection) driver.connect(url, info);

System.out.println(connection);

}

/**

 * 通过配置,将数据库的基本信息放置到配置文件中。

 * @throws ClassNotFoundException 

 * @throws IllegalAccessException 

 * @throws InstantiationException 

 * @throws SQLException

 */

public Connection getConnection() throws Exception{

//读取类路径下的jdbc.properties

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

Properties properties=new Properties();

properties.load(in);

String driverClass=properties.getProperty("driverClass");

String jdbcUrl=properties.getProperty("jdbcUrl");

String userName=properties.getProperty("userName");

String password=properties.getProperty("password");

if(password==null&&"".equals(password)){

password="";

}

Properties info=new Properties();

info.put("user", userName);

info.put("password", password);

//通过反射创建对象

Driver driver=(Driver)Class.forName(driverClass).newInstance();

Connection connection=(Connection) driver.connect(jdbcUrl, info);

return connection;

}

@Test

public void testGetConnection() throws Exception{

System.out.println(this.getConnection());

}

}













本文转自lzf0530377451CTO博客,原文链接:http://blog.51cto.com/8757576/1544454 ,如需转载请自行联系原作者




相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
3月前
|
XML 设计模式 Java
Spring——整合junit4、junit5使用方法
Spring——整合junit4、junit5使用方法
|
5月前
|
druid Java 数据库连接
Spring整合Mybatis&Junit单元测试
Spring整合Mybatis&Junit单元测试
|
8月前
|
SQL Java 关系型数据库
mybatis批量执行sql的处理 spring boot
mybatis批量执行sql的处理 spring boot
151 0
|
10月前
|
XML Oracle Java
SpringCloud+MyBatis(oracle)逆向工程自动生成代码
平时我们的开发过程,除了系统框架的搭建。其他无非就是CRUD增删改查的代码逻辑搬砖,CRUD也就避免不了要跟数据库打交道。
233 0
SpringCloud+MyBatis(oracle)逆向工程自动生成代码
|
10月前
|
Java 测试技术 数据库连接
SSM项目使用junit单元测试时Mybatis通配符加载Mapper不能正常加载
SSM项目使用junit单元测试时Mybatis通配符加载Mapper不能正常加载
179 2
|
10月前
|
XML SQL Java
Mybatis-Plus中实现使用xml文件来写复杂sql
Mybatis-Plus中实现使用xml文件来写复杂sql
879 0
|
11月前
|
SQL Java 数据库连接
mybatis中sql和include标签使用后idea识别不了where
mybatis中sql和include标签使用后idea识别不了where
mybatis中sql和include标签使用后idea识别不了where
|
Java 数据库连接 数据库
Spring整合MyBatis和Junit
Spring整合MyBatis和Junit
Spring整合MyBatis和Junit
|
Java 数据库连接 数据库
mybatis学习(13): 连接数据库之前的准备(测试连接)
mybatis学习(13): 连接数据库之前的准备(测试连接)
109 0
mybatis学习(13): 连接数据库之前的准备(测试连接)
|
SQL XML 存储
【MyBatis系列3】MyBatis SQL执行流程(二)
在《【MyBatis系列1】基础知识(上)》中,我们讲解了MyBaits的工作原理,以及它的四大核心组件的使用姿势,包括SqlSessionFactoryBuilder、SqlSessionFactory、SqlSession和SQL Mapper。在《【MyBatis系列1】基础知识(下)》中,通过完整的MayBatis使用示例,详细讲解了MyBatis的XML配置文件。
170 0
【MyBatis系列3】MyBatis SQL执行流程(二)
推荐文章
更多