java oracle 操作日志_JAVA操作ORACLE数据库的存储过程

packagecom.zhwy;import java.sql.*;public classTest {

String driver= "oracle.jdbc.driver.OracleDriver";

String strUrl= "jdbc:oracle:thin:@localhost:1521:orcl";

ResultSet rs= null;

Connection conn= null;

CallableStatement cstmt= null;public static voidmain(String[] args) {new Test().testPageSet(3, 1);

}/*** 没有返回参数的存储过程

*@paraminputeSno

*@paraminputSage*/

public void testNoOutParameterUpdate(int inputeSno, intinputSage) {try{

Class.forName(driver);

conn= DriverManager.getConnection(strUrl, "scott", "scott");

cstmt= conn.prepareCall("{ call scott.testa2(?,?)}");

cstmt.setInt(1, inputeSno);

cstmt.setInt(2, inputSage);

cstmt.execute();

System.out.println("执行成功!");

}catch(SQLException ex) {

ex.printStackTrace();

}catch(Exception ex) {

ex.printStackTrace();

}finally{try{if (cstmt != null)

cstmt.close();if (conn != null) {

conn.close();

conn= null;

}

}catch(SQLException ex) {

ex.printStackTrace();

}

}

}/*** 没有返回参数的存储过程

*@paraminputeSno

*@paraminputSage*/

public void testNoOutParameterInsert(int a, String b, intc) {try{

Class.forName(driver);

conn= DriverManager.getConnection(strUrl, "scott", "scott");

cstmt= conn.prepareCall("{ call scott.testa1(?,?,?)}");

cstmt.setInt(1, a);

cstmt.setString(2, b);

cstmt.setInt(3, c);

cstmt.execute();

}catch(SQLException ex) {

ex.printStackTrace();

}catch(Exception ex) {

ex.printStackTrace();

}finally{try{if (cstmt != null)

cstmt.close();if (conn != null) {

conn.close();

conn= null;

}

}catch(SQLException ex) {

ex.printStackTrace();

}

}

}/*** 有返回参数的存储过程

*@paraminputeSno

*@paraminputSage*/

public void testOutParameter(intinputSno) {try{

Class.forName(driver);

conn= DriverManager.getConnection(strUrl, "scott", "scott");

cstmt= conn.prepareCall("{ call scott.testb(?,?,?)}");

cstmt.setInt(1, inputSno);

cstmt.registerOutParameter(2, Types.VARCHAR);

cstmt.registerOutParameter(3, Types.INTEGER);

cstmt.execute();

String name= cstmt.getString(2);int age = cstmt.getInt(3);

System.out.print("学号是:" + inputSno + "的学生的名字是:" + name + ",年龄是:" +age);

}catch(SQLException ex) {

ex.printStackTrace();

}catch(Exception ex) {

ex.printStackTrace();

}finally{try{if (cstmt != null)

cstmt.close();if (conn != null) {

conn.close();

conn= null;

}

}catch(SQLException ex) {

ex.printStackTrace();

}

}

}/*** 返回列表的存储过程

*@paraminputeSno

*@paraminputSage*/

public voidtestOutResult() {try{

Class.forName(driver);

conn= DriverManager.getConnection(strUrl, "scott", "scott");

cstmt= conn.prepareCall("{ call scott.testc(?)}");

cstmt.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);//指定是oracle里规定的类型

cstmt.execute();

rs= (ResultSet) cstmt.getObject(1);while(rs.next()) {

System.out.print("学号是:" + rs.getInt(1) + "的学生的名字是:" + rs.getString(2) + ",年龄是:" + rs.getInt(3) + "\r\n");

}

}catch(SQLException ex) {

ex.printStackTrace();

}catch(Exception ex) {

ex.printStackTrace();

}finally{try{if (cstmt != null)

cstmt.close();if (conn != null) {

conn.close();

conn= null;

}

}catch(SQLException ex) {

ex.printStackTrace();

}

}

}/*** 分页返回列表的存储过程

*@paraminputeSno

*@paraminputSage*/

public void testPageSet(int recordPerPage, intcurrentPage) {try{

Class.forName(driver);

conn= DriverManager.getConnection(strUrl, "scott", "scott");

cstmt= conn.prepareCall("{ call scott.testd(?,?,?)}");

cstmt.registerOutParameter(3, oracle.jdbc.OracleTypes.CURSOR);//指定是oracle里规定的类型

cstmt.setInt(1, recordPerPage);

cstmt.setInt(2, currentPage);

cstmt.execute();

rs= (ResultSet) cstmt.getObject(3);while(rs.next()) {

System.out.print("学号是:" + rs.getInt(1) + "的学生的名字是:" + rs.getString(2) + ",年龄是:" + rs.getInt(3) + "\r\n");

}

}catch(SQLException ex) {

ex.printStackTrace();

}catch(Exception ex) {

ex.printStackTrace();

}finally{try{if (cstmt != null)

cstmt.close();if (conn != null) {

conn.close();

conn= null;

}

}catch(SQLException ex) {

ex.printStackTrace();

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值