#数据库驱动
driver=com.mysql.jdbc.Driver
#数据库连接
url=jdb:mysql://localhost:3306/newdb3?useUnicode=true&characterEncoding=utf8
#数据库用户名
username=root
#数据库密码
password=root
#数据库连接池初始连接数
initialSize=3
#数据库连接池最连接线程数
maxActive=5

DButils的编写

package util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties; import org.apache.commons.dbcp.BasicDataSource; public class DBUtils { private static BasicDataSource dataSource; static {
//读取属性配置文件
Properties prop = new Properties();
//得到文件输入流
InputStream ips = DBUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
//把文件流加载到prop对象中
try {
prop.load(ips);
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
String initSize = prop.getProperty("initialSize");
String maxSize = prop.getProperty("maxActive"); //创建连接池数据源对象
dataSource = new BasicDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
//设置连接池策略信息
dataSource.setInitialSize(Integer.parseInt(initSize));
dataSource.setMaxActive(Integer.parseInt(maxSize)); } catch (IOException e) {
e.printStackTrace();
}
} public static Connection getConn() throws SQLException{
return dataSource.getConnection();
} public static void close(Connection conn,Statement stat,ResultSet rs) throws SQLException {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw e;
}
} if(stat != null) {
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
throw e;
}
} if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw e;
}
}
} }

JDBC配置文件db.properties(Mysql) 及dbutils的编写的更多相关文章

  1. db.properties是干什么用的

    连接池配置文件db.properties是java中采用数据库连接池技术完成应用对数据库的操作的配置文件信息的文件.具体配置项目如下:drivers=com.microsoft.sqlserver.j ...

  2. j2ee 使用db.properties连接mysql数据库

    转自: http://blog.csdn.net/u013815546/article/details/50808493 注: 下面的方法是未安装构架的写法,需要自己加载驱动并建立连接. 若引入了Ac ...

  3. 无法解析db.properties,spring报错:Caused by: java.sql.SQLException: unkow jdbc driver : ${url}

    db.properties中配置了url等jdbc连接属性: driver=org.sqlite.JDBCurl=jdbc:sqlite:D:/xxx/data/sqliteDB/demo.dbuse ...

  4. spring配置文件引入properties文件:<context:property-placeholder>标签使用总结

    一.问题描述: 1.有些参数在某些阶段中是常量,比如: (1)在开发阶段我们连接数据库时的连接url.username.password.driverClass等 (2)分布式应用中client端访问 ...

  5. Spring/Maven/MyBatis配置文件结合properties文件使用

    使用properties文件也叫注入,比如把一些常用的配置项写入到这个文件,然后在Spring的XML配置文件中使用EL表达式去获取. 这种方式不只Spring可以使用,同样MyBatis也可以使用, ...

  6. 003医疗项目-关于<context:property-placeholder location="classpath:db.properties"/>的问题

    项目结构如下:

  7. log4CXX第二篇---配置文件(properties文件)详解

    一.Log4j简介 Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以何种形式输出.综合使 ...

  8. Jdbc使用SSH连接mysql

    pom.xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-ja ...

  9. mySql版本的相关问题:com.mysql.cj.jdbc.Driver和com.mysql.jdbc.Driver

    Mysql版本的相关问题:com.mysql.cj.jdbc.Driver和com.mysql.jdbc.Driver 1. 在使用mysql时,控制台日志报错如下: Loading class `c ...

随机推荐

  1. 1、Dubbo源码解析--Dubbo如何驱动Spring IOC容器并配合工作的?

    首先Spring要注入自己的bean需要在Spring-provider.xml(提供者spring注入文件,名字可能不一样)添加bean注入,其中有dubbo的自定义标签,xml如何识别这些标签?拿 ...

  2. PAT 1021 Deepest Root

    #include <cstdio> #include <cstdlib> #include <vector> using namespace std; class ...

  3. vuex深入浅出

    本文主要记录使用vuex的使用场景.重要组成部分和学习心得. 1.说在前面 学习vue有两周的时间了,目前已经对vue的基础使用比较熟悉了.但是一直对vuex的使用耿耿于怀,这么说是因为总是不太理解, ...

  4. Android NDK开发 网址

    http://www.cnblogs.com/shangdahao/archive/2013/05/01/3053146.html http://www.cnblogs.com/shangdahao/ ...

  5. Exchange 域用户无权管理邮箱

    将需要管理邮箱的域用户添加至“Microsoft Exchange Security Groups”用户组即可.

  6. 【Leetcode】【Easy】Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  7. 操作系统页面置换算法之FIFO,LRU

    #include<iostream> #include<unistd.h> #include<vector> #include<wait.h> #inc ...

  8. Android(java)学习笔记21:Java异常处理机制

    1. try....catch  /  try...catch...finally package cn.itcast_02; /* * 我们自己如何处理异常呢? * A:try...catch... ...

  9. BZOJ2648/2716:SJY摆棋子/[Violet]天使玩偶(K-D Tree)

    Description 这天,SJY显得无聊.在家自己玩.在一个棋盘上,有N个黑色棋子.他每次要么放到棋盘上一个黑色棋子,要么放上一个白色棋子,如果是白色棋子,他会找出距离这个白色棋子最近的黑色棋子. ...

  10. 【转】JS模块化工具requirejs教程(二):基本知识

    前一篇:JS模块化工具requirejs教程(一):初识requirejs 我们以非常简单的方式引入了requirejs,这一篇将讲述一下requirejs中的一些基本知识,包括API使用方式等. 基 ...