/*
使用JDBC程序去调用并执行ORACLE数据库中的存储过程。
*/
import java.sql.*;
public class Test
{
public static void main(String[] args){
Connection conn = null;
CallableStatement callstmt = null;
try{
//1.注册驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.获取连接
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:name","scott","tiger");
//3.获取数据库操作对象(专门执行存储过程的数据库操作对象)
String procedureSql = "{call sumPro(?,?)}";
callstmt = conn.prepareCall(procedureSql);
//给?赋值
callstmt.setInt(1,100);
callstmt.setInt(2,0);
callstmt.registerOutParameter(2,Types.INTEGER);
//4.执行存储过程
callstmt.execute();
//获取存储过程返回的数据
int retValue = callstmt.getInt(2);
System.out.println(retValue);
}catch(Exception e){
e.printStackTrace();
}finally{
//5.释放资源
if(callstmt!=null){
try{
callstmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(conn!=null){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}
使用JDBC调用存储过程
最新推荐文章于 2024-04-09 18:04:35 发布