Chinaunix首页 | 论坛 | 博客
  • 博客访问: 92547345
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类:

2008-04-12 10:14:31

    来源:白菜乐园    作者:okcai

(二)调用举例:

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();
      
       //与数据库建立连接
       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);
       //创建Statement对象,用于执行SQL语句
       Statement stmt=con.createStatement();
       //创建CallableStatement对象,用于执行存储过程
       CallableStatement cs=con.prepareCall("{call PRO_YHDL1(?,?,?)}");
       //注册输出参数
       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("\n * * * SQLException caught * * * \n");
      
       while(ex!=null)
       {
          System.out.println("SQLState: "+ex.getSQLState());
          System.out.println("Message: "+ex.getMessage());
          System.out.println("Vendor:  "+ex.getErrorCode());
          ex=ex.getNextException();
          System.out.println("");
       }
     }   
     catch(java.lang.Exception ex)
     {    
       ex.printStackTrace();
     }
  }
  
阅读(342) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~