version3:对bug进行了修复
version2:又对类内部的属性进行了抽取,加入了dbName属性可以方便的更改数据库
version1:这是一个再改进版的JDBCUtil工具类,不仅在原来的基础上又对关闭资源的方法做了改进,将关闭方法进行了分拆,
在成神的路上,我在疯狂的敲代码
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
/**
*
* @ClassName: JDBCUtil
* @Description: JDBC连接数据库工具类
* @author wanglf
* @email wanglf_mail@sina.com
* @version V3.0
*
*/
public class JDBCUtil {
private static Properties p = new Properties();
private static String dbName = null;
private static String drivername = null;
private static String username = null;
private static String url = null;
private static String password = null;
static {
try {
p.load(JDBCUtil.class.getResourceAsStream("/db.properties"));//getResourceAsStream获取配置文件的输入流 ,db.properties位于源文件夹下
//p.load(JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties"));//getClassLoader().getResourceAsStream获取配置文件的输入流
dbName = p.getProperty("database");//获取要连接的数据库名
drivername = p.getProperty(dbName + "drivername");
username = p.getProperty(dbName + "username");
url = p.getProperty(dbName + "url");
password = p.getProperty(dbName + "password");
Class.forName(drivername);
} catch (Exception e) {
System.out.println("类加载和配置文件失败,请尝试检查下配置文件drivername:[" + drivername+"]");
e.printStackTrace();
}
}
//这里利用ThreadLocal类来保证一个线程仅能获得唯一的一个Connection
static ThreadLocal<Connection> t1 = new ThreadLocal<Connection>();
public static Connection getConnection() {
//如果该线程已经获得过连接
if (t1.get() != null) {
return t1.get();//从线程中取出Connection再返还给它
} else {
try {
Connection connection = DriverManager.getConnection(url, username, password);
t1.set(connection);
return connection;
} catch (Exception e) {
System.out.println("获取链接错误,请检查参数[" + url + "][" + username + "][" + password + "]");
e.printStackTrace();
}
}
return null;
}
public static int executeUpdateAndDelete(String sql) {
try {
Connection connection = getConnection();
PreparedStatement pstm = connection.prepareStatement(sql);
return pstm.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* @MethodName: clostResultSet
* @Description:clostResultSet
* @param rs
*/
public static void clostResultSet(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
*
* @MethodName: clostStatement
* @Description:clostStatement
* @param s
*/
public static void clostStatement(Statement s) {
if (s != null) {
try {
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
*
* @MethodName: clostConnection
* @Description:clostConnection
* @param conn
*/
public static void clostConnection(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
*
* @MethodName: close
* @Description:
* @param rs
* @param s
* @param conn
*/
public static void close(ResultSet rs, Statement s, Connection conn) {
clostResultSet(rs);
clostStatement(s);
clostConnection(conn);
}
}
以下是相应的配置文件
##database type
database=mysql
## oracle datebase
oracledrivername=oracle.jdbc.driver.OracleDriver
oracleurl=jdbc:oracle:thin:@localhost:1521:xe
oracleusername=scott
oraclepassword=tiger
##mysql database config
mysqldrivername=com.mysql.jdbc.Driver
mysqlUrl=jdbc:mysql://127.0.0.1:3306/wanglf
mysqlUser=root
mysqlPassword=root