JAVA jdbc ResultSet 通過反射機制轉換為實體類Bean
阿新 • • 發佈:2019-02-09
最近專案又有一部分用到jdbc了。。。都快忘光了。。。複習了一下,在這記錄一下。肯定有有問題的地方,歡迎指正。
- 轉實體類工具
/**
* 公司名稱:
* 專案名稱:
* 版本號 : 1.0
* 建立時間:2017/3/24 16:19
* 建立人:leo
**/
package com.shinow.slec.utils;
import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* 類說明:
*ResultSet轉實體類
* @author leo
* @version Version 1.0
* @project SLEC
* @date 2017/3/24.
*/
public class GetBeanFromResultUtil {
private GetBeanFromResultUtil() {
}
public static <T> List<T> getBeans(ResultSet resultSet, Class<T> className) {
List<T> list = new ArrayList<T>();
Field fields[] = className.getDeclaredFields();
try {
while (resultSet.next()) {
T instance = className.newInstance();
for (Field field : fields) {
Object result = resultSet.getObject(field.getName());
boolean flag = field.isAccessible();
field.setAccessible(true );
field.set(instance, result);
field.setAccessible(flag);
}
list.add(instance);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return list;
}
public static <T> T getBean(ResultSet resultSet, Class<T> className) {
T instance = null;
try {
instance = className.newInstance();
Field fields[] = className.getDeclaredFields();
for (Field field : fields) {
Object result = resultSet.getObject(field.getName());
boolean flag = field.isAccessible();
field.setAccessible(true);
field.set(instance, result);
field.setAccessible(flag);
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return instance;
}
}
- jdbc工具
package com.shinow.slec.utils;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtil{
private static Properties prop = null;
private JDBCUtil() {
}
static{
try{
prop = new Properties();
prop.load(new FileReader(JDBCUtils.class.getClassLoader().getResource("jdbc-police.properties").getPath()));
}catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 獲取連線
* @throws ClassNotFoundException
* @throws SQLException
*/
public static Connection getConn() throws ClassNotFoundException, SQLException{
// 1.註冊資料庫驅動
Class.forName(prop.getProperty("jdbc.driver"));
// 2.獲取連線
return DriverManager.getConnection(prop.getProperty("jdbc.url"), prop.getProperty("jdbc.username"), prop.getProperty("jdbc.password"));
}
/**
* 關閉連線
*/
public static void close(ResultSet rs, Statement stat,Connection conn){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
rs = null;
}
}
if(stat!=null){
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
stat = null;
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
conn = null;
}
}
}
}