Results 1 to 2 of 2
Thread: Set names utf8
- 11-05-2009, 12:03 PM #1
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 methodwhy it does not work?PHP Code:public void setEncoding(String Encoding)
- 11-22-2009, 10:29 AM #2
Member
- Join Date
- Feb 2008
- Posts
- 78
- Rep Power
- 0
Similar Threads
-
how to print utf8 formatted(.txt) file from DOS mode
By logic in forum New To JavaReplies: 1Last Post: 12-21-2008, 11:24 PM -
Drive names
By alwz_nikhil in forum New To JavaReplies: 0Last Post: 11-28-2007, 10:04 AM -
how to get the names of the files
By mary in forum Advanced JavaReplies: 2Last Post: 08-05-2007, 04:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks