标签:并且 net 封装 values factory dbutil 自动 pre override
//jdbc提取出来的工具类package utils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import com.mysql.jdbc.Statement;
public class JdbcUtils {
private static final String DRIVERCLASS;
private static final String URL;
private static final String USERNAME;
private static final String PASSWORD;
static{
Properties p = new Properties();
try {
p.load(new FileInputStream("src/jdbc.properties"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
DRIVERCLASS = p.getProperty("driverClass");
URL = p.getProperty("url");
USERNAME = p.getProperty("username");
PASSWORD = p.getProperty("password");
}
/**
@param conn
*/
public static void release(Statement stmt,Connection conn){
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
//链接池和jdbc联合提取出来的工具类
package utils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mysql.jdbc.Statement;
public class JdbcUtils {
/**
* 获取连接,返回连接 是apache的
* @return
*/
public static Connection getDbcpConnection(){
Connection connection = null;
try {
Properties pro = new Properties();
pro.load(new FileInputStream("src\\dbcpconfig.properties"));
DataSource ds = BasicDataSourceFactory.createDataSource(pro);
connection = ds.getConnection();//被增强后的connetion
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
/**
* 获取连接,返回连接 不是apache的
* 配置文件名称是固定的c3p0-config.xml 并且 放在src目录下
* ComboPooledDataSource会自动去src目录查找c3p0-config.xml的配置文件
* @return
*/
public static Connection getC3P0Connection(){
Connection connection = null;
try {
DataSource ds =new ComboPooledDataSource();//这个地方的参数是与配置文件匹配,如果不写,则为默认
connection = ds.getConnection();//被增强后的connetion
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
/**
* 释放资源
* @param stmt
* @param conn被增强后的
*/
public static void release(Statement stmt,Connection conn){
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
// 已经变成了归还了...
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 释放资源
* @param stmt
* @param conn被增强后的
*/
public static void release(ResultSet rs,Statement stmt,Connection conn){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
// 把close()给修改了,原来是销毁连接,现在让方法变成归还连接。
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
//DButis的使用 导入包
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.junit.Test;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
*/
public class test {
DataSource ds = new ComboPooledDataSource();br/>@Test
public void runInsert(){
/**
*/
class MyResultSetHandler implements ResultSetHandler<Product>{br/>@Override
public Product handle(ResultSet arg0) throws SQLException {
Product p = new Product();
p.setPid(arg0.getInt(1));
p.setPname(arg0.getString(2));
p.setPrice(arg0.getDouble(3));
p.setPdate(arg0.getDate(4));
return p;
}
}
标签:并且 net 封装 values factory dbutil 自动 pre override
原文地址:http://blog.51cto.com/13579086/2065151