java调用存储过程

news/2024/5/18 21:49:26 标签: Java, Microsoft, JDBC, SQL, 脚本

创建存储过程的脚本,

使用sqlserver2000 中的pubs 数据库中的 jobs表为例.


create procedure showAll
as
select * from jobs



create procedure obtainJob_desc
@outputParam varchar ( 20 )output,
@id int
as
select @outputParam = job_desc from jobs where job_id = @id


create procedure obtainReturn
as
declare @ret int
select @ret = count ( * ) from jobs
return @ret


declare @ret int
exec @ret = obtainReturn
print @ret

用来获得连接的函数

public ConnectiongetConnection() ... {
Connectioncon
=null;
try...{
Class.forName(
"com.microsoft.jdbc.sqlserver.SQLServerDriver");
con
=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databasename=pubs","sa","");
}
catch(Exceptione)...{
e.printStackTrace();
}

returncon;
}


1,调用得到结果集的存储过程

public void getResultSet() ... {
//获得连接
Connectioncon=this.getConnection();
try...{
//showAll为存储过程名
java.sql.CallableStatementcstm=con.prepareCall("{callshowAll}");
ResultSetrs
=cstm.executeQuery();
while(rs.next())...{
//这里写逻辑代码。
System.out.println(rs.getString(1));
}

rs.close();
con.close();
}
catch(SQLExceptione)...{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}


}

2,调用带有 输入 ,输出 参数的存储过程。

public void getOutParameter( int inParam) ... {
StringoutParam;
Connectioncon
=this.getConnection();

try...{
CallableStatementcstm
=con.prepareCall("{callobtainJob_desc(?,?)}");
cstm.registerOutParameter(
1,Types.VARCHAR);
cstm.setInt(
2,inParam);
cstm.execute();;

//得到输出参数。
StringoutParma=cstm.getString(2);
System.out.println(outParma);
cstm.close();
con.close();

}
catch(SQLExceptione)...{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}


}

3,调用带返回值的存储过程。

public void getReturn() ... {
intret;
Connectioncon
=this.getConnection();
try...{
CallableStatementcstm
=con.prepareCall("{?=callobtainReturn()}");
cstm.registerOutParameter(
1,Types.INTEGER);

cstm.execute();
//得到返回值
ret=cstm.getInt(1);

System.
out.println(ret);
cstm.close();
con.close();

}
catch(SQLExceptione)...{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}


}

http://www.niftyadmin.cn/n/592053.html

相关文章

点点动画~画出懂你的3D魔方

作者&#xff1a;首席填坑官∙苏南 来源&#xff1a;IT平头哥联盟公众号&#xff1a;honeyBadger8 前言 最近在写《点点动画系列》文章&#xff0c;上一期分享了< 手把手教你如何绘制一辆会跑的车 >&#xff0c;本期给大家带来是结合CSS3画出来的一个立体3d魔方&#xff…

wampserver 64 php7.0,win10 wampserver升级 php7.0至 php7.2

1.去官网下载php7.2下载地址&#xff1a;2.下载安装visual c 2017 或 visual c 2015下载地址3.在以前安装的 D:\wamp64\bin\php 目录下新建 php7.2.214. 把下载好的 php-7.2.21-Win32-VC15-x64.zip复制到php7.2.21 并解压到当前目录下5.把以前安装的php7或php5版本 的php.ini…

cocoapods下Swift如何配置DEBUG打印函数

在开发过程中&#xff0c;我们经常需要用到NSLog输出一些信息&#xff0c;但是一个发布的程序&#xff0c;里面带有太多的NSLog输出&#xff0c;肯定对于App性能有所影响&#xff0c;这时候我们可以使用一个宏定义来处理&#xff0c;在开发的时候使用DEBUG模式&#xff0c;在发…

oracle在线迁移,oracle在线迁移同步数据,数据库报错

报需要升级的错误&#xff0c;具体处理步骤如下&#xff1a;一、错误信息SQL> alter database open ;alter database open resetlogs*ERROR at line 1:ORA-01092: ORACLE instance terminated. Disconnection forcedORA-00704: bootstrap process failureORA-39700: databas…

Xcode环境变量 build Settings 设置

[http://blog.csdn.net/zf135792468/article/details/8863547] 一、xcode4中的环境变量 $(BUILT_PRODUCTS_DIR) build成功后的&#xff0c;最终产品路径&#xff0d;&#xff0d;可以在Build Settings参数的Per-configuration Build Products Path项里设置 $(TARGET_NAME) 目标…

使用异步Servlet扩展AJAX应用程序

作为Web应用程序模型的AJAX的出现使服务器端的面貌发生了巨大的变化。用户对着Web页面填写表单并单击提交按钮转到下一个链接的典型Web使用模式现在正在转变为更先进的客户端JavaScript以及功能更丰富的用户界面&#xff0c;只要对表单进行操作&#xff0c;比如单击一个复选框、…

linux 蓝牙发送文件夹,linux下通过蓝牙发送文件到手机上

已经把bluez和openobex移植到开发板上&#xff0c;用以下命令能成功把文件发送到手机&#xff1a;[rootReal /]# usb 1-2.1: new full speed USB device using s3c2410-ohci and address 8idVendor1131idProduct1001[rootReal /]# hciconfig hci0 up[rootReal /]# hcid -f /etc…

idea下gradle构建的spring项目dao层xml不识别问题

from: http://blog.csdn.net/littleSONGJIAN/article/details/73225066 说明&#xff1a;项目是idea、gradle、springboot和mybatis构建的。 问题&#xff1a;点击gradle的classes任务后&#xff0c;在build文件夹的相应dao层下并没有接口对应的mapper.xml文件&#xff0c;如图…