自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

DB2數(shù)據(jù)庫(kù)調(diào)用存儲(chǔ)過程的方法及實(shí)例介紹

數(shù)據(jù)庫(kù)
本文我們主要介紹了DB2數(shù)據(jù)庫(kù)對(duì)存儲(chǔ)過程的調(diào)用方法,并給出了一個(gè)調(diào)用存儲(chǔ)過程的實(shí)例,通過這個(gè)實(shí)例我們能夠更清晰地理解DB2調(diào)用存儲(chǔ)過程的原理,希望能夠?qū)δ兴鶐椭?/div>

上次我們介紹了DB2數(shù)據(jù)庫(kù)創(chuàng)建觸發(fā)器的實(shí)現(xiàn)過程,本文我們來介紹一下DB2數(shù)據(jù)庫(kù)對(duì)存儲(chǔ)過程的調(diào)用,接下來就讓我們來一起了解一下這部分內(nèi)容吧。

一、對(duì)存儲(chǔ)過程的調(diào)用分三部分

1.連接(與數(shù)據(jù)庫(kù)建立連接)

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

2.注冊(cè)輸出參數(shù)

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

3.調(diào)用存儲(chǔ)過程:

  1. CallableStatement cs=con.prepareCall("{call store_name(參數(shù),參數(shù),參數(shù))}"); 

二、調(diào)用舉例:

  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. //與數(shù)據(jù)庫(kù)建立連接  
  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. //創(chuàng)建Statement對(duì)象,用于執(zhí)行SQL語句  
  40.  
  41. Statement stmt=con.createStatement();  
  42.  
  43. //創(chuàng)建CallableStatement對(duì)象,用于執(zhí)行存儲(chǔ)過程  
  44.  
  45. CallableStatement cs=con.prepareCall("{call PRO_YHDL1(?,?,?)}");  
  46.  
  47. //注冊(cè)輸出參數(shù)  
  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.execute();  
  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.  

三、存儲(chǔ)過程舉例:

Pro_yhdl1是一個(gè)存儲(chǔ)過程,它的功能是從數(shù)據(jù)庫(kù)表YHDL中取出PWD:

  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.  

關(guān)于DB2數(shù)據(jù)庫(kù)調(diào)用存儲(chǔ)過程的知識(shí)就介紹到這里了,希望本次的介紹能夠?qū)δ兴鶐椭?/p>

【編輯推薦】

  1. Oracle數(shù)據(jù)庫(kù)中Constraint約束的四對(duì)屬性
  2. SQL Server 2005無法連接到本地服務(wù)器的解決
  3. Linux下重新配置MySQL數(shù)據(jù)庫(kù)引擎innodb的過程
  4. Navicat MySQL連接Linux下MySQL的問題解決方案
  5. SQL Server 2000在Windows7 旗艦版中的安裝配置

 

責(zé)任編輯:趙鵬 來源: CSDN博客
相關(guān)推薦

2010-08-27 10:06:23

DB2安裝雙機(jī)

2010-09-30 11:49:21

DB2數(shù)據(jù)庫(kù)權(quán)限

2010-08-31 15:39:25

DB2存儲(chǔ)過程

2010-11-03 10:46:49

DB2存儲(chǔ)過程

2010-08-31 13:06:49

DB2數(shù)據(jù)庫(kù)

2010-08-25 09:56:02

DB2存儲(chǔ)過程

2010-08-05 10:20:29

DB2數(shù)據(jù)庫(kù)動(dòng)態(tài)

2011-03-04 17:54:45

DB2數(shù)據(jù)庫(kù)卸載

2010-08-27 11:08:59

DB2安裝目錄

2010-08-27 11:03:44

DB2數(shù)據(jù)庫(kù)性能調(diào)整

2010-09-30 10:59:32

卸載DB2數(shù)據(jù)庫(kù)

2010-08-31 14:24:25

DB2聯(lián)合數(shù)據(jù)庫(kù)

2010-08-27 14:39:46

db2連接數(shù)據(jù)庫(kù)

2011-03-11 16:02:03

DB2數(shù)據(jù)庫(kù)安裝

2010-11-03 10:35:45

DB2存儲(chǔ)過程

2010-11-03 11:02:34

DB2存儲(chǔ)過程

2010-11-01 13:34:20

DB2數(shù)據(jù)庫(kù)安裝

2010-11-03 11:36:53

訪問DB2表

2010-08-27 11:28:39

DB2shell數(shù)據(jù)庫(kù)

2010-11-02 13:40:34

DB2函數(shù)調(diào)用
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)