`
qjbagu
  • 浏览: 22855 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论
  • wjl1174: String 不是基本的类型,所有他的变量就是一个对象,放在m ...
    静态字段问题
  • laojiang: 数据库设置缺省值或者类修改成Integer原因null是没办法 ...
    JDBC ORM
  • yin_bp: 补充说明一下连接池的配置     bboss persiste ...
    JDBC 批处理
  • yin_bp: 再贴点稍微有点点技术含量的,呵呵 看看开源j2ee 框架bbo ...
    JDBC 批处理
  • yin_bp: 贴点稍微有点点技术含量的,呵呵 有空看看开源bbossgrou ...
    JDBC 批处理

JDBC ORM

    博客分类:
  • JDBC
阅读更多
相当头疼,郁闷。
SQL> desc person;
名称                                      是否为空? 类型
----------------------------------------- -------- ---------------
ID                                        NOT NULL NUMBER
NAME                                               VARCHAR2(20)
BIRTHDAY                                           DATE
MONEY                                              NUMBER
这是person的表结构。
我想利用JDBC进行ORM测试,User类如下:
import java.sql.Date;

public class User {
	private int id;
	private String name;
	private Date birthday;
	private int money;

	public User() {

	}

	@Override
	public String toString() {
		return "id=" + this.id + " name=" + this.name + " birthday="
				+ this.birthday + " money=" + this.money;
	}


	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 int getMoney() {
		return money;
	}

	public void setMoney(int money) {
		this.money = money;
	}
}

ORMTest表如下:
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;

public class ORMTest {
	public static void main(String[] args) throws Exception {
		User user = (User)getObject("select id,name,birthday,money from person where id = 3",User.class);
		System.out.println(user);
		Bean bean = (Bean)getObject("select id,name,birthday,money from person where id = 3",Bean.class);
		System.out.println(bean);
	}

	static Object getObject(String sql,Class clazz) throws InvocationTargetException,IllegalArgumentException, Exception {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			con = JdbcUtil.getConnection();
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			ResultSetMetaData rsmd = rs.getMetaData();

			int count = rsmd.getColumnCount();
			String[] colNames = new String[count];
			for (int i = 0; i < count; i++) {
				String str = rsmd.getColumnName(i + 1);
				str = str.substring(0, 1) + str.toLowerCase().substring(1);
				colNames[i] = str;
							}
			Object object = null;
			Method[] method = clazz.getMethods();
			if (rs.next()) {
				object = clazz.newInstance();
				for (int i = 1; i <= colNames.length; i++) {
					String colName = colNames[i - 1];
					String methodName = "set" + colName;					
					for (Method m : method) {
						if (methodName.equals(m.getName())) {
							m.invoke(object, rs.getObject(colName));
						}
					}
				}
			}
			return object;
		} finally {
			JdbcUtil.close(rs, ps, con);
		}

	}

}

为什么总是提醒IllegalArgumentException,说是参数类型不匹配。如果我把User类中的int类型的 id和money改成Object类型就可以得到结果,为什么Date和String可以直接转换啊。
1
3
分享到:
评论
1 楼 laojiang 2010-05-17  
数据库设置缺省值


或者类修改成Integer


原因null是没办法赋值给int的

相关推荐

Global site tag (gtag.js) - Google Analytics