原创

Java+sql server+CallableStatement调用存储过程三种情况 (转)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://jeecg.blog.csdn.net/article/details/5973983

在JSP页面中进行测试,代码如下:

<%@ page contentType="text/html;charset=gb2312"%>

<%@ page import="java.sql.*"%>

<% Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver") .newInstance();

String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";

//pubs为你的数据库的

String user = "sa";

String password = "";

Connection conn = DriverManager.getConnection(url, user, password);

//不带参数的存储过程,并且返回结果集

CallableStatement stmt = conn.prepareCall("{call ghy_proc}");

stmt.execute();

ResultSet rs = stmt.getResultSet();

while (rs.next())

{ out.println(rs.getString(1)); }

out.println("
");

//带参数的存储过程,并且返回值

stmt = conn.prepareCall("{call ghy_proc_return(?,?)}");

stmt.registerOutParameter(1, Types.INTEGER);

stmt.registerOutParameter(2, Types.INTEGER);

stmt.setInt(1, 10); stmt.setInt(2, 10);

stmt.execute();

out.println("加1的值是:" + stmt.getString(1) + "
"); out.println("减1的值是:" + stmt.getString(2) + "
");

//带参数的存储过程,并且返回结果集

stmt = conn.prepareCall("{call ghy_proc_var(?)}");

stmt.setInt(1, 14);

stmt.execute();

rs = stmt.getResultSet();

while (rs.next())

{ out.println("job_id value is:" + rs.getString(1) + "
"); out.println("job_desc value is:" + rs.getString(2) + "
");

} %>

三个SQL Server 2000存储过程如下:

CREATE PROCEDURE ghy_proc AS

select * from jobs GO CREATE PROCEDURE ghy_proc_return

(@max int output,@min int output) AS select @max=@max+1 select @min=@min-1 GO CREATE PROCEDURE ghy_proc_var (@id int) AS select * from jobs where job_id=@id GO

文章最后发布于: 2010-10-29 13:19:00
展开阅读全文
0 个人打赏
私信求帮助

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览