Introduction to methods and instances for calling stored procedures in DB2 Databases

Source: Internet
Author: User

The last time we introduced the implementation process of creating a trigger for a DB2 database, this article will introduceDB2 databasePairStored ProcedureNext, let's take a look at this part.

I. Stored Procedure calls are divided into three parts

1. Connection (establish a connection with the database)

 
 
  1. Class.forName("COM.ibm.db2.jdbc.net.DB2Driver").newInstance();  
  2.  
  3. Connection con=DriverManager.getConnection(url,user,password); 

2. Register output parameters

 
 
  1. cs.registerOutParameter (3, Types.INTEGER); 

3. Call the stored procedure:

 
 
  1. CallableStatement cs = con. prepareCall ("{call store_name (parameter, parameter, parameter )}");

Ii. Call example:

 
 
  1. Import java.net. URL;
  2.  
  3. Import java. SQL .*;
  4.  
  5. Class test2
  6.  
  7. {
  8.  
  9. Public static void main (String args [])
  10.  
  11. {
  12.  
  13. String url = "jdbc: db2: // wellhope/sample ";
  14.  
  15. String user = "db2admin ";
  16.  
  17. String password = "db2admin ";
  18.  
  19. Try
  20.  
  21. {
  22.  
  23. Class. forName ("COM.ibm.db2.jdbc.net. DB2Driver"). newInstance ();
  24.  
  25. // Establish a connection with the database
  26.  
  27. Connection con = DriverManager. getConnection (url, user, password );
  28.  
  29. CheckForWarning (con. getWarnings ());
  30.  
  31. DatabaseMetaData dma = con. getMetaData ();
  32.  
  33. String str = "This is a string ";
  34.  
  35. // Int hashcode = str. hashCode ();
  36.  
  37. // System. out. println ("Hashcode" + hashcode );
  38.  
  39. // Create a Statement object for executing SQL statements
  40.  
  41. Statement stmt = con. createStatement ();
  42.  
  43. // Create a CallableStatement object for executing the Stored Procedure
  44.  
  45. CallableStatement cs = con. prepareCall ("{call PRO_YHDL1 (?,?,?)} ");
  46.  
  47. // Register output parameters
  48.  
  49. Cs. registerOutParameter (3, Types. INTEGER );
  50.  
  51. Int result = 0;
  52.  
  53. Cs. setString (1, "123 ");
  54.  
  55. Cs. setString (2, "123 ");
  56.  
  57. Cs.exe cute ();
  58.  
  59. Result = cs. getInt (3 );
  60.  
  61. DispResultSet (result );
  62.  
  63. Cs. close ();
  64.  
  65. Con. close ();
  66.  
  67. }
  68.  
  69. Catch (SQLException ex)
  70.  
  71. {
  72.  
  73. System. out. println ("*** SQLException caught ***");
  74.  
  75. While (ex! = Null)
  76.  
  77. {
  78.  
  79. System. out. println ("SQLState:" + ex. getSQLState ());
  80.  
  81. System. out. println ("Message:" + ex. getMessage ());
  82.  
  83. System. out. println ("Vendor:" + ex. getErrorCode ());
  84.  
  85. Exex = ex. getNextException ();
  86.  
  87. System. out. println ("");
  88.  
  89. }
  90.  
  91. }
  92.  
  93. Catch (java. lang. Exception ex)
  94.  
  95. {
  96.  
  97. Ex. printStackTrace ();
  98.  
  99. }
  100.  
  101. }

Iii. Example of stored procedure:

Pro_yhdl1 is a stored procedure. Its function is to retrieve PWD from the database table YHDL:

 
 
  1. import java.sql.*;                    
  2.  
  3. public class Pro_yhdl1  
  4.  
  5. {  
  6.  
  7. public static void pro_yhdl1 ( String m_id,  
  8.  
  9. String m_pwd,  
  10.  
  11. int[] result ) throws SQLException, Exception  
  12.  
  13. {  
  14.  
  15. // Get connection to the database  
  16.  
  17. Connection con = DriverManager.getConnection("jdbc:default:connection");  
  18.  
  19. PreparedStatement stmt = null;  
  20.  
  21. ResultSet rs = null;  
  22.  
  23. String sql;  
  24.  
  25. String m_password="";  
  26.  
  27. sql = "SELECT" 
  28.  
  29. + "       DB2ADMIN.YHDL.PWD"  
  30.  
  31. + " FROM"  
  32.  
  33. + "    DB2ADMIN.YHDL"  
  34.  
  35. + " WHERE"  
  36.  
  37. + "    ("  
  38.  
  39. + "       ( "  
  40.  
  41. + "          DB2ADMIN.YHDL.ID = '"+m_id.trim()+"'"  
  42.  
  43. + "       )"  
  44.  
  45. + "    )";  
  46.  
  47. stmt = con.prepareStatement( sql );  
  48.  
  49. rs = stmt.executeQuery();  
  50.  
  51. // Access query results  
  52.  
  53. while (rs.next())  
  54.  
  55. {  
  56.  
  57. m_password=rs.getString(1);  
  58.  
  59. m_passwordm_password=m_password.trim();  
  60.  
  61. if (rs.wasNull())  
  62.  
  63. System.out.print("NULL");  
  64.  
  65. else  
  66.  
  67. System.out.print(m_password);  
  68.  
  69. }  
  70.  
  71. if(m_password.equals(m_pwd.trim()))  
  72.  
  73. {  
  74.  
  75. result[0] =1;  
  76.  
  77. }  
  78.  
  79. else  
  80.  
  81. {  
  82.  
  83. result[0] =0;  
  84.  
  85. }  
  86.  
  87. // close open resources  
  88.  
  89. if (rs != null) rs.close();  
  90.  
  91. if (stmt != null) stmt.close();  
  92.  
  93. if (con != null) con.close();  
  94.  
  95. // set return parameter  
  96.  
  97. //result[0] = result[0];  
  98.  
  99. }  
  100.  

This article describes how to call a stored procedure in a DB2 database.

Related Article

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.