db2 处理大字段 方法 java

大家好,我以前处理oracle数据库大字段时没啥问题,比如
插入一条记录 将大字段 对应使用 empty_clob() 函数代替,然后 select for update这条记录
然后在改写大字段内容,最后commit提交
但是 我现在使用 db2数据库 报 classcast 异常,啥原因? shi因为 数据库jdbc jar不正确 还是?
我的部分代码:

if(rs.next()){
COM.ibm.db2.app.Clob msgClob = (COM.ibm.db2.app.Clob)rs.getClob(Alert_logPoHelper.OP_MSG.getName());
COM.ibm.db2.app.Clob descClob = (COM.ibm.db2.app.Clob)rs.getClob(Alert_logPoHelper.OP_DESC.getName());
try {
Writer writer = msgClob.getWriter();
writer.write(msgInfo.toCharArray());
writer.flush(); //强制写入
writer.close(); //关闭
writer = descClob.getWriter();
writer.write(desInfo.toCharArray());
} catch (Exception e) {
e.printStackTrace();
}
}

java.lang.ClassCastException: com.ibm.db2.jcc.b.vd
at com.mjp.system.SystemLogUtil$1.callBack(SystemLogUtil.java:77)
at com.mjp.core.db.MyQueryRunner.rsQuery(MyQueryRunner.java:160)
at com.mjp.system.SystemLogUtil.dbLog(SystemLogUtil.java:93)
at com.mjp.system.SystemLogUtil.main(SystemLogUtil.java:131)
不能将 java.sql.Clob 转换成 COM.ibm.db2.app.Clob ?
大家 在db2上是如何操作 Clob的?

[color=indigo]http://www.ibm.com/developerworks/cn/data/library/techarticles/0310bhogal/0310bhogal.html
http://lavasoft.blog.51cto.com/62575/64683/
希望对你有帮助[/color]

可以参考一下这文章

http://lavasoft.blog.51cto.com/62575/64683/

JAVA中如何读写DB2 CLOB类型的数据

参考下面的代码:

/*
(c) Copyright IBM Corp. 2000 All rights reserved.

This sample program is owned by International Business Machines

Corporation or one of its subsidiaries ("IBM" and is copyrighted

and licensed, not sold.

You may copy, modify, and distribute this sample program in any

form without payment to IBM, for any purpose including developing,
using, marketing or distributing programs that include or are

derivative works of the sample program.

The sample program is provided to you on an "AS IS" basis, without
warranty of any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL

WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED

TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A

PARTICULAR PURPOSE.

Some jurisdictions do not allow for the exclusion or limitation of
implied warranties, so the above limitations or exclusions may not
apply to you. IBM shall not be liable for any damages you suffer

as a result of using, modifying or distributing the sample program
or its derivatives.

Each copy of any portion of this sample program or any derivative

work, must i

*/

import java.lang.*;
import java.sql.*;
import java.io.*;

/**

  • Sample Code to insert a blob data type into the database. WriteBlob class accepts 6 arguments from the user during
  • runtime. These are the databasename, user id, password, tablename, columnname and picture filename. It inserts a .jpg
  • picture file from the current directory into the column of BLOB data type as specified by the user.
  • @author Suita Gupta
  • @version 0.1 (02/04/2002 11:10:00 AM)
  • @see java.sql.*
  • @since JDK 1.3 */

public class WriteBlob {

private static PreparedStatement ps;

/**

  • Method to establish and return a connection object. This connection object is used to execute the prepared statements to
  • insert the BLOB into the database.
  • @param argv[] - User's command line input for databasename, user id, password, tablename, columnname, picture filename
  • @return con - Connection object
  • */

    public static Connection getConnect(String argv[])
    {
            Connection con = null;
            String url=""; 
            String dbname = "";
            String user_id = "";
            String pwd = "";
    
            try
            {        
                    // register the driver with DriverManager
                     // The newInstance() call is needed for the sample to work with
                     // JDK 1.1.1 on OS/2, where the Class.forName() method does not
                     // run the static initializer. For other JDKs, the newInstance
                     // call can be omitted.
                    Class.forName("COM.ibm.db2.jdbc.app.DB2Driver".newInstance();
    
                    if (argv.length == 6){
                            //assign values from command line input to dbname, user_id, pwd variables
                            dbname = argv[0];
                            user_id = argv[1];
                            pwd = argv[2];
                            url = "jdbc:db2:" + dbname;
                            con = DriverManager.getConnection(url, user_id, pwd);
                       }
                     else{
                        System.out.println("\nUsage: java WriteBlob [databasename user id password tablename columnname picture.jpg]\n";
                        System.exit(0);
                     }
    
                    System.out.println ("You are connected to " + dbname);
            } // end try block
    
            catch (Exception e){ 
                    e.printStackTrace(); 
            } // end catch block
    
            return con;
    
    } // end of getConnect()
    

/**

  • Method to insert the picture file from current directory to the table mentioned by user. A point to note is that the
  • setBinaryStream() does not have a limit unlike the setBytes() which has a limit of 33124 bytes. Both however are valid methods
  • for retrieving a stream to write to the BLOB value.
  • @param argv[] user's command line input for databasename, user id, password, tablename, columnname, picture filename
  • @param con Connection object
  • */

    public static void writePicture(Connection con, String argv[]){

            //Assuming that the first column of the table is of BLOB data type
            String insert_picture = "insert into " + argv[3] + " (" + argv[4] + "" + " VALUES(?)";
    
            try{
    
                    //create a new file object for the picture
                       File fil =new File(argv[5]);
    
                    //Creating byte[] data array to store the picture file
                    byte[] arrBytes = new byte[ ((new Long(fil.length())).intValue())];
    
                    //Initialising PreparedStatement ps with query        
                    ps =con.prepareStatement(insert_picture);
    
                    //Using ps.setBinaryStream() instead of ps.setBytes to retrieve source for BLOB value to be inserted into the database
                    //setBinaryStream() accepts an inputstream and the length, once the statement is executed the length value
              //is read and inserted into the column.
                    ps.setBinaryStream(1, new ByteArrayInputStream(arrBytes), Integer.MAX_VALUE); 
    
                    //Execute the prepared statement
                    ps.execute();
    
            }//end of try 
    
            catch(Exception ee){
                    ee.printStackTrace();        
            }//end of catch
    
            System.out.println("Successfully inserted picture.jpg into " + argv[0]+ "." + argv[3]);
    
    }//end of writePicture()
    

/**

  • Main method which invokes the getConnect() and passes user's command line input. A connection object is returned from the
  • getConnect() and this is passed along with argv[] to the writePicture(). Connection is then closed once the BLOB has
  • successfully been inserted into the table.
  • @param argv[] user's command line input for databasename, user id, password, tablename,columnname, picture filename
  • */

    public static void main(String[] argv){
    
            try{
                    //Establish connection
                    Connection conn = getConnect(argv);
    
                    //invoke writePicture method and pass connection object and command line input for tablename and columnname values 
                    writePicture(conn,argv);
    
                    // Close connection object
                    conn.close();
    
            } //end try block
    
            catch (Exception ex)
            {
                    ex.printStackTrace();
            } //end catch block
    
    }//end of main()
    

}//end of WriteBlob class