简体   繁体   中英

What could happen if I use resultset.getString() by passing a blob?

I have a blob column in my table, using MySQL. I'm trying to get this value by the getString method of ResultSet, rs.getString("xxxxxx") I'm just not sure if there would be a problem in some situation. Dose it depend on what I stored in this column?

Can anyone help with this?

It doesn't matter what is stored in the database. It doesn't matter it is String or int. You get data from the query result and it is interpreted as the String. String is not some sequence of bytes. It is reference to the Object which expected to be a String. Which means reference points to some structure in memory which is mostly structure of other references. So, when you take some sequence of bytes and assume that this is reference to the Object then, most likely, you will have Runtime exception. Probably it will be ClassCastException but who knows what your data has. So even exception is unpredictable in this case.

Bottom line - don't do this.

There are no restrictions, you can use rs.getString("xxxxxx") , according to the implementation of getString you will get a String that will contain your blob data in the form of byte array

    if ((localObject1 instanceof OracleSerialBlob)) {
        localObject2 = (OracleSerialBlob) localObject1;
        return new String(((OracleSerialBlob) localObject2).getBytes(0L, (int) ((OracleSerialBlob) localObject2).length()));
    }

as above if condition calls below String constructor:

public String(byte bytes[]) {
    this(bytes, 0, bytes.length);
}

Below is the implementation of getString("xxxx");

public String getString(int paramInt) throws SQLException {
    Object localObject1 = getObject(paramInt);

    if (localObject1 == null) {
        return (String) localObject1;
    }
    if ((localObject1 instanceof String)) {
        return (String) localObject1;
    }
    if (((localObject1 instanceof Number)) || ((localObject1 instanceof BigDecimal))) {
        return localObject1.toString();
    }
    if ((localObject1 instanceof java.sql.Date)) {
        return ((java.sql.Date) localObject1).toString();
    }
    if ((localObject1 instanceof Timestamp)) {
        return ((java.sql.Date) localObject1).toString();
    }
    if ((localObject1 instanceof byte[]))
        return new String((byte[]) localObject1);
    Object localObject2;
    if ((localObject1 instanceof OracleSerialClob)) {
        localObject2 = (OracleSerialClob) localObject1;
        return ((OracleSerialClob) localObject2).getSubString(0L, (int) ((OracleSerialClob) localObject2).length());
    }

    if ((localObject1 instanceof OracleSerialBlob)) {
        localObject2 = (OracleSerialBlob) localObject1;
        return new String(((OracleSerialBlob) localObject2).getBytes(0L, (int) ((OracleSerialBlob) localObject2).length()));
    }

    if ((localObject1 instanceof URL)) {
        return ((URL) localObject1).toString();
    }
    if ((localObject1 instanceof Reader)) {
        try {
            localObject2 = (Reader) localObject1;
            char[] arrayOfChar = new char[1024];
            int i = 0;
            StringBuffer localStringBuffer = new StringBuffer(1024);
            while ((i = ((Reader) localObject2).read(arrayOfChar)) > 0)
                localStringBuffer.append(arrayOfChar, 0, i);

            return localStringBuffer.substring(0, localStringBuffer.length());
        } catch (IOException localIOException) {
            throw new SQLException("Error during conversion: " + localIOException.getMessage());
        }
    }

    throw new SQLException("Fail to convert to internal representation");
}

Reference .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM