android開發中 解決服務器端解析MySql數據時中文顯示亂碼的狀況

首先,仍是確認本身MySql帳戶和密碼java

1.示例  帳戶:root   密碼:123456   有三個字段   分別是_id  、username(插入有中文數據)、passwordmysql

1)首先咱們知道咱們的MySql通常默認的編碼類型是gdb,也就是latin1編碼。由於咱們有時候插入表中的數據是中文的格式,因此咱們通常都是默認的這種方式。sql

2)在咱們的服務器端解析顯示MySql數據就必定要使用服務器

 1 package com.test.an;
 2 
 3 import java.io.UnsupportedEncodingException;
 4 import java.sql.Connection;
 5 import java.sql.DriverManager;
 6 import java.sql.PreparedStatement;
 7 import java.sql.ResultSet;
 8 import java.sql.SQLException;
 9 
10 
11 public class TestCon1{
12     public static void main(String[] args) throws UnsupportedEncodingException 
13     {
14         Connection con = null;
15         String sql;
16         PreparedStatement pre;
17         ResultSet rs;
18         
19         try {
20             String driver="com.mysql.jdbc.Driver";
21             Class.forName(driver);
22             
23             String url="jdbc:mysql://localhost:3306/cui?useUnicode=true&characterEncoding=latin1";
24             con = DriverManager.getConnection(url, "root", "123456");
25             
26             sql = "select _id,username,password from test1" ;
27             pre = con.prepareStatement(sql);
28             
29             rs = pre.executeQuery(); 
30             while(rs.next()){
31                 int id = rs.getInt(1);
32                 String username =new String(rs.getString(2).getBytes("iso8859-1"));
33                 String password = rs.getString(3);
34                 
35                 System.out.println("id="+id+";username="+username+";password="+password);
36             }
37             con.close();
38         } catch (SQLException e) {
39             e.printStackTrace();
40         } catch (ClassNotFoundException e) {
41             e.printStackTrace();
42         }
43         
44     }
45 
46 }
相關文章
相關標籤/搜索