更新時間:2022-12-01 12:14:51 來源:動力節點 瀏覽1253次
完成SQL查詢 并將查詢結果放入Vector容器,以便其他程序使用
/*
* 執行sql查詢語句
*/
public static Vector executeQuery(Class clazz, String sql, Object... args) {
Connection conn = null;
PreparedStatement preparedstatement = null;
ResultSet rs = null;
Vector vecRs = new Vector();
T obj = null;
try {
conn = JDBCTools.getConnection();
preparedstatement = conn.prepareStatement(sql);
// 通過sql語句來判斷選擇了那些列
for (int i = 0; i < args.length; i++) {
preparedstatement.setObject(i + 1, args[i]);
}
// 利用sql查詢獲取結果集
// 利用反射創建實體類的對象
// 獲取結果街的別名Stud_id 獲取JDBC的元數據
// 獲取結果集每一列的值,結合上一步得到一個Map鍵值對
// 鍵:列的別名 值:列的值
// 在利用反射對實體類對象的屬性賦值
// 屬性為Map的鍵 值為Map的值
rs = preparedstatement.executeQuery();
// 獲取元數據
ResultSetMetaData rsmd = rs.getMetaData();
Map mapMetaData = new HashMap();
// 打印一列的列名
while (rs.next()) {
//獲取數據表中滿足要求的一行數據,并放入Map中
for (int i = 0; i < rsmd.getColumnCount(); i++) {
String columnLabel = rsmd.getColumnLabel(i + 1);
Object columnValue = rs.getObject(columnLabel);
// System.out.println(columnLabel);
mapMetaData.put(columnLabel, columnValue);
}
//將Map中的數據通過反射初始化T類型對象
if (mapMetaData.size() > 0) {
obj = clazz.newInstance();
for (Map.Entry entry : mapMetaData.entrySet()) {
String fieldkey = entry.getKey();
Object fieldvalue = entry.getValue();
// System.out.println(fieldkey + ":" + fieldvalue);
ReflectionUtils.setFieldValue(obj, fieldkey, fieldvalue);
//通過反射賦值
}
}
//將對象裝入Vector容器
vecRs.add(obj);
}
}
catch (Exception e) {
e.printStackTrace();
}
return vecRs;
}
其中使用到的工具類方法
獲取數據庫連接JDBCTools.getConnection()
/*
* 獲取數據庫的連接
*/
public static Connection getConnection() throws Exception {
Connection conn = null;
String driver = null;
String jdbcUrl = null;
String username = null;
String password = null;
// 獲取Properties對象
Properties properties = new Properties();
InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");
properties.load(in);
driver = properties.getProperty("driver");
jdbcUrl = properties.getProperty("jdbcUrl");
username = properties.getProperty("user");
password = properties.getProperty("password");
Class.forName(driver);
conn = DriverManager.getConnection(jdbcUrl, username, password);
return conn;
}
ReflectionUtils.setFieldValue(obj,fieldkey,fieldvalue);
將obj對象的fieldkey屬性賦值為fieldvalue
//設置對象的屬性
public static void setFieldValue(Object obj,String fieldName,Object value){
Field field=getDeclaredField(obj, fieldName);
if(field==null){
throw new IllegalArgumentException("Could not find field["+
fieldName+"] on target ["+obj+"]");
}
makeAccessiable(field);
try{
field.set(obj, value);
}
catch(IllegalAccessException e){
System.out.println("不可能拋出的異常");
}
}
//判斷field的修飾符是否是public,并據此改變field的訪問權限
public static void makeAccessiable(Field field){
if(!Modifier.isPublic(field.getModifiers())){
field.setAccessible(true);
}
}
//獲取field屬性,屬性有可能在父類中繼承
public static Field getDeclaredField(Object obj,String fieldName){
for (Class> clazz=obj.getClass(); clazz!=Object.class; clazz=clazz.getSuperclass()){
try{
return clazz.getDeclaredField(fieldName);
}
catch(Exception e){
}
}
return null;
}
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習