码迷,mamicode.com
首页 > 数据库 > 详细

中文 数据库 乱码 excel中导入数据到mysql 问题

时间:2015-07-19 19:50:43      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

数据库编码  表编码  ide编码 必须一致,即可解决问题

 

 

场景:把这些数据导入数据库,并且得到城市名称拼音的首字母

从excel中导入数据到mysql,使用了jxl这个库   

使用了pinyin4这个库,但是发现有bug

 

 

 技术分享

 

 

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();

--------------------------------------------------------------------------------------------------------------------------------------------

测试了下面代码    输出sing   估计这个库有问题

String[] s2 = PinyinHelper.toTongyongPinyinStringArray("兴城".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);
}

}

中文 数据库 乱码 excel中导入数据到mysql 问题

标签:

原文地址:http://www.cnblogs.com/nongmei/p/4658968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!