자바 반사 Jdbc 조회 결 과 를 대상 으로 패키지
1489 단어 자바 반사
public class ORMTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public static Object getUser(String sql, Class clazz) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Object obj = null;
try {
conn = JdbcUtils.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
String[] colNames = new String[count];
for (int i=1; i<=count; i++) {
colNames[i-1] = rsmd.getColumnLabel(i);
}
Method[] ms = obj.getClass().getMethods();
if (rs.next()) {
obj = clazz.newInstance();
for (int i=0; i<colNames.length; i++) {
String colName = colNames[i];
String methodName = "set" + colName;
// Method method = user.getClass().getMethod(methodName, String.class);
// method.invoke(user, rs.getObject(colName));
for (Method m : ms) {
if (m.getName().equals(methodName)) {
m.invoke(obj, rs.getObject(colName));
}
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return obj;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
자바 리 플 렉 션 관련 및 예시자바 리 플 렉 션 과 관련 된 것 은 네 개의 final 유형의 클래스 클래스 클래스, Method, Field, Constructor 이다.자바 의 모든 종류의 부모 클래스 Object 입 니 다.그 중에서 Cl...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.