java如何执行ms sql2000的存储过程

vagrant_zy 2005-11-03 04:18:15
java中如何执行ms sql2000的存储过程
...全文
162 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzhzzh204553 2005-11-03
  • 打赏
  • 举报
回复
使用接口CallableStatement接口,它是从Connection的方法得到.
CallableStatement prepareCall(String sql)

The interface used to execute SQL stored procedures. The JDBC API provides a stored procedure SQL escape syntax that allows stored procedures to be called in a standard way for all RDBMSs. This escape syntax has one form that includes a result parameter and one that does not. If used, the result parameter must be registered as an OUT parameter. The other parameters can be used for input, output or both. Parameters are referred to sequentially, by number, with the first parameter being 1.

{?= call <procedure-name>[<arg1>,<arg2>, ...]}
{call <procedure-name>[<arg1>,<arg2>, ...]}

IN parameter values are set using the set methods inherited from PreparedStatement. The type of all OUT parameters must be registered prior to executing the stored procedure; their values are retrieved after execution via the get methods provided here.

A CallableStatement can return one ResultSet object or multiple ResultSet objects. Multiple ResultSet objects are handled using operations inherited from Statement.

For maximum portability, a call's ResultSet objects and update counts should be processed prior to getting the values of output parameters.

柯本 2005-11-03
  • 打赏
  • 举报
回复

我的一个java调用系统过程sp_tables并返回光标的过程,供你参考
------------------------------------------------------
import java.sql.*;

import sun.jdbc.*;

public class sqltest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
Connection conn;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:Driver={SQL Server};Server=vpc98;uid=sa;pwd=;Database=master");
// String sql="";
Statement st=conn.createStatement();
ResultSet rs;
/* sql="select * from test";
rs=st.executeQuery(sql);
while (rs.next())
{
String id=rs.getString("postcode");
System.out.println(id);
}
if (rs!=null) rs.close();
*/
CallableStatement cs = conn.prepareCall("dbo.sp_tables");
rs = null;
int updateCount = -1;
boolean flag = cs.execute();
do{
updateCount = cs.getUpdateCount();
if(updateCount != -1){//说明当前行是一个更新计数
//处理.
cs.getMoreResults();
System.out.println(updateCount);
continue;//已经是更新计数了,处理完成后应该移动到下一行
//不再判断是否是ResultSet
}
rs = cs.getResultSet();
if(rs != null){//如果到了这里,说明updateCount == -1
//处理rs
System.out.println("ok");
while (rs.next())
{
String id=rs.getString("table_name");
System.out.println(id);
}
rs.close();
rs = null;
//cs.getMoreResults();
continue;
//是结果集,处理完成后应该移动到下一行
}
//如果到了这里,说明updateCount == -1 && rs == null,什么也没的了

}while(!(updateCount == -1 && rs == null));

if (st!=null)st.close();
if (conn!=null) conn.close();
}
catch(Exception e){
System.out.println(e.toString());
}

}

}
loulou82 2005-11-03
  • 打赏
  • 举报
回复
<%
String driver = "oracle.jdbc.driver.OracleDriver";
String strUrl = "jdbc:oracle:thin:@192.168.1.6:1521:db";

Class.forName(driver);
Connection conn = DriverManager.getConnection(strUrl, "scott", "tiger");

String procedure = "{call p_test2 (?) }";
CallableStatement cstmt = conn.prepareCall(procedure);
cstmt.setInt(1,33);
cstmt.executeUpdate();
%>

把驱动改为sqlserver即可~

62,629

社区成员

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

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