在JAVA中,保存数值的Varchar DB列的正确映射是什么?



我在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.01030438800而不是1.0304388E9。我不能将数据类型从String更改为int,因为值也可能包含字符,也可能有空白值。

检查SQL,确保插入的值被引号括起来,以防止发生任何转换。

"update tablex set counterparty_company_number = '" + value + "'"

最新更新