JSP中调用Oracle存储过程的小例子

ZDNet软件频道 时间:2008-06-29 作者: | 中国IT实验室 我要评论()
本文关键词:存储 Oracle JSP
JSP中调用Oracle存储过程的小例子
 *执行一条insert语句并传参数*/
  create or replace procedure p_test2(i in number) as
  begin
  insert into t values (i,'x'||to_char(i));
  commit;
  end;
  /
  
  <%@ page language="java" contentType="text/html;charset=gb2312"%>
 import="java.sql.*"%>
  <html>
  <body>
  <table>
  <tr><td>aaa</td><td>bbb</td></tr>
  <%
  String driver = "Oracle.jdbc.driver.OracleDriver";
  String strUrl = "jdbc:Oracle:thin:@192.168.1.6:1521:db";
  
  Class.forName(driver);
  Connection conn = DriverManager.getConnection(strUrl, "scott", "tiger");
  
  String procedure = "{call p_test2 (?) }";
  CallableStatement cstmt = conn.prepareCall(procedure);
  cstmt.setInt(1,33);
  cstmt.executeUpdate();
  
  Statement stmt = conn.createStatement();
  ResultSet rs = stmt.executeQuery("select * from t");
  
  while(rs.next())
  {
  out.println("<tr><td>" + rs.getString(1) + "</td><td>" + rs.getString(2) + "</td></tr>");
  }
  
  rs.close();
  stmt.close();
  conn.close();
  %>
  </table>
  <p>
  <%
  out.print(conn.isClosed());
  %>
  
  </body>
  </html>
  </body>
  </html>

百度大联盟认证黄金会员Copyright© 1997- CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号 京ICP备09041801号-159
京公网安备:1101082134