想用反射从数据库里查询出数据,并把他们用set方法设进对象中,在调用set的方法,会抛出java.lang.IllegalArgumentException: argument type mismatch错误,出错代码如下:- for (Method m : ms) {
- if (methodName.equals(m.getName())) {
- //System.out.println(m.getName());
- //System.out.println(rs.getObject(colName));
- m.invoke(obj, new Object[] {rs.getObject(colName)});
- }
- }
- JdbcUtils.java:
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class JdbcUtils {
- private static String url = "jdbc:mysql://localhost:3306/userinfo";
- private static String username = "root";
- private static String password = "root";
-
- private JdbcUtils(){}
-
- static{
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static Connection getConnection() throws SQLException{
-
- return DriverManager.getConnection(url,username,password);
-
- }
-
- public static void free(ResultSet rs, Statement smt,Connection conn){
- if(rs != null)
- try {
- rs.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- if(smt != null)
- try {
- smt.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- if(conn != null)
- try {
- conn.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- }
- ORMTest.java:
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- public class ORMTest {
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- User user = (User) getObject(
- "select id as Id, name as Name, birthday as Birthday, salary as Salary from userinfo where id=1",
- User.class);
- System.out.println(user.getName());
- }
- public static Object getObject(String sql, Class clazz) throws Exception {
- Connection conn = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- try {
- conn = JdbcUtils.getConnection();
- ps = conn.prepareStatement(sql);
- rs = ps.executeQuery();
- String[] colNames = getColNames(rs);
- Object obj = null;
- Method[] ms = clazz.getMethods();
- if (rs.next()) {
- obj = clazz.newInstance();
- for (int i = 0; i < colNames.length; i++) {
- String colName = colNames[i];
- String methodName = "set" + colName;
- System.out.println(methodName);
- System.out.println(rs.getObject(colName));
- Object argument = rs.getObject(colName);
- for (Method m : ms) {
- if (methodName.equals(m.getName())) {
- //System.out.println(m.getName());
- //System.out.println(rs.getObject(colName));
- m.invoke(obj, new Object[] {rs.getObject(colName)});
- }
- }
- }
- }
- return obj;
- } finally {
- JdbcUtils.free(rs, ps, conn);
- }
- }
- private static String[] getColNames(ResultSet rs) throws Exception {
- 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);
- }
- return colNames;
- }
- static Object getObject1(String sql, Class clazz) throws SQLException,
- Exception, IllegalAccessException, InvocationTargetException {
- Connection conn = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- try {
- conn = JdbcUtils.getConnection();
- ps = conn.prepareStatement(sql);
- rs = ps.executeQuery();
- String[] colNames = getColNames(rs);
- Object object = null;
- Method[] ms = clazz.getMethods();
- if (rs.next()) {
- object = clazz.newInstance();
- for (int i = 0; i < colNames.length; i++) {
- String colName = colNames[i];
- String methodName = "set" + colName;
- // Object value = rs.getObject(colName);
- // try {
- // Method m = clazz
- // .getMethod(methodName, value.getClass());
- // if (m != null)
- // m.invoke(object, value);
- // } catch (NoSuchMethodException e) {
- // e.printStackTrace();
- // //
- // }
- for (Method m : ms) {
- if (methodName.equals(m.getName())) {
- m.invoke(object, rs.getObject(colName));
- break;
- }
- }
- }
- }
- return object;
- } finally {
- JdbcUtils.free(rs, ps, conn);
- }
- }
- }
- User.java:
- import java.util.Date;
- public class User {
- private int id;
- private String name;
- private Date birthday;
- private float salary;
-
- public User(){}
-
- public User(String name){
- this.name = name;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public float getSalary() {
- return salary;
- }
- public void setSalary(float salary) {
- this.salary = salary;
- }
-
-
- }
复制代码 |
|