Java JDBC MySQL Connection createConnectionUtf8(String connString)

Here you can find the source of createConnectionUtf8(String connString)

Description

create Connection Utf

License

Open Source License

Declaration

public static final Connection createConnectionUtf8(String connString)
            throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.sql.Connection;
import java.sql.DriverManager;

import java.sql.SQLException;
import java.sql.Statement;

import java.util.Properties;

public class Main {
    public static final Connection createConnectionUtf8(String connString)
            throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {

        if (connString == null || connString.length() == 0) {
            throw new IllegalArgumentException("connString");
        }//from ww  w. j av  a2  s  .c om

        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Properties props = new Properties();
        props.put("useUnicode", "true");
        props.put("characterEncoding", "utf8");
        props.put("characterSetResults", "utf8");
        Connection conn = DriverManager.getConnection(connString, props);

        Statement st = conn.createStatement();
        st.execute("SET NAMES 'utf8'");
        st.execute("SET character_set_client=utf8");
        st.execute("SET character_set_connection=utf8");
        st.execute("SET character_set_results=utf8");
        st.close();

        return conn;
    }
}

Related

  1. checkConnection(String dbFile)
  2. computeAverageGeneIdsPerName()
  3. createConnection()
  4. createConnection(String hostname, String port, String database, String username, String password)
  5. createConnection(String url, String user, String password)
  6. createTable(String dbFile, String tableName, String tableDefinition)
  7. deleteTableData(String tableName)
  8. getAllNames()
  9. getConnection()