我在postgres DB的表中定义了一个列:
counterparty_company_number character varying(255)
我编写了一个JAVA代码,用excel文件的相应列中的值填充此表。在示例excel文件中,此列有8行,其中一些是空行,如:
counterparty_company_number
blank
234567
345678
456789
567890
1.03E+09
blank
blank
在我的模型类中,我已将此列的属性定义为:
private String counterparty_company_number;
在我的代码中,我在保存时用空字符串替换了空白值。从excel读取数据并保存在DB中后,DB表的相应列中的值显示为:
""
"234567.0"
"345678.0"
"456789.0"
"567890.0"
"1.0304388E9"
""
""
这是代码中的代码片段,它读取excel文件并在DB中填充表格:
..
//reads excel file and stores values in a list
FileInputStream file = new FileInputStream(new File(filepath));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet xssfSheet = workbook.getSheetAt(0);
if (xssfSheet.getLastRowNum() != 0) {
for (int i = 1; i <= xssfSheet.getLastRowNum(); i++) {
Row row = xssfSheet.getRow(i);
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
if (cell != null) {
valueHolder.add(cell.toString());
} else {valueHolder.add("");
}
}
}
}
..
//sets attribute values in model class
if (!valueHolder.isEmpty() && valueHolder != null) {
for (int i = 0; i < valueHolder.size(); i += 10) {
CardsCounterparty ccp = new CardsCounterparty();
..
ccp.setCounterparty_company_number(valueHolder.get(i+3).toString());
..
counterpartyList.add(ccp);//counterpartyList is a list of counterparty objects
}
}
return counterpartyList;//this list is passed to the code that will save the objects in DB table
}
..
//code to persist in DB
Configuration c = new Configuration();
c.configure("/hibernate.cfg.xml");
SessionFactory sf = c.buildSessionFactory();
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
try {
for (int i = 0; i < counterparties.size(); i++) {//counterparties is the list of all counterparty objects to be saved
CardsCounterparty ccp = counterparties.get(i);
s.save(ccp);
}
tx.commit();
s.close();
} catch (Exception e) {
tx.rollback();
}
}
我想知道如何以正确的数字格式获取值,例如 234567 而不是 234567.0 和 1030438800 而不是 1.0304388E9 即可。我无法将数据类型从字符串更改为 int ,因为值也可能包含字符,并且可能还有空值。
答案 0 :(得分:0)
检查您的SQL以确保插入的值被引号括起,以防止发生任何转换。
"update tablex set counterparty_company_number = '" + value + "'"