Chinese database garbled Excel import data to MySQL problem

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.