关于object []的使用

smilelhh 2005-01-25 06:56:36
我有个函数如下,它接收一个object[]参数,我就是不知道怎么用?
public ResultSet executeQuery(String sql,Object[] params)
throws SQLException {

PreparedStatement stmt = null;
ResultSet rs = null;

try {
stmt = conn.prepareStatement(sql);
//设置参数
if (params != null) {
for (int i = 0; i < params.length; i++) {
if (params[i] != null) {
stmt.setObject(i + 1, params[i]);
} else {
stmt.setNull(i + 1, Types.OTHER);
}
}
}
//得到结果
rs = stmt.executeQuery();

} catch (SQLException e) {
// this.rethrow(e, sql, params);
}
return rs;//esult;
}


比如我想executeQuery("select a from tab where id=?",..)这最后一个参数怎么传?
...全文
182 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
smilelhh 2005-01-26
  • 打赏
  • 举报
回复
高手呀。出来吧

代码是这样的。注释掉的部分!

ret = new Vector();
ResultSet rs=null;
//Object[] params =null;
//params[0] = (Object) addvcd;
//rs = dbo.executeQuery(this.GET_ADCD_SQL,params);
PreparedStatement st = conn.prepareStatement(this.GET_ADCD_SQL);
//if (-1== addvcd.indexOf("%")) addvcd += "%";
st.setString(1,addvcd);
rs=st.executeQuery();
while (rs.next()){
ret.add(rs.getString("stcd"));
}
st.close();
rs.close();
smilelhh 2005-01-26
  • 打赏
  • 举报
回复
ResultSet rs=null;
Object[] param =null;
param[0] = stcd;
rs = dbo.executeQuery(this.LOAD_FDCD_SQL,param);
smilelhh 2005-01-26
  • 打赏
  • 举报
回复
不行呀?
编译虽然能通过,但是总是取不到数据呀?
sqlink 2005-01-25
  • 打赏
  • 举报
回复
比如
Object[] params=new Object[2];
params[0]=new String("aa");
params[1]=new String("bb");

executeQuery("select a from tab where id=? and name=?",params)
lhj 2005-01-25
  • 打赏
  • 举报
回复
一个参数时:
String[1] myparam;
myparam[0] = "1";
executeQuery("select a from tab where id=?",myparam);

两个参数时:
String[2] myparam;
myparam[0] = "id";
myparam[1] = "name";
executeQuery("select a from tab where id=? and name=?",myparam);

呵呵,好用吗

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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