Object强转为实体类类型失败!!!!!!

时间:2024-03-18 22:36:03

我这是运用回调内部类的方法,实现对数据库的查询
废话不多说,先上代码:
Impl:

//用户登录
    @Override
    public User login(String username, String password) throws Exception {
        String sql = "select * from user where username=? and password=? ";
        return (User)jdbcTemplete.query(sql, new ResultSetHandler() {
            @Override
            public Object doHandler(ResultSet rs) throws SQLException {
                User user = null;
                if (rs.next()) {
                    user = new User();
                    user.setUserId(rs.getInt(1));
                    user.setUserName(username);
                    user.setPassWord(password);
                    user.setUserType(rs.getInt(4));
                    user.setCreatedDate(rs.getDate(5));
                    user.setLastmodifyDate(rs.getDate(6));
                }
                return user;
            }
        }, username, password);
    }

jdbcTemplete.query方法:

/**
	 * 查询方法的实现
	 * @param sql
	 * @param handler
	 * @param args
	 * @return                    ResultSetHandler接口     **其实接口也是一个类**
	 */
	public Object query(String sql, ResultSetHandler handler, Object...args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = DBUtils.getConnection();
			//预编译
			ps = conn.prepareStatement(sql);
			//赋值
			if(args != null){
				for (int i = 0; i < args.length; i++) {
					//用setObject是因为object是所有类型的父类
					ps.setObject(i+1, args[i]);
				}
			}
			//执行
			rs = ps.executeQuery();
			Object result = handler.doHandler(rs);
			return  result;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			DBUtils.close(rs, ps, conn);
		}
	}

Object强转为实体类类型失败!!!!!!