java從資料庫獲取單個物件或物件集合的簡單工具類
阿新 • • 發佈:2019-02-12
開發中在不用資料庫的情況下操作資料庫,比如說對資料庫的增刪改查,每次都重新擼一遍重複的程式碼,那樣就會很麻煩,這裡就給安利一個很簡單的工具類來對資料庫進行讀寫操作,當然並不能涵蓋所有的功能,說了,只是很簡單的操作資料庫,當然你可以直接使用框架來操作,那樣更好。。。
話不多說,直接上程式碼。。。
public class DBOperate<T> {
private static Connection connection = null;
private static PreparedStatement statement = null;
private static ResultSet query = null;
private static ResultSetMetaData metaData = null;
/**
* 增刪改 操作
* @param sql
* @param params
* @return
*/
public static int operateDB(String sql,Object...params){
int value = -1;
try {
connection = DBManager.getConnection();
connection.setAutoCommit(false );
statement = connection.prepareStatement(sql);
for(int i = 0;i<params.length;i++){
statement.setObject(i+1, params[i]);
}
System.out.println(sql);
value = statement.executeUpdate();
connection.commit();
} catch (SQLException e) {
e.printStackTrace();
if(connection!=null){
try {
connection.rollback();
connection.setAutoCommit(true);
} catch (SQLException e1) {
e1.printStackTrace();
}
}
} finally {
DBManager.closeAll(statement,connection);
}
return value;
}
/**
* 獲取集合
* @param sql
* @param clazz
* @param params
* @return
*/
public List<T> getList(String sql,Class<T> clazz,Object...params){
List<T> list = new ArrayList<>();
try {
connection = DBManager.getConnection();
statement = connection.prepareStatement(sql);
for(int i = 0;i<params.length;i++){
statement.setObject(i+1, params[i]);
}
query = statement.executeQuery();
metaData = query.getMetaData();
int column = metaData.getColumnCount();
while(query.next()){
T t = clazz.newInstance();
for(int i = 1;i<=column;i++){
String name = metaData.getColumnName(i);
Object value = query.getObject(i);
Field field = clazz.getDeclaredField(name);
field.setAccessible(true);
field.set(t, value);
}
list.add(t);
}
} catch (SQLException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} finally {
DBManager.closeAll(query,statement,connection);
}
return list;
}
/**
* 獲取物件
* @param sql
* @param clazz
* @param params
* @return
*/
public T getInstance(String sql,Class<T> clazz,Object...params){
try {
connection = DBManager.getConnection();
statement = connection.prepareStatement(sql);
for(int i = 0;i<params.length;i++){
statement.setObject(i+1,params[i]);
}
query = statement.executeQuery();
metaData = query.getMetaData();
int column = metaData.getColumnCount();
if(query.next()){
T t = clazz.newInstance();
for(int i = 1;i<=column;i++){
String name = metaData.getColumnName(i);
Object value = query.getObject(i);
Field field = clazz.getDeclaredField(name);
field.setAccessible(true);
field.set(t, value);
}
return t;
}
} catch (SQLException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}finally {
DBManager.closeAll(statement,connection);
}
return null;
}
}
這裡面會有DBManager類,主要就是連線資料庫和關閉的,可以自行補全。
很簡單,簡單使用反射的方法來給每一個屬性賦值,然後返回就行。。。
當我們要使用的時候就可以這樣:
public class StudentDaoImpl extends DBOperator<Student> {
public List<Student> getList() {
String sql = "select * from student";
return super.getList(sql, Student.class);
}
public Student getStudentById(int id) {
String sql = "select * from student where id=?";
return super.getInstance(sql, GoodsInfo.class, id);
}
}
Over。
寫的不好,歡迎指正,不喜勿噴!!!