JDBC操作封裝實例
JDBCUtile.properties
drv=com.mysql.jdbc.Driver
url=jdbc:mysql:///myHomePage
uid=root
pwd=root
JDBCUtile
/**
* JDBC工具類
* @author leex
*
*/
public class JDBCUtils {
//創建Properties文件對象
private static Properties prop = new Properties();
//創建ThreadLocal線程鎖
private static ThreadLocal<Connection> treadLocal = new ThreadLocal<Connection>();
static {
//TWR語法創建Properties文件輸入流
//JDBCUtils.class.getClassLoader().getResourceAsStream("JDBC.properties")通過類加載器獲取文件完全路徑
try (InputStream in = JDBCUtils.class.getClassLoader().getResourceAsStream("JDBC.properties")){
//加載Properties文件
prop.load(in);
//加載數據庫驅動
Class.forName(prop.getProperty("drv"));
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 獲取Connection對象的方法
* @return Connection對象
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
//獲取與此線程綁定的Connection
Connection conn = treadLocal.get();
//如果不為空返回
if(null != conn) {
return conn;
}
//如果為空創建新的
conn = DriverManager.getConnection(
prop.getProperty("url"), prop.getProperty("uid"), prop.getProperty("pwd"));
//把創建的線程綁定到線程
treadLocal.set(conn);
return conn;
}
/**
* 關閉Connection
* @throws SQLException
*/
public static void close() throws SQLException {
Connection conn = treadLocal.get();
if(null != conn) {
conn.close();
//從線程綁定中移除
treadLocal.remove();
}
}
}
BaseDao
/**
* 封裝的JDBC基礎操作類
* @author leex
*
*/
public class BaseDao {
/**
* 更新方法
* @param sql sql語句
* @param paramers 占位符的值
* @return
* @throws SQLException
*/
public int update(String sql ,Object...paramers) throws SQLException {
//通過JDBCUtils獲取Connection連接
Connection conn = JDBCUtils.getConnection();
//創建PreparedStatement對象
PreparedStatement statm = conn.prepareStatement(sql);
//創建PreparedStatement元數據
ParameterMetaData parameterMetaData = statm.getParameterMetaData();
//通過PreparedStatement元數據獲取數據庫中的字段數
int count = parameterMetaData.getParameterCount();
//判斷輸入的值數量是否正確(提高程序健壯性)
if(null != paramers) {
if(count != paramers.length) {
throw new RuntimeException("傳入的參數有錯誤....");
}
}
//給sql語句中的占位符賦值
for (int i = 0; i < paramers.length; i++) {
statm.setObject(i + 1, paramers[i]);
}
//更新
int executeUpdate = statm.executeUpdate();
//關閉connection
JDBCUtils.close();
//返回收影響的行數
return executeUpdate;
}
/**
* 通用查詢方法
* @param sql sql語句
* @param clazz 返回的封裝類
* @param paramers sql占位符值
* @return
* @throws SQLException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public <T> List<T> queryList(String sql, Class clazz, Object... paramers ) throws SQLException, InstantiationException, IllegalAccessException, InvocationTargetException {
List<T> list = new ArrayList<>();
//通過JDBCUtils獲取Connection連接
Connection conn = JDBCUtils.getConnection();
//創建PreparedStatement對象
PreparedStatement statm = conn.prepareStatement(sql);
//創建PreparedStatement元數據
ParameterMetaData parameterMetaData = statm.getParameterMetaData();
//通過PreparedStatement元數據獲取數據庫中的字段數
int count = parameterMetaData.getParameterCount();
//判斷輸入的值數量是否正確(提高程序健壯性)
if(null != paramers) {
if(count != paramers.length) {
throw new RuntimeException("傳入的參數有錯誤....");
}
}
if(0 != paramers.length)
for (int i = 0; i < paramers.length; i++) {
statm.setObject(i + 1, paramers[i]);
}
//獲取結果集
ResultSet result = statm.executeQuery();
//創建結果集元數據
ResultSetMetaData metaData = result.getMetaData();
//獲取結果集的列數
int columnCount = metaData.getColumnCount();
//遍歷結果集
while(result.next()) {
//創建對象
T t = (T) clazz.newInstance();
//給對象賦值
for (int i = 0; i < columnCount; i++) {
//獲取結果集的列名(有別名則就是別名)
String columnName = metaData.getColumnLabel(i+1);
//獲取值
Object value = result.getObject(columnName);
//通過BeanUtils匹配賦值
BeanUtils.copyProperty(t, columnName, value);
}
//加入列表
list.add(t);
}
//關閉connection
JDBCUtils.close();
//返回list
return list;
}
}