Database encoding table encoding IDE encoding must be consistent, can solve the problem
Scenario: Import this data into the database and get the first letter of the city name Pinyin
Import data from Excel to MySQL, using the JXL library
The PINYIN4 library was used, but a bug was found
Import java.io.UnsupportedEncodingException;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.SQLException;
Import java.sql.Statement;
public class Db {
static Connection Connection = null;
static Statement Statement = null;
public static Connection Getcon () {
try {
Class.forName ("Com.mysql.jdbc.Driver");
String url = "Jdbc:mysql://localhost/my?characterencoding=utf-8";
String uname = "root";
String pwd = "root";
Connection = drivermanager.getconnection (URL, uname, pwd);
statement = Connection.createstatement ();
} catch (ClassNotFoundException e) {
return null;
} catch (SQLException e) {
return null;
}
return connection;
}
public static void Clores (Connection Connection, Statement Statement) {
if (statement! = NULL) {
try {
Statement.close ();
} catch (SQLException e) {
E.printstacktrace ();
}
statement = NULL;
}
if (connection! = null) {
try {
Connection.close ();
} catch (SQLException e) {
E.printstacktrace ();
}
connection = null;
}
}
public void Add (string cityname, String citypin) {
String sql =
"Insert City (Cityname,citypin) VALUES ('" + CityName + "', '" + Citypin + "')";
SYSTEM.OUT.PRINTLN (SQL);
try {
Statement.executeupdate (SQL);
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Import Java.io.File;
Import java.io.IOException;
Import java.util.ArrayList;
Import java.util.List;
Import JXL. Cell;
Import JXL. Sheet;
Import JXL. Workbook;
Import jxl.read.biff.BiffException;
Import Net.sourceforge.pinyin4j.PinyinHelper;
public class Input {
public static void Main (string[] args) {
Db.getcon ();
--------------------------------------------------------------------------------------------------------------- -----------------------------
Tested the following code output sing estimate the problem with this library
string[] s2 = Pinyinhelper.totongyongpinyinstringarray ("Xingcheng". CharAt (0));
System.out.println (S2[0]);
------------------------------------------------------------------------------------------------------------ -------------------------------
File File = new file ("City.xls");
Workbook Workbook;
list<string> cities = new arraylist<string> ();
char[] Citypin = new char[700];
try {
Workbook = workbook.getworkbook (file);
Sheet Sheet = workbook.getsheet (0);
cell[] Cell = Sheet.getcolumn (2);
for (int i = 0; i < cell.length; i++) {
Cities.add (cell[i].getcontents ());
}
for (int i = 0; i < cities.size (); i++) {
Char cs = cities.get (i). charAt (0);
String[] s = Pinyinhelper.totongyongpinyinstringarray (CS);
Citypin[i] = S[0].charat (0);
System.out.println (Citypin[i]);
}
} catch (Biffexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
for (int i = 0; i < cities.size (); i++) {
New Db (). Add (Cities.get (i), string.valueof (Citypin[i]));
}
Db.clores (db.connection,db.statement);
}
}
Chinese database garbled Excel import data to MySQL problem