求助 数据类型.

eacc31 2018-06-01 01:16:55
rh有问题.
The method query(String, RsHandler<capture#2-of ?>, Object...) in the type QR<capture#2-of ?> is not applicable for the arguments (String, RsHandler<Stu>, Object[])


	public Stu load(String id) {
QR<?> qr = new QR<>(C3P0.getDataSource());
String sql = "select * from stu where id=?";
Object[] params = { id };

RsHandler<Stu> rh = new RsHandler<Stu>() {
public Stu handler(ResultSet result) {
if (!result.next())
return null;
Stu stu = new Stu();
stu.setId(result.getString("id"));
stu.setName(result.getString("name"));
stu.setSex(result.getString("sex"));
return stu;
}

};

return (Stu) qr.query(sql, rh, params);
}



定义的接口
public interface RsHandler<T> {
public T handler(ResultSet result) throws RuntimeException;

}


query 方法
	public T query(String sql, RsHandler<T> rh, Object... params) {
Connection con = null;
PreparedStatement psmt = null;
ResultSet result = null;
try {
con = dataSource.getConnection();// 通过连接池得到链接对象
psmt = con.prepareStatement(sql);// 使用sql创建psmt

initParams(psmt, params);// 设置参数

result = psmt.executeQuery();// 执行

return rh.handler(result);

} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (psmt != null)
psmt.close();
if (con != null)
con.close();
} catch (SQLException e) {
}
}

}
...全文
1031 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Coder_D 2018-06-01
  • 打赏
  • 举报
回复
params是个可变长参数,传多个的话,直接逗号分开就好了,内部会处理成数组,不需要你直接传数组进去

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧