DB2数据库调用存储过程的方法及实例介绍

数据库
本文我们主要介绍了DB2数据库对存储过程的调用方法,并给出了一个调用存储过程的实例,通过这个实例我们能够更清晰地理解DB2调用存储过程的原理,希望能够对您有所帮助。

上次我们介绍了DB2数据库创建触发器的实现过程,本文我们来介绍一下DB2数据库存储过程的调用,接下来就让我们来一起了解一下这部分内容吧。

一、对存储过程的调用分三部分

1.连接(与数据库建立连接)

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

2.注册输出参数

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

3.调用存储过程:

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

二、调用举例:

  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. //与数据库建立连接  
  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. //创建Statement对象,用于执行SQL语句  
  40.  
  41. Statement stmt=con.createStatement();  
  42.  
  43. //创建CallableStatement对象,用于执行存储过程  
  44.  
  45. CallableStatement cs=con.prepareCall("{call PRO_YHDL1(?,?,?)}");  
  46.  
  47. //注册输出参数  
  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.  

三、存储过程举例:

Pro_yhdl1是一个存储过程,它的功能是从数据库表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.  

关于DB2数据库调用存储过程的知识就介绍到这里了,希望本次的介绍能够对您有所帮助。

【编辑推荐】

  1. Oracle数据库中Constraint约束的四对属性
  2. SQL Server 2005无法连接到本地服务器的解决
  3. Linux下重新配置MySQL数据库引擎innodb的过程
  4. Navicat MySQL连接Linux下MySQL的问题解决方案
  5. SQL Server 2000在Windows7 旗舰版中的安装配置

 

责任编辑:赵鹏 来源: CSDN博客
相关推荐

2010-08-27 10:06:23

DB2安装双机

2010-11-03 10:46:49

DB2存储过程

2010-09-30 11:49:21

DB2数据库权限

2010-08-31 15:39:25

DB2存储过程

2010-08-31 13:06:49

DB2数据库

2010-08-25 09:56:02

DB2存储过程

2010-08-05 10:20:29

DB2数据库动态

2011-03-04 17:54:45

DB2数据库卸载

2010-09-30 10:59:32

卸载DB2数据库

2010-08-27 11:08:59

DB2安装目录

2010-08-27 11:03:44

DB2数据库性能调整

2010-08-31 14:24:25

DB2联合数据库

2010-08-27 14:39:46

db2连接数据库

2011-03-11 16:02:03

DB2数据库安装

2010-11-03 10:35:45

DB2存储过程

2010-11-03 11:36:53

访问DB2表

2010-11-01 13:34:20

DB2数据库安装

2010-11-03 11:02:34

DB2存储过程

2010-08-27 11:28:39

DB2shell数据库

2010-11-02 13:40:34

DB2函数调用
点赞
收藏

51CTO技术栈公众号