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

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类:

2008-04-12 10:15:02

    来源:白菜乐园    作者:okcai

(三)存储过程举例:

     Pro_yhdl1是一个存储过程,它的功能是从数据库表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_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];
    }
}
 
  九 JAVA数据库链接(JDBC)
 
  DB2 的 Java 支持包括 JDBC,一个以供应商为中心的动态 SQL 接口,它通过标准的 Java方法提供对应用程序的数据存取。JDBC 与 DB2 CLI 相似之处在于您不必预编译或联编 JDBC 程序。使用 JDBC 编写的应用程序只使用动态 SQL。
  JDBC编程步骤:
  1建立与数据库的连接:
       Class.forName("Com.ibm.db2.jdbc.net.DB2Driver");
       connection con=DriverManager.getConnection(url);
  2.创建Statement对象:
       Statement stmt=con.createStatement();
  3执行查询语句:
       ResultSet rs=stmt.execQuery("SQL语句");
  4.获取和设置选项:
       ResultSetMetaData rsmd=rs.getMetaData();
       int numCols=rsmd.getColumnCount()获取结果集总列数;
       rsmd.getColumnLabel(i))获取记录值;
       setMaxRows  :设置结果集能容纳的最多行数.
       setQueryTimeout:设置一个语句执行等待的时间.
       setEscapeProcessing:通知驱动程序如何处理转义字符.
  5.关闭Statement
       stmt.clost();
 
  十 调用层接口(CLI)
  CLI不是一种新的查询语言,它只不过是应用程序可利用SQL语句去提交事务处理的一种简单接口,对数据库的查询和修改,仍要使用SQL语言编写,包括CLI函数的调用。
  调用层接口(CLI)为DB2所提供的处理动态SQL语句的两种机制之一,即在应用程序首次运行时,动态SQL语句提交给数据库系统,CLI依赖一组函数调用,可嵌入主语言中。
阅读(405) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~