jdbc调用带参数的存储过程疑问??

仙人长 2010-05-14 04:40:53
//谁能帮我实现调用下面存储过程的方法(主要是给存储过程的参数赋值有些不明白以及调用时sql变量作为实参怎样写)
//方法
public int addOneMessage(Object args[]){//数组按顺序保存了参数
//实现此方法
}
//sql中的存储过程(带参数的)
create proc [dbo].[Insert_chat]
@Name varchar(12) ,
@inceptId int,
@content varchar(200)
as
declare @id int;
select @id=userid from users where username=@Name
insert into chat (sendId, inceptId, content ) values
(@id,@inceptId,@content )

//java代码
// 执行增删改SQL命令操作
public int executeSql(String sql, Object args[]) {
int result = 0;
try {
getConn();//获得连接//此处省略连接的具体实现conn为数据库边接对象
ps=conn.prepareStatement(sql);
//给参数赋值
this.setValue(ps, args);//如果调用存过程这种方式给存储过程赋值是否正确???????
//执行SQL命令
result=ps.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeAll();//此处省略具体实现
}
return result;
}
// 给参数赋会下方法
public void setValue(PreparedStatement ps, Object []args) throws SQLException {
if(args!=null){
for(int i=0;i<args.length;i++){
ps.setObject(i+1, args[i]);
}
}
}
...全文
84 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
商科程序员 2010-05-14
  • 打赏
  • 举报
回复
清单 3. 存储过程返回多个结果集
CallableStatement cstmt = null;

While (cstmt.getMoreResults()) {
ResultSet rs2 = cstmt.getResultSet();
while (rs2.next())
System.out.println(rs2.getString(1) + " " + rs2.getString(2));
rs2.close();
}

商科程序员 2010-05-14
  • 打赏
  • 举报
回复
清单 2. 存储过程返回一个结果集
CallableStatement cstmt = null;

boolean moreResultSets = cstmt.execute();
ResultSet rs1 = cstmt.getResultSet();
while (rs1.next())
System.out.println(rs1.getString(1) + " " + rs1.getString(2));

商科程序员 2010-05-14
  • 打赏
  • 举报
回复
LZ太懒了,我也很懒,就搜一个贴上来吧?
以下是调用使用out参

Connection con = null;
...
// Create a CallableStatement object
CallableStatement cstmt = con.prepareCall("CALL exampleJDBC (?, ?, ?, ?, ?)");
cstmt.setString (1, “BeiJing”); // Set input parameter
cstmt.setInt (2, 2008); // Set input parameter
cstmt.registerOutParameter (3, Types.INTEGER);
cstmt.registerOutParameter (4, Types.INTEGER);
cstmt.registerOutParameter (5, Types.VARCHAR);
cstmt.executeUpdate(); // Call the stored procedure
int goldnumber = cstmt.getInt(3); // Get the output parameter values
int silvernumber = cstmt.getInt(4);
String errorinfo = cstmt.getString(5);
cstmt.close();

81,094

社区成员

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

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