static int l;
public <T> List<T> getAll(String sql, T classS, Object... objects)
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
List<T> listNews_detail = null;
l = objects.length;// Object... objects以陣列形式回傳 長度
Object[] params = new Object[l];// 實體化陣列的 長度是L
for (int i = 0; i < objects.length; i++) {
params[i] = objects[i];// 實體化陣列的后賦值
++l;
}
ResultSet rs = QueryDataBase(sql, params);// 呼叫方法
try {
Class<? extends Object> Newclass = classS.getClass();// 反射classS類
Object newInstance = null;
listNews_detail = new ArrayList<>();// 定義的陣列
Class<?> forName = Class.forName(Newclass.getName());// 反射類
Field[] declaredFields = forName.getDeclaredFields();// 反射出classS類里面的屬性以陣列
String columnName = null;
while (rs.next()) {// 指標指向行
newInstance = forName.newInstance();// 呼叫構造 資料型別和資料庫資料型別一樣
for (int j = 0; j < declaredFields.length; j++) {// declaredFields.length屬性數量
// declaredFields[j].getName()陣列形式獲得名稱--在通過forName.getDeclaredField回傳屬性(不是屬性字串)
Field declaredField = forName.getDeclaredField(declaredFields[j].getName());
Class<?> type = (Class<Object>) declaredFields[j].getType();// declaredFields[j].getType()陣列形式獲得型別
String name = declaredFields[j].getName();// declaredFields[j].getName()陣列形式獲得名稱
declaredField.setAccessible(true);// 將可訪問private的方法。
String newName = name.substring(0, 1).toUpperCase() + name.substring(1);// 字串截取首字母并且大寫
Method method = forName.getMethod("set" + newName, type);// forName.getMethod--呼叫反射類中的方法
// --型別是type(轉換的)
// String columnName = rs.getMetaData().getColumnName()//顯示sql陳述句里面*的數量;
int columnCount = rs.getMetaData().getColumnCount();// 欄位長度
if ((j + 1) <= columnCount) {// 控制getColumnLabel(j + 1);括弧里面的長度
columnName = rs.getMetaData().getColumnLabel(j + 1);// getMetaData()獲得表--getColumnLabel(j +
// 1)獲得表列的名字
}
// System.out.println(columnName.equals(name));
// System.out.println(columnName + rs.getMetaData().getColumnCount());
if (type == Integer.class) {// 比較型別是否等于改型別
if (columnName.equals(name)) {// 判斷表列是否存在
method.invoke(newInstance, rs.getInt(name));// 使用呼叫的方法賦值 (類,賦值)
}
} else if (type == Object.class) {
if (columnName.equals(name)) {
method.invoke(newInstance, rs.getObject(name));
}
} else if (type == String.class) {
if (columnName.equals(name)) {
method.invoke(newInstance, rs.getString(name));
}
} else if (type == Double.class) {
if (columnName.equals(name)) {
method.invoke(newInstance, rs.getDouble(name));
}
} else if (type == Date.class) {
if (columnName.equals(name)) {
method.invoke(newInstance, rs.getDate(name));
}
} else if (type == Short.class) {
if (columnName.equals(name)) {
method.invoke(newInstance, rs.getShort(name));
}
}
}
listNews_detail.add((T) newInstance);// 添加集合
}
return listNews_detail;
} catch (SQLException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} finally {
close(rs);
return listNews_detail;
}
}
String sql = "SELECT id,categoryId,summary FROM news_detail WHERE summary=?;";
BestDao dao = new BestDao();
News_detail detail = new News_detail();
List<News_detail> all = dao.getAll(sql, detail, "測驗內容5");//可不填最后一個引數 依據sql陳述句靈活變動
for (Object object : all) {
System.out.println(object);
}
uj5u.com熱心網友回復:
如果你這是給入行新人看的,那你這命名風格應該再規范一些。再者,新人看不懂,老人不想看
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/31082.html
標籤:Java SE
