新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
DB2數(shù)據(jù)庫調用存儲過程的方法及實例介紹
上次我們介紹了DB2數(shù)據(jù)庫創(chuàng)建觸發(fā)器的實現(xiàn)過程,本文我們來介紹一下DB2數(shù)據(jù)庫對存儲過程的調用,接下來就讓我們來一起了解一下這部分內容吧。

創(chuàng)新互聯(lián)建站是一家專注于網(wǎng)站制作、網(wǎng)站建設與策劃設計,湟源網(wǎng)站建設哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設十年,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:湟源等地區(qū)。湟源做網(wǎng)站價格咨詢:028-86922220
一、對存儲過程的調用分三部分
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.調用存儲過程:
- CallableStatement cs=con.prepareCall("{call store_name(參數(shù),參數(shù),參數(shù))}");
二、調用舉例:
- 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ù)庫調用存儲過程的知識就介紹到這里了,希望本次的介紹能夠對您有所幫助。
當前文章:DB2數(shù)據(jù)庫調用存儲過程的方法及實例介紹
文章地址:http://fisionsoft.com.cn/article/ccdghhi.html


咨詢
建站咨詢
