Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

SET NAMES UTF8

843859Nov 5 2009 — edited Nov 20 2014
Problem with set char set after connect to MySQL
I have the class for connection:
/*
 * Class CreateBaseConnection provide connect to base MySQL
 * 
 */
package MySQL;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author initmax
 */
public class MySQLConnect {

private String url = null;
private String name = null;
private String password = null;

    public MySQLConnect() {
      url = "jdbc:mysql://127.0.0.1:3030/initmax?autoReconnect=true";
      name = "root";
      password = ":Lkf)93450LJfelffnnv><Nf.,mn:LKJfdf><Mn,nfg89o";
    }

    public MySQLConnect(String a_url, String a_name, String a_password){
        url = a_url;
        name = a_name;
        password = a_password;
    }
//***********************************************************************
private Connection CurrentConnect;

//this line need out to txt file

//***********************************************************************
//first need download driver, for work MySQL connect
    public void downloadDriver(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
//            System.out.println("Driver loading success!");
        }
        catch (ClassNotFoundException e){
            e.printStackTrace();
        }
    }

//***********************************************************************
//Connected to MySQL Base use download driver
     public void connected(){
            try {
                CurrentConnect = DriverManager.getConnection(url, name, password);
//                System.out.println("Connected.");
            }

            catch (SQLException e) {
                    e.printStackTrace();
            }
     }      
    
//*********************************************************************
//disconnected base after finish work, get object connect
     public int disConnected() {
         try {
          CurrentConnect.close();
          System.out.println("Disconnected.");
         }
         catch (SQLException e) {
                e.printStackTrace();
         }

         return 0;
      }
//get current connection
      public Connection getConnection() {
          return this.CurrentConnect;
      }
} 
 
Class for set char set and other query
package MySQL;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author initmax
 */
public class MySQLQuery {

    private Connection CurrentConnect; //obj for connect to databae
  

    public MySQLQuery() {}

//constructor accept current conection database
    public MySQLQuery(Connection CurrentConnectBase) {
    CurrentConnect = CurrentConnectBase;
    }



    public void setEncoding(String Encoding) {

 //       out.print("MySQLConnectObj.GetConnection() = "+MySQLConnectObj.getConnection());

        try{
         Statement st = CurrentConnect.createStatement();
         String query0 = ("SET NAMES "+Encoding);
          String query1 = ("SET character_set_client = "+Encoding);
           String query2 = ("SET character_set_connection = "+Encoding);
            String query3 = ("SET character_set_database = "+Encoding);
             String query4 = ("SET character_set_results = "+Encoding);
              String query5 = ("SET character_set_server = "+Encoding);
               String query6 = ("SET character_set_system = "+Encoding);

         st.executeQuery(query0);
         st.executeQuery(query1);
         st.executeQuery(query2);
         st.executeQuery(query3);
         st.executeQuery(query4);
         st.executeQuery(query5);
        }

        catch (SQLException e) {
         e.printStackTrace();
        }



    }

//*****************************************************************
//method accept name table and link on List, after work return all info on this table inside List
public List<GenPageMySQL> selectAllField(String NameTable, List<GenPageMySQL> ListPageObj) {
       try {
         Statement st = CurrentConnect.createStatement();
         String query = ("select * from "+NameTable);
         ResultSet resultQuery = null;
         resultQuery = st.executeQuery(query);


//step in cycle after execution query, and create Vector object
           while (resultQuery.next()) {
              GenPageMySQL PageObj = new GenPageMySQL();

              PageObj.setId(resultQuery.getInt("id"));
              PageObj.setTheme(resultQuery.getString("theme"));
              PageObj.setPage(resultQuery.getString("page"));

              ListPageObj.add(PageObj); //add obj in tail vector
           }
       }
       catch (SQLException e) {
         e.printStackTrace();
      }
       return ListPageObj;        
}
    /**
    *@set the CurrentConnect
    */
    public void setConnection(Connection CurrentConnectBase) {
         CurrentConnect = CurrentConnectBase;
    }
}
After connection in start servlet, I set char set my database, executing this method MySQLQueryObj.setEncoding("utf8");
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import MySQL.*;

public class indexServlet extends HttpServlet {

    private  String getpage;
    //Connected MySQL
    private MySQLConnect MySQLConnectObj = new MySQLConnect();
    private MySQLQuery MySQLQueryObj = new MySQLQuery();

    public void init() {
          MySQLConnectObj.downloadDriver();
          MySQLConnectObj.connected();
          
          //Use current connection, for execution query
          MySQLQueryObj.setConnection(MySQLConnectObj.getConnection());
          MySQLQueryObj.setEncoding("UTF8");     
    }
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, NullPointerException {

          init();
        

        PrintWriter out = response.getWriter();
       out.print("MySQLConnectObj.GetConnection() = "+MySQLConnectObj.getConnection());

            List<GenPageMySQL> ListPageObj = new ArrayList();
            //get List<RowObject>

            MySQLQueryObj.setEncoding("utf8");
            ListPageObj = MySQLQueryObj.selectAllField("up_menu",ListPageObj);
  
            out.print("!!!!!!!!!--- "+ListPageObj.get(1).getPage());

            MySQLConnectObj.disConnected();
    //        request.setAttribute("upMenu",ListPageObj);
    //        RequestDispatcher Dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/jsp/index.jsp");
    //        Dispatcher.forward(request, response);
      }
}
but words in browser printed so
"???????????????????..?????????...?????????..."

if I connect to MySQL server in console, and execute
SET NAMES UTF8;
text my tables in console printing correctly...

I call in main servlet method
 public void setEncoding(String Encoding) 
why it does not work?

Edited by: initmax on Nov 5, 2009 2:10 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 9 2009
Added on Nov 5 2009
2 comments
465 views