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

yuejingjiahong 发表于:12年05月02日 17:33 [转载] CSDN

  • 分享:
[导读]本文我们主要介绍了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数据库调用存储过程的知识就介绍到这里了,希望本次的介绍能够对您有所帮助。

[责任编辑:赵航]
咸师
中国企业信息化从90年代初期开始起步,经过20年的发展,许多企业尤其是大中型企业的IT架构已经搭建完毕。但是,中国企业信息化建设有一个非常显著的特点是,IT系统建设是根据企业各个阶段的需求完成,并没有一个整体的规划。这就导致企业各个IT系统是孤立的,各个系统无法有效地连接起来。
官方微信
weixin
精彩专题更多
存储风云榜”是由DOIT传媒主办的年度大型活动。回顾2014年,存储作为IT系统架构中最基础的元素,已经成为了推动信息产业发展的核心动力,存储产业的发展迈向成熟,数据经济的概念顺势而为的提出。
华为OceanStor V3系列存储系统是面向企业级应用的新一代统一存储产品。在功能、性能、效率、可靠性和易用性上都达到业界领先水平,很好的满足了大型数据库OLTP/OLAP、文件共享、云计算等各种应用下的数据存储需求。
联想携ThinkServer+System+七大行业解决方案惊艳第十六届高交会
 

公司简介 | 媒体优势 | 广告服务 | 客户寄语 | DOIT历程 | 诚聘英才 | 联系我们 | 会员注册 | 订阅中心

Copyright © 2013 DOIT Media, All rights Reserved. 北京楚科信息技术有限公司 版权所有.