public class JdbcUtil {
public static Connection getConnection() throws Exception {
Class.forName("oracle.jdbc.OracleDriver");
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "test", "oracle");
return connection;
}
}
建立连接
Connection connection = JdbcUtil.getConnection();
准备sql语句,?为存储过程的入参(也可有出参)
strSql="{call proc_GetJgOccupationRatio(?,?,?,?)}";
CallableStatement callableStatement = connection.prepareCall(strSql);
设置参数
......
callableStatement.setObject(3,"0");
......
4号参数为返回多对象集合的游标
callableStatement.registerOutParameter(4, OracleTypes.CURSOR);
执行sql语句
callableStatement.execute();
获取存储过程中保存在游标中的结果集
ResultSet rs = ((OracleCallableStatement) callableStatement).getCursor(4);
根据游标表中的字段名称获取对应值
while (rs.next()){
AnalysisData data = new AnalysisData();
data.setName(rs.getString("OrgName"));
// data.setValue(rs.getInt("CurrOccupationRatio"));
}
关闭连接
rs.close();
callableStatement.close();
connection.close();
注:只提供大致思路步骤,未能跑通;