Created
July 26, 2012 08:20
-
-
Save MaySnow/3180938 to your computer and use it in GitHub Desktop.
访问数据库工作类(带数据库连接池DBCP)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.InputStream; | |
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Properties; | |
import org.apache.commons.dbcp.BasicDataSource; | |
/** | |
* 访问数据库的工具类 | |
* 数据库连接池:首先导入jar包commons-dbcp-1.4.jar和commons-pool-1.5.6.jar(可以去apache.org网站上下载) | |
* 对于driver、url、name、pwd等不能写死了,需要建一个配置文件db.properties(new→file),在里面设置配置信息,然后将配置文件读出 | |
* @author MaySnow | |
* | |
*/ | |
public class DBHelp<T> { | |
private static BasicDataSource bs; | |
/** | |
* 静态块原理: | |
* 静态块里的代码有且只能执行一次,第一次创建类的实例或者调用类的方法时执行,以后就不再执行。 | |
* 静态块里只能引用静态,不能引用外部非静态 | |
* 把数据库连接池放到静态块里保证了数据源只被建立一次,防止出现建立多个连接池的混乱 | |
*/ | |
static { | |
//获取源代码包根目录中的文件 | |
InputStream inStream = DBHelp.class.getClassLoader().getResourceAsStream("db.properties"); | |
Properties p = new Properties(); | |
try { | |
p.load(inStream); | |
bs= new BasicDataSource(); | |
bs.setDriverClassName(p.getProperty("driver")); | |
bs.setUrl(p.getProperty("url")); | |
bs.setUsername(p.getProperty("name")); | |
bs.setPassword(p.getProperty("pwd")); | |
bs.setMaxWait(5000);//单位是毫秒 | |
bs.setInitialSize(5); | |
bs.setMaxActive(20); | |
bs.setMinIdle(10); | |
System.out.println("数据源创建成功"); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* 获取数据库连接对象 | |
* @return Connection类的对象 | |
*/ | |
public Connection getConnection() { | |
Connection conn =null; | |
try { | |
conn = bs.getConnection(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
return conn; | |
} | |
public List<T> executeQueryForList(String sql,RowMapper<T> rm,Object...args) { | |
Connection conn = null; | |
PreparedStatement stat = null; | |
ResultSet rs = null; | |
List<T> list = new ArrayList<T>(); | |
try { | |
conn = getConnection(); | |
stat = conn.prepareStatement(sql); | |
for (int i = 0; i < args.length; i++) { | |
stat.setObject(i+1, args[i]); | |
} | |
rs = stat.executeQuery(); | |
while(rs.next()) { | |
T obj = rm.mapRow(rs); | |
list.add(obj); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
close(rs, stat, conn); | |
} | |
return list; | |
} | |
public T executeQueryForObject(String sql,RowMapper<T> rm,Object...args) { | |
Connection conn = null; | |
PreparedStatement stat = null; | |
ResultSet rs = null; | |
T obj = null; | |
try { | |
conn = getConnection(); | |
stat = conn.prepareStatement(sql); | |
for (int i = 0; i < args.length; i++) { | |
stat.setObject(i+1, args[i]); | |
} | |
rs = stat.executeQuery(); | |
if(rs.next()) { | |
obj = rm.mapRow(rs); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
close(rs, stat, conn); | |
} | |
return obj; | |
} | |
/** | |
* 执行insert update delete语句 | |
* @param sql insert or update or delte语句 | |
* @return true代表成功 false代表失败 | |
*/ | |
public boolean executeSQL(String sql,Object... args) { | |
Connection conn = null; | |
PreparedStatement stat = null; | |
try { | |
conn = getConnection(); | |
stat = conn.prepareStatement(sql); | |
//? | |
for (int i = 0; i < args.length; i++) { | |
stat.setObject(i + 1, args[i]); | |
} | |
int rows = stat.executeUpdate(); | |
if(rows > 0) { | |
return true; | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
close(stat,conn); | |
} | |
return false; | |
} | |
/** | |
* 释放数据库资源 | |
* @param rs | |
* @param stat | |
* @param conn | |
*/ | |
public void close(ResultSet rs, Statement stat, Connection conn) { | |
try { | |
if(rs != null) { | |
rs.close(); | |
} | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
if(stat != null) { | |
stat.close(); | |
} | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
if(conn != null) { | |
conn.close(); | |
} | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
/** | |
* 释放数据库资源 | |
* @param stat | |
* @param conn | |
*/ | |
public void close(Statement stat,Connection conn) { | |
close(null,stat,conn); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment