如何在JDBC里调用PLSQL创建的存储过程?
发布网友
发布时间:2022-04-08 02:18
我来回答
共2个回答
懂视网
时间:2022-04-08 06:39
在JDBC简单封装的基础上实现
public class UserDao{
public static void testGetUser(String userName) throws Exception{
Connection conn=null;
CallableStatement callableStatement=null;
ResultSet resultSet=null;
try{
conn=JdbcUtils.getConnetcion();
String sql="{call addUser(?,?,?,?)}";//调用数据库addUser存储过程,有四个参数,最后一个参数是输出新数据的ID
callableStatement=conn.prepareCall(sql);
callableStatement.setString(1,"test");
callableStatement.setDate(2,new java.sql.Date(System.currentTimeMillis()));
callableStatement.setFloat(3,100f);
callableStatement.registerOutParameter(4,Types.INTEGER);
callableStatement.excuteUpdate();
int id=callableStatement.getInt(4);//获得存储过程的输出参数值
}finally{
JdbcUtils.freeResource(resultSet,preparedStatement,conn);
}
}
}
初学JDBC,调用存储过程
标签:
热心网友
时间:2022-04-08 03:47
import oracle.jdbc.*;
import oracle.sql.*;
import java.sql.*;
public class ReturnIndexTable
{
Connection ociconn=null;
OracleCallableStatement stmt =null;
public String[] getTable(String in_param)
{
String[] reAry=null;
try
{
OracleDriver S_Driver=null;
if(S_Driver==null)
S_Driver=new oracle.jdbc.OracleDriver();
DriverManager.registerDriver(S_Driver);
String url="jdbc:oracle:oci8:@test";
String user="user";
String password=" password";
ociconn= DriverManager.getConnection(url,user,password);
stmt =(OracleCallableStatement)ociconn.prepareCall("begin yejf_count(?,?); end;");
// 返回的索引表最大长度(可以大于索引表实际长度)
int maxLen =31;
// 索引表元素类型
int elemSqlType = OracleTypes.VARCHAR;
//索引表元素长度(CHAR, VARCHAR or RAW),其它元素类型可忽略该项值,但该参数仍须定义
int elemMaxLen=50;
stmt.setString(1,in_param);
// 注册返回参数
stmt.registerIndexTableOutParameter(2,maxLen,elemSqlType,elemMaxLen);
stmt.execute();
// 返回数组类型
reAry=(String[])stmt.getPlsqlIndexTable(2);
}
catch (Exception e)
{e.printStackTrace();
}
finally
{return reAry;
}
}
//关闭连接.............
}