我用setXXX(CLOB clob),getXXX();
但是我在SET的时候是从数据库中获取的CLOB字段的数据,放进数组的时候是否STRING类型的。
在this.setXXX(这里面是string)  //但是这样会报错的
我该如何将string转换成CLOB的类型呢?而且不要用resultset。
谢谢

解决方案 »

  1.   

    不好意思,没看懂你的问题。
    既然你是从数据库取出CLOB字段,那么不用resultset那是用什么取出的。
    如果从数据库里面取出的是CLOB字段,为什么会变成String类型呢。String转CLOB,下面是个例子public class TestDB { 
    public static void main(String[] args) { 
    try { /** Loading the driver*/ Class.forName("com.oracle.jdbc.Driver"); /** Getting Connection*/ 
    Connection con = DriverManager.getConnection("jdbc:oracle://localhost:3306/test","test","test"); PreparedStatement pstmt = con.prepareStatement("insert into Emp(id,name,description)values(?,?,?)"); 
    pstmt.setInt(1,5); 
    pstmt.setString(2,"Das"); 
    // Create a big CLOB value...AND inserting as a CLOB 
    StringBuffer sb = new StringBuffer(400000); sb.append("This is the Example of CLOB .."); 
    String clobValue = sb.toString(); 
    pstmt.setString(3, clobValue); 
    int i= pstmt.executeUpdate(); 
    System.out.println("Done Inserted"); 
    pstmt.close(); 
         con.close();      // Retrive CLOB values 
    Connection con = DriverManager.getConnection("jdbc:oracle://localhost:3306/test","test","test"); 
    PreparedStatement pstmt = con.prepareStatement("select * from Emp where id=5"); 
    ResultSet rs = pstmt.executeQuery(); 
    Reader instream = null; 
    int chunkSize; 
    if(rs.next()){ 
    String name = rs.getString("name"); 
    java.sql.Clob clob = result.getClob("description") 
    StringBuffer sb1 = new StringBuffer(); chunkSize = ((oracle.sql.CLOB)clob).getChunkSize(); 
    instream = clob.getCharacterStream(); 
    BufferedReader in = new BufferedReader(instream); 
    String line = null; 
    while ((line = in.readLine()) != null) { 
    sb1.append(line); 
        } 
    if(in != null){ 
    in.close(); 
    } String clobdata = sb1.toString(); // this is the clob data converted into string } 
    } catch (Exception e) { e.printStackTrace(); 

         } 
      

  2.   


    我只是希望不要用conn,因为用conn有点太麻烦。
    我是通过
    ArrayList arr
    arr.get(i).toString();去获取数据库中每行的值,然后再取出每个字段的值的。
    所以取出来的都是string类型的。
      

  3.   

    从数据库中取出的数据放入byte[]数组中.
    String转换byte[]数组可以用String.getBytes() 方法。
      

  4.   

    谁帮我写个String类型转换成Clob类型的方法,急用,谢谢了
      

  5.   

    在entity类里加上一个字段如:Sting contentStr ,Clob content是在数据库里,在进行增删改的时候,把content转换成contentStr,如:entity.setContentStr(Class.ClobToString(entity.getContent())),这样就ok了,在FCk中,数据内容都是用Clob的,在前台表的数据时,
    用${action.contentStr}
    public static String ClobToString(Clob clob){ 
                    if(clob==null) return ""; 
                    StringBuffer str = new StringBuffer(); 
                    Reader reader = null; 
                     
                    try { 
                            reader = clob.getCharacterStream(); 
                            //char[] ch = new char[10240]; 
                            int len = -1; 
                            StringBuffer strb =new StringBuffer(); 
                            while((len=reader.read())!=-1){ 
                                    strb.append((char)len); 
                            } 
                            //System.out.println(new String(strb.toString().getBytes("gb2312"),"gbk")); 
                            return new String(strb.toString().getBytes("gb2312"),"gbk"); 
                             
                    } catch (SQLException e) { 
                            e.printStackTrace(); 
                    } catch (IOException e) { 
                            e.printStackTrace(); 
                    }finally{ 
                            if(reader!=null){ 
                            try { 
                                    reader.close(); 
                            } catch (IOException e) { 
                                    e.printStackTrace(); 
                            } 
                            } 
                    } 
                    return ""; 
            }