Domanda

I am trying to convert serialclob to string. But I am not successful. I think I am not doing in the correct way. My code is:

char[] buffer;
        int count = 0;
        int length = 0;
        String data = null;
        String[] type;
        StringBuffer sb;
        try{
            SqlRowSet rows = getJdbcTemplate().queryForRowSet("select c.BOUNDING_BOX.GET_WKT() BOUNDING_BOX FROM EXAMPLE c WHERE EXAMPLE_ID = 100",
                    new Object[] {subid});
            SubscriptionModel subscription = new SubscriptionModel();
            System.out.println("bbox");
            SqlRowSetMetaData rsmd = rows.getMetaData();
                type = new String[rsmd.getColumnCount()];
                for(int col=0;col<rsmd.getColumnCount();col++)
                    type[col] = rsmd.getColumnTypeName(col + 1);
                System.out.println("Read rows and only serial clob data type values");
            while(rows.next()){
                System.out.println("test 1 here");
                for(int col=0;col<rsmd.getColumnCount();col++){
                    System.out.println("test 2 here");
                    if("CLOB".equals(type[col]){
                        System.out.println("test 3 here");
                        SerialClob clob = (SerialClob) ((ResultSet) rows).getClob(col + 1);
                        if(clob != null){
                            System.out.println("clob is not null");
                            Reader is = clob.getCharacterStream();

                            sb = new StringBuffer();
                            length = (int) clob.length();

                            if(length>0){
                                buffer = new char[length];
                                count = 0;
                                try{
                                    while((count = is.read(buffer)) != -1)
                                        sb.append(buffer);

                                    data = new String(sb);
                                }catch(Exception e){

                                }
                            }
                            else
                                data = (String) null;
                        }else
                            data = (String) null;
                    }else{
                        data = (String) rows.getObject(col + 1);
                    }
                }
            }
            return subscription;
    }catch(Exception e){
        e.printStackTrace();
    }

But I am getting an error like:

bbox
Read rows and only serial clob data type values
test 1 here
test 2 here
java.lang.ClassCastException: javax.sql.rowset.serial.SerialClob cannot be cast to java.lang.String

Where is my mistake?

È stato utile?

Soluzione

Well this is at least one problem:

if(type[col] == "CLOB")

That's comparing string references directly. I suspect this would work better:

if ("CLOB".equals(type[col]))

That will get you into the right if block. I haven't looked in detail at the rest of your code - it would be simpler to read and maintain if you could break it up into smaller methods.

Altri suggerimenti

The problem is most likely here --

if(type[col] == "CLOB"){ // <-- This does Object equality

Use String#equals() method to do String comparisons.

if ( "CLOB".equals(type[col]) ) {
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top