Logo Search packages:      
Sourcecode: mysql-connector-java version File versions

boolean com::mysql::jdbc::Connection::configureClientCharacterSet (  )  throws SQLException [inline, private]

Sets up client character set for MySQL-4.1 and newer if the user This must be done before any further communication with the server!

Returns:
true if this routine actually configured the client character set, or false if the driver needs to use 'older' methods to detect the character set, as it is connected to a MySQL server older than 4.1.0
Exceptions:
SQLException if an exception happens while sending 'SET NAMES' to the server, or the server sends character set information that the client doesn't know about.

Definition at line 2948 of file Connection.java.

References configureCharsetProperties(), database, execSQL(), com::mysql::jdbc::ConnectionProperties::getCharacterSetResults(), com::mysql::jdbc::ConnectionProperties::getConnectionCollation(), com::mysql::jdbc::ConnectionProperties::getEncoding(), com::mysql::jdbc::ConnectionProperties::getUseOldUTF8Behavior(), com::mysql::jdbc::ConnectionProperties::getUseUnicode(), io, com::mysql::jdbc::MysqlIO::serverCharsetIndex, com::mysql::jdbc::ConnectionProperties::setEncoding(), and com::mysql::jdbc::ConnectionProperties::setUseUnicode().

Referenced by changeUser(), and initializePropsFromServer().

                                                                      {
        String realJavaEncoding = getEncoding();
        boolean characterSetAlreadyConfigured = false;

        try {
            if (versionMeetsMinimum(4, 1, 0)) {
                characterSetAlreadyConfigured = true;

                setUseUnicode(true);

                configureCharsetProperties();
                realJavaEncoding = getEncoding(); // we need to do this again to grab this for 
                                                  // versions > 4.1.0

                try {
                    setEncoding(CharsetMapping.INDEX_TO_CHARSET[this.io.serverCharsetIndex]);
                } catch (ArrayIndexOutOfBoundsException outOfBoundsEx) {
                    if (realJavaEncoding != null) {
                        // user knows best, try it
                        setEncoding(realJavaEncoding);
                    } else {
                        throw new SQLException(
                            "Unknown initial character set index '"
                            + this.io.serverCharsetIndex
                            + "' received from server. Initial client character set can be forced via the 'characterEncoding' property.",
                            SQLError.SQL_STATE_GENERAL_ERROR);
                    }
                }

                if (getEncoding() == null) {
                    // punt?
                    setEncoding("ISO8859_1");
                }

                //
                // Has the user has 'forced' the character encoding via
                // driver properties?
                //
                if (getUseUnicode()) {
                  if (realJavaEncoding != null) {
                      
                          //
                          // Now, inform the server what character set we
                          // will be using from now-on...
                          //
                          if (realJavaEncoding.equalsIgnoreCase("UTF-8")
                                  || realJavaEncoding.equalsIgnoreCase("UTF8")) {
                              // charset names are case-sensitive
                              
                              if (!getUseOldUTF8Behavior()) { 
                                    execSQL(null, "SET NAMES utf8", -1, null,
                                                java.sql.ResultSet.TYPE_FORWARD_ONLY,
                                                            java.sql.ResultSet.CONCUR_READ_ONLY, false, false,
                                                            this.database, true, Statement.USES_VARIABLES_FALSE);
                              }
                              
                              setEncoding(realJavaEncoding);
                          } /* not utf-8 */ else {
                              String mysqlEncodingName = (String) CharsetMapping.JAVA_UC_TO_MYSQL_CHARSET_MAP
                                  .get(realJavaEncoding.toUpperCase(Locale.ENGLISH));
      
                              if ("koi8_ru".equals(mysqlEncodingName)) {
                                  // This has a _different_ name in 4.1...
                                  mysqlEncodingName = "ko18r";
                              }
      
                              if (mysqlEncodingName != null) {
                                  execSQL(null, "SET NAMES " + mysqlEncodingName, -1,
                                      null, java.sql.ResultSet.TYPE_FORWARD_ONLY,
                                      java.sql.ResultSet.CONCUR_READ_ONLY, false,
                                      false, this.database, true,
                                      Statement.USES_VARIABLES_FALSE);
                              }
      
                              // Switch driver's encoding now, since the server
                              // knows what we're sending...
                              //
                              setEncoding(realJavaEncoding);
                          }
                  } else if (getEncoding() != null) {
                        // Tell the server we'll use the server default charset to send our
                        // queries from now on....
                        String mysqlEncodingName = (String) CharsetMapping.JAVA_UC_TO_MYSQL_CHARSET_MAP
                              .get(getEncoding().toUpperCase(Locale.ENGLISH));
                              
                        execSQL(null, "SET NAMES " + mysqlEncodingName, -1,
                                null, java.sql.ResultSet.TYPE_FORWARD_ONLY,
                                java.sql.ResultSet.CONCUR_READ_ONLY, false,
                                false, this.database, true,
                                Statement.USES_VARIABLES_FALSE);
                              
                        realJavaEncoding = getEncoding();
                    }
                  
                }
                
                //
                // We know how to deal with any charset coming back from
                // the database, so tell the server not to do conversion
                // if the user hasn't 'forced' a result-set character set
                //
                
                if (getCharacterSetResults() == null) {
                  execSQL(null, "SET character_set_results = NULL", -1, null, java.sql.ResultSet.TYPE_FORWARD_ONLY,
                            java.sql.ResultSet.CONCUR_READ_ONLY, false,
                            false, this.database, true,
                            Statement.USES_VARIABLES_FALSE);
                } else {
                  StringBuffer setBuf = new StringBuffer("SET character_set_results = ".length() + getCharacterSetResults().length());
                  setBuf.append("SET character_set_results = ").append(getCharacterSetResults());
                  
                  execSQL(null, setBuf.toString(), -1, null, java.sql.ResultSet.TYPE_FORWARD_ONLY,
                            java.sql.ResultSet.CONCUR_READ_ONLY, false,
                            false, this.database, true,
                            Statement.USES_VARIABLES_FALSE);
                }
                
                if (getConnectionCollation() != null) {
                  StringBuffer setBuf = new StringBuffer("SET collation_connection = ".length() + getConnectionCollation().length());
                  setBuf.append("SET collation_connection = ").append(getConnectionCollation());
                  
                  execSQL(null, setBuf.toString(), -1, null, java.sql.ResultSet.TYPE_FORWARD_ONLY,
                            java.sql.ResultSet.CONCUR_READ_ONLY, false,
                            false, this.database, true,
                            Statement.USES_VARIABLES_FALSE);
                }
            } else {
                // Use what the server has specified
                realJavaEncoding = getEncoding(); // so we don't get 
                                                  // swapped out in the finally
                                                  // block....
            }
        } finally {
            // Failsafe, make sure that the driver's notion of character
            // encoding matches what the user has specified.
            //
            setEncoding(realJavaEncoding);
        }

        
        
        return characterSetAlreadyConfigured;
    }


Generated by  Doxygen 1.6.0   Back to index