DB2數(shù)據(jù)庫調(diào)用存儲過程的方法及實例介紹
作者:yuejingjiahong 
  本文我們主要介紹了DB2數(shù)據(jù)庫對存儲過程的調(diào)用方法,并給出了一個調(diào)用存儲過程的實例,通過這個實例我們能夠更清晰地理解DB2調(diào)用存儲過程的原理,希望能夠對您有所幫助。
 上次我們介紹了DB2數(shù)據(jù)庫創(chuàng)建觸發(fā)器的實現(xiàn)過程,本文我們來介紹一下DB2數(shù)據(jù)庫對存儲過程的調(diào)用,接下來就讓我們來一起了解一下這部分內(nèi)容吧。
一、對存儲過程的調(diào)用分三部分
1.連接(與數(shù)據(jù)庫建立連接)
- Class.forName("COM.ibm.db2.jdbc.net.DB2Driver").newInstance();
 - Connection con=DriverManager.getConnection(url,user,password);
 
2.注冊輸出參數(shù)
- cs.registerOutParameter (3, Types.INTEGER);
 
3.調(diào)用存儲過程:
- CallableStatement cs=con.prepareCall("{call store_name(參數(shù),參數(shù),參數(shù))}");
 
二、調(diào)用舉例:
- import java.net.URL;
 - import java.sql.*;
 - class test2
 - {
 - public static void main(String args[])
 - {
 - String url = "jdbc:db2://wellhope/sample";
 - String user="db2admin";
 - String password="db2admin";
 - try
 - {
 - Class.forName("COM.ibm.db2.jdbc.net.DB2Driver").newInstance();
 - //與數(shù)據(jù)庫建立連接
 - Connection con=DriverManager.getConnection(url,user,password);
 - checkForWarning(con.getWarnings());
 - DatabaseMetaData dma=con.getMetaData();
 - String str="This is a string";
 - //int hashcode=str.hashCode();
 - //System.out.println("Hashcode "+hashcode);
 - //創(chuàng)建Statement對象,用于執(zhí)行SQL語句
 - Statement stmt=con.createStatement();
 - //創(chuàng)建CallableStatement對象,用于執(zhí)行存儲過程
 - CallableStatement cs=con.prepareCall("{call PRO_YHDL1(?,?,?)}");
 - //注冊輸出參數(shù)
 - cs.registerOutParameter (3, Types.INTEGER);
 - int result = 0;
 - cs.setString(1,"123");
 - cs.setString(2,"123");
 - cs.execute();
 - result = cs.getInt (3);
 - dispResultSet(result);
 - cs.close();
 - con.close();
 - }
 - catch(SQLException ex)
 - {
 - System.out.println(" * * * SQLException caught * * * ");
 - while(ex!=null)
 - {
 - System.out.println("SQLState: "+ex.getSQLState());
 - System.out.println("Message: "+ex.getMessage());
 - System.out.println("Vendor: "+ex.getErrorCode());
 - exex=ex.getNextException();
 - System.out.println("");
 - }
 - }
 - catch(java.lang.Exception ex)
 - {
 - ex.printStackTrace();
 - }
 - }
 
三、存儲過程舉例:
Pro_yhdl1是一個存儲過程,它的功能是從數(shù)據(jù)庫表YHDL中取出PWD:
- import java.sql.*;
 - public class Pro_yhdl1
 - {
 - public static void pro_yhdl1 ( String m_id,
 - String m_pwd,
 - int[] result ) throws SQLException, Exception
 - {
 - // Get connection to the database
 - Connection con = DriverManager.getConnection("jdbc:default:connection");
 - PreparedStatement stmt = null;
 - ResultSet rs = null;
 - String sql;
 - String m_password="";
 - sql = "SELECT"
 - + " DB2ADMIN.YHDL.PWD"
 - + " FROM"
 - + " DB2ADMIN.YHDL"
 - + " WHERE"
 - + " ("
 - + " ( "
 - + " DB2ADMIN.YHDL.ID = '"+m_id.trim()+"'"
 - + " )"
 - + " )";
 - stmt = con.prepareStatement( sql );
 - rs = stmt.executeQuery();
 - // Access query results
 - while (rs.next())
 - {
 - m_password=rs.getString(1);
 - m_passwordm_password=m_password.trim();
 - if (rs.wasNull())
 - System.out.print("NULL");
 - else
 - System.out.print(m_password);
 - }
 - if(m_password.equals(m_pwd.trim()))
 - {
 - result[0] =1;
 - }
 - else
 - {
 - result[0] =0;
 - }
 - // close open resources
 - if (rs != null) rs.close();
 - if (stmt != null) stmt.close();
 - if (con != null) con.close();
 - // set return parameter
 - //result[0] = result[0];
 - }
 - }
 
關于DB2數(shù)據(jù)庫調(diào)用存儲過程的知識就介紹到這里了,希望本次的介紹能夠對您有所幫助。
【編輯推薦】
- Oracle數(shù)據(jù)庫中Constraint約束的四對屬性
 - SQL Server 2005無法連接到本地服務器的解決
 - Linux下重新配置MySQL數(shù)據(jù)庫引擎innodb的過程
 - Navicat MySQL連接Linux下MySQL的問題解決方案
 - SQL Server 2000在Windows7 旗艦版中的安裝配置
 
責任編輯:趙鵬 
                    來源:
                    CSDN博客
 














 
 
 
 
 
 
 