当前位置: 代码迷 >> Java Web开发 >> excel导入list后把数据传到数据库是乱码
  详细解决方案

excel导入list后把数据传到数据库是乱码

热度:87   发布时间:2016-04-13 22:12:00.0
excel导入list后把数据传入数据库是乱码
从客户端获取excel后根据里面的ID更新数据库里面的名字·接着就报错Unknown column ‘这里是乱码显示’ in 'field list'。
十一月 06, 2015 4:53:21 下午 com.jfinal.core.ActionHandler error
严重: /PrimaryCustomer/importExcel
com.jfinal.plugin.activerecord.ActiveRecordException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column '这里是乱码显示' in 'field list'
at com.jfinal.plugin.activerecord.DbPro.update(DbPro.java:279)
at com.jfinal.plugin.activerecord.DbPro.update(DbPro.java:290)
at com.jfinal.plugin.activerecord.Db.update(Db.java:214)
at com.ht.system.controllers.PrimaryCustomerController.importExcel(PrimaryCustomerController.java:341)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

我拼接的SQL语句的数据在控制台显示的是正确的:
Sql: update  primarycustomer set companyName =  喜明有限公司  where primaryId = 11
下面是我的相关代码··求大神帮忙。
	public void importExcel() {
System.out.println("importyExcel");

// 获取文件
UploadFile file = getFile("uploadExcel");
System.out.println(file.getFileName());
String path = file.getSaveDirectory() + file.getFileName();

// 处理导入数据
{
List<Map<Integer, String>> list = dealDataByPath(path); // 分析EXCEL数据
/*System.out.println(list); */
for(Map<Integer,String> map:list) { // 遍历取出的数据,并保存
String ch =map.get(1);
int mh =Integer.parseInt(map.get(0));
System.out.println(ch);
System.out.println(mh);
StringBuilder sql1=  new StringBuilder("update  primarycustomer set companyName =  ")
                                          .append(ch).append("  where ")
                                          .append("primaryId = ").append(mh);
String sql = sql1.toString();
Db.update(sql);
}
}
renderJson("success");

}
//解析excel
private List<Map<Integer, String>> dealDataByPath(String path) {
List<Map<Integer,String>> list = new ArrayList<Map<Integer,String>>();
// 工作簿
HSSFWorkbook hwb = null;
try {
hwb = new HSSFWorkbook(new FileInputStream(new File(path)));

HSSFSheet sheet = hwb.getSheetAt(0); // 获取到第一个sheet中数据

for(int i = 0;i<sheet.getLastRowNum() + 1; i++) {// 第二行开始取值,第一行为标题行

HSSFRow row = sheet.getRow(i); // 获取到第i列的行数据(表格行)

Map<Integer, String> map = new HashMap<Integer, String>();

for(short j=0;j<row.getLastCellNum(); j++) {

HSSFCell cell = row.getCell(j); // 获取到第j行的数据(单元格)

cell.setCellType(HSSFCell.CELL_TYPE_STRING);

map.put((int) j, cell.getStringCellValue());
}

list.add(map);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return list;
}

------解决思路----------------------
update  primarycustomer set companyName =  喜明有限公司  where primaryId = 11
中文要引号吧
  相关解决方案