Bug #2604 Error in com.mysql.jdbc2.PreparedStatement + Unicode + encoding!=null
Submitted: 2 Feb 2004 3:22 Modified: 2 Feb 2004 6:45
Reporter: Jaroslaw Szczepankiewicz Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / J Severity:S1 (Critical)
Version:2.0.14 OS:Windows (winXP, Linux)
Assigned to: CPU Architecture:Any

[2 Feb 2004 3:22] Jaroslaw Szczepankiewicz
Description:
While using something like:
jdbcURI=jdbc:mysql://localhost/mybase?user=root&useUnicode=true&characterEncoding=cp1250

there are errors in ResultSet.getString, but only when using PreparedStatement
there are additional quotes in String at 0 and end positons, this is result of error in file com.mysql.jdbc2.PreparedStatement. 

How to repeat:
use connection with useUnicode=true&characterEncoding=cp1250 
unicode + encoding!=null
+ PreparedStatement + ResultSet.getString

Suggested fix:
change method PreparedStatement.getBytes to

byte[] getBytes(int parameterIndex) throws SQLException {
                if (_IsStream[parameterIndex]) {
                        
                        return streamToBytes(_ParameterStreams[parameterIndex], false);
                }
                else {
                        String encoding = null;

                        if (_conn.useUnicode()) {
                                encoding = _conn.getEncoding();
                        }

                        if (encoding != null) {
                                try {
                                        String stringVal = _ParameterStrings[parameterIndex];

                                        if (stringVal.startsWith("'") && stringVal.endsWith("'")) {
                                                stringVal = stringVal.substring(1, stringVal.length() - 1);
                                        }
                                        return stringVal.getBytes(encoding);
                                }
                                catch (java.io.UnsupportedEncodingException uee) {
                                        throw new SQLException("Unsupported character encoding '" + encoding + "'");
                                }
                        }
                        else {
                                String stringVal = _ParameterStrings[parameterIndex];

                                if (stringVal.startsWith("'") && stringVal.endsWith("'")) {
                                        stringVal = stringVal.substring(1, stringVal.length() - 1);
                                }

                                return stringVal.getBytes();
                        }
                }
        }
[2 Feb 2004 6:45] Mark Matthews
Thank you for taking the time to report a problem.  Unfortunately
you are not using a current version of the product your reported a
problem with -- the problem might already be fixed. Please download
a new version from http://www.mysql.com/downloads/

If you are able to reproduce the bug with one of the latest versions,
please change the version on this bug report to the version you
tested and change the status back to "Open".  Again, thank you for
your continued support of MySQL.