package _5_37;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.lang.reflect.Method;
public class UserInfo {
private int id;
private String name;
private String pwd;
private int age;
@Override
public String toString() {
return "UserInfo [id=" + id + ", name=" + name + ", pwd=" + pwd + ", age=" + age + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String value) {
this.name = value;
}
public String getPwd() {
return pwd;
}
public void setPwd(String value) {
this.pwd = value;
}
public int getAge() {
return age;
}
public void setAge(int value) {
this.age = value;
}
}
class DBFactory {
public static Connection getDBConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/blogsystem";
String user = "root";
String password = "yourpass";
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
class DBHandle {
public static Object getObject(String className, int Id) {
Object obj = null;
try {
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery(sql);
Method[] methods = c.getMethods();
while (rs.next()) {
obj = c.newInstance();
for (Method method : methods) {
String methodName = method.getName();
if (methodName.startsWith("set")) {
String columnName = methodName.substring(3, methodName.length());
Class<?>[] params = method.getParameterTypes();
if (params[0] == String.class) {
method.invoke(obj, rs.getString(columnName));
} else if (params[0] == int.class) {
method.invoke(obj, rs.getInt(columnName));
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
}
973

被折叠的 条评论
为什么被折叠?



