Sample Code Analysis for calling the Oracle stored procedure using Java

Source: Internet
Author: User

OracleIn the databaseStored ProcedureCan be usedJavaJava calls Oracle stored procedures are divided into calling stored procedures without return values and stored procedures with returned values. This article will introduce this part. Next let's take a look at it.

1. Stored Procedure without return values

The stored procedure is:

 
 
  1. CREATE OR REPLACE PROCEDURE TESTA(PARA1 IN VARCHAR2,PARA2 IN VARCHAR2) AS  
  2.  
  3. BEGIN  
  4.  
  5. INSERT INTO HYQ.B_ID (I_ID,I_NAME) VALUES (PARA1, PARA2);  
  6.  
  7. END TESTA; 

Then, the following code is used for calling in java:

 
 
  1. package com.hyq.src;  
  2.  
  3. import java.sql.*;  
  4.  
  5. import java.sql.ResultSet;  
  6.  
  7. public class TestProcedureOne {  
  8.  
  9. public TestProcedureOne() {  
  10.  
  11. }  
  12.  
  13. public static void main(String[] args ){  
  14.  
  15. String driver = "oracle.jdbc.driver.OracleDriver";  
  16.  
  17. String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521: hyq ";  
  18.  
  19. Statement stmt = null;  
  20.  
  21. ResultSet rs = null;  
  22.  
  23. Connection conn = null;  
  24.  
  25. CallableStatement cstmt = null;  
  26.  
  27. try {  
  28.  
  29. Class.forName(driver);  
  30.  
  31. conn = DriverManager.getConnection(strUrl, " hyq ", " hyq ");  
  32.  
  33. CallableStatement proc = null;  
  34.  
  35. proc = conn.prepareCall("{ call HYQ.TESTA(?,?) }");  
  36.  
  37. proc.setString(1, "100");  
  38.  
  39. proc.setString(2, "TestOne");  
  40.  
  41. proc.execute();  
  42.  
  43. }  
  44.  
  45. catch (SQLException ex2) {  
  46.  
  47. ex2.printStackTrace();  
  48.  
  49. }  
  50.  
  51. catch (Exception ex2) {  
  52.  
  53. ex2.printStackTrace();  
  54.  
  55. }  
  56.  
  57. finally{  
  58.  
  59. try {  
  60.  
  61. if(rs != null){  
  62.  
  63. rs.close();  
  64.  
  65. if(stmt!=null){  
  66.  
  67. stmt.close();  
  68.  
  69. }  
  70.  
  71. if(conn!=null){  
  72.  
  73. conn.close();  
  74.  
  75. }  
  76.  
  77. }  
  78.  
  79. }  
  80.  
  81. catch (SQLException ex1) {  
  82.  
  83. }  
  84.  
  85. }  
  86.  

Of course, we need to create a table named TESTTB, which contains two fields: I _ID and I _NAME ).

2. Stored Procedures with returned values are not listed)

The stored procedure is:

 
 
  1. CREATE OR REPLACE PROCEDURE TESTB(PARA1 IN VARCHAR2,PARA2 OUT VARCHAR2) AS  
  2.  
  3. BEGIN  
  4.  
  5. SELECT INTO PARA2 FROM TESTTB WHERE I_ID= PARA1;  
  6.  
  7. END TESTB; 

Use the following code when calling in java:

 
 
  1. package com.hyq.src;  
  2.  
  3. public class TestProcedureTWO {  
  4.  
  5. public TestProcedureTWO() {  
  6.  
  7. }  
  8.  
  9. public static void main(String[] args ){  
  10.  
  11. String driver = "oracle.jdbc.driver.OracleDriver";  
  12.  
  13. String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521:hyq";  
  14.  
  15. Statement stmt = null;  
  16.  
  17. ResultSet rs = null;  
  18.  
  19. Connection conn = null;  
  20.  
  21. try {  
  22.  
  23. Class.forName(driver);  
  24.  
  25. conn = DriverManager.getConnection(strUrl, " hyq ", " hyq ");  
  26.  
  27. CallableStatement proc = null;  
  28.  
  29. proc = conn.prepareCall("{ call HYQ.TESTB(?,?) }");  
  30.  
  31. proc.setString(1, "100");  
  32.  
  33. proc.registerOutParameter(2, Types.VARCHAR);  
  34.  
  35. proc.execute();  
  36.  
  37. String testPrint = proc.getString(2);  
  38.  
  39. System.out.println("=testPrint=is="+testPrint);  
  40.  
  41. }  
  42.  
  43. catch (SQLException ex2) {  
  44.  
  45. ex2.printStackTrace();  
  46.  
  47. }  
  48.  
  49. catch (Exception ex2) {  
  50.  
  51. ex2.printStackTrace();  
  52.  
  53. }  
  54.  
  55. finally{  
  56.  
  57. try {  
  58.  
  59. if(rs != null){  
  60.  
  61. rs.close();  
  62.  
  63. if(stmt!=null){  
  64.  
  65. stmt.close();  
  66.  
  67. }  
  68.  
  69. if(conn!=null){  
  70.  
  71. conn.close();  
  72.  
  73. }  
  74.  
  75. }  
  76.  
  77. }  
  78.  
  79. catch (SQLException ex1) {  
  80.  
  81. }  
  82.  
  83. }  
  84.  

Note:Proc. the value 2 in getString (2) is not arbitrary, but corresponds to the out column in the stored procedure. If the out column is in the first position, it is proc. getString (1). If it is the third position, it is proc. getString (3), of course, you can also have multiple return values at the same time, that is, add a few more out parameters.

The example of calling the stored procedure of Oracle Database in Java is described here. I hope this introduction will help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.