Results 1 to 2 of 2

Thread: Set names utf8

  1. #1
    initmax's Avatar
    initmax is offline Member
    Join Date
    Oct 2009
    Posts
    9
    Rep Power
    0

    Default Set names utf8

    Problem with set char set after connect to MySQL

    I have the class for connection:

    PHP Code:
    /*
     * 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

    PHP Code:
    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");

    PHP Code:
    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
    PHP Code:
    public void setEncoding(String Encoding)
    why it does not work?

  2. #2
    Menre is offline Member
    Join Date
    Feb 2008
    Posts
    79
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 12-22-2008, 12:24 AM
  2. Drive names
    By alwz_nikhil in forum New To Java
    Replies: 0
    Last Post: 11-28-2007, 11:04 AM
  3. how to get the names of the files
    By mary in forum Advanced Java
    Replies: 2
    Last Post: 08-05-2007, 05:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •