我在mysql中创建了一个数据库,将数据库的排序规则和每个表的字符集设置为utf16
我选择utf16的原因是,我想存储所有类型的数学方程,比如ΦB=B.A=BAcosθβ(实际上在ΦB中,B是上标)
不仅上面的公式,而且数据库可能有任何类型的希腊字母,如链接所示
我试图通过阅读微软excel文件插入数据在执行查询之前,java代码中的一切似乎都是正确的,但在插入数据库后,它显示为?B=B.A=BAcos?
根据utf16支持的字符集链接,上面的等式对我来说应该很好但事实并非如此。
知道吗?
数据库表架构
mysql> SHOW CREATE TABLE formulae;
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| formulae | CREATE TABLE `formulae` (
`formulaeid` int(11) NOT NULL AUTO_INCREMENT,
`formulae` text CHARACTER SET utf16,
`concept_conceptid` int(11) NOT NULL,
PRIMARY KEY (`formulaeid`),
KEY `fk_formulae_concept_idx` (`concept_conceptid`),
CONSTRAINT `fk_formulae_concept` FOREIGN KEY (`concept_conceptid`) REFERENCES `concept` (`conceptid`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf16 COLLATE=utf16_unicode_ci |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
代码:
final static int CONCEPT_SHEET_NUMBER = 0;
final static int FORMULAE_SHEET_NUMBER = 1;
//Reads xl file
public void readProductArgExcel(File inputFile) throws IOException {
FileInputStream file = new FileInputStream(inputFile);
XSSFWorkbook workBook = new XSSFWorkbook(file);
boolean isError = false;
for (int sheetNumber = 0; sheetNumber < workBook.getNumberOfSheets(); sheetNumber++) {
XSSFSheet sheet = workBook.getSheetAt(sheetNumber);
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
isError = false;
Row row = sheet.getRow(i);
List<Object> columnValues = new ArrayList<Object>();
for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
Cell cell = row.getCell(j);
if (!(cell == null)) {
if (cell.getCellType() == 0) {
Integer conceptId = (int) cell
.getNumericCellValue();
columnValues.add(conceptId);
}
if (cell.getCellType() == 1) {
String rowString = cell
.getStringCellValue();
columnValues.add(rowString);
}
switch (sheetNumber) {
case CONCEPT_SHEET_NUMBER:
break;
//XL file has two sheets, second sheet has few formulae
case FORMULAE_SHEET_NUMBER:{
if(j == 1){
if(columnValues.size() >= 2)
//this xl sheet has column 1 - formula, column2 - integer
insertFormulae((String)columnValues.get(0),(Integer)columnValues.get(1));
}
break;
}
default:
break;
}
}
}
}
System.out.println(sheet.getSheetName());
}
}
public static Connection getConnection() throws InstantiationException,
IllegalAccessException, ClassNotFoundException, SQLException {
Connection connection = null;
/*Tried with but didn't work
jdbc:mysql://localhost:3306/formulaeDB?useUnicode=true&characterEncoding=utf16
but it perfectly works with
jdbc:mysql://localhost:3306/formulaeDB?useUnicode=true&characterEncoding=utf8
*/
String connectionURL = "jdbc:mysql://localhost:3306/formulaeDB";
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root","root");
/*The below code throws an exception
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Variable 'character_set_client' can't be set to the value of 'utf16'
*/
String queryString = "SET NAMES utf16";
PreparedStatement preparedStatement = connection.prepareStatement(
queryString, PreparedStatement.RETURN_GENERATED_KEYS);
preparedStatement.executeUpdate();
return connection;
}
//To insert into table
public static void insertFormulae(String formula,Integer conceptId){
String queryString = "INSERT INTO formulae(formulae,concept_conceptid) VALUES(?,?)";
Connection connection;
try {
connection = getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(
queryString, PreparedStatement.RETURN_GENERATED_KEYS);
preparedStatement.setString(1, formula);
preparedStatement.setInt(2, conceptId);
preparedStatement.executeUpdate();
} catch (InstantiationException | IllegalAccessException
| ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String st[]) throws IOException {
ExcelParser r = new ExcelParser();
r.readProductArgExcel(new File(
“c://data/Formulae.xlsx"));
}
您需要确保从excel读取的数据是正确的,您的表已经正确创建,您的连接已经正确设置,并且您从excel读取数据并将其写入数据库的代码工作正常。
如果所有这些似乎都没有问题,那么接下来要怀疑的是,也许数据库中的数据很好,而你观察数据的方法是错误的。
具体来说,如果您使用mysql控制台命令,那么您需要确保控制台能够正确显示从数据库中提取的unicode字段。
图形数据库管理工具通常不会有这些缺点,那么为什么不使用MySQL附带的"MySQL Workbench"工具呢?
这应该可以解决您的unicode问题。
您仍然会遇到另一个问题:格式化。
unicode几乎不支持上标和下标(你可以有几个上标数字,仅此而已),所以excel在单元格文本中使用特殊格式来表示上标和下标。
不幸的是,当你以这种方式读取单元格时,excel并没有为你提供这种格式。这似乎是一个很难解决的问题,但这里似乎有某种解决方案:从excel单元格中读取富文本
我听说没有任何产品公开utf16。Utf8是交换的常用格式。MySQL的utf8mb4甚至更好,因为它允许使用4字节的utf8字符(而utf8没有)。
将所有设置更改为utf8,看看它们是否能更好地互操作。
如果您已经有包含utf16字符的数据文件或数据库,那么问题就更为棘手了。