gpt4 book ai didi

java - java中处理空字符串

转载 作者:行者123 更新时间:2023-12-01 13:10:30 26 4
gpt4 key购买 nike

我正在读取 CSV 文件并插入表中。但是当我将 completdate 设为 null 时,我想插入默认日期。我检查了此

if(COMPLETEDATE == null){
css.setString(24, "2013-06-12 00:00:00.0");
}else{
css.setString(24, COMPLETEDATE);
}

这是整个函数。

public void ImportCSV() {
String csvFile = "C:\\seema\\CSV Files\\2013\\August\\15.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
String PRODLINE,EMPID,EMPFNAME,SHIFT,STATIONID,CURWKDATE,OPCODE,OPDESC,STARTWORKTIME,ENDWORKTIME,PIECECNT,
BUNDLECNT,PIECERATE,SAM,SKILLLEVEL,DAILYBONUS,NORMALRATE,OVERTIMERATE,WORKDURATION,MONO,DESIGNCODE,
DESIGNCOLOR,DESIGNSIZE,COMPLETEDATE;

int i=0;
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
try {
PreparedStatement css = null;
css= conn.prepareStatement("exec uspInsertEWLProduction ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?");

String[] country = line.split(",");

PRODLINE=country[0];
EMPID=country[1];
EMPFNAME =country[2];
SHIFT=country[3];
STATIONID=country[4];
CURWKDATE =country[5];
OPCODE=country[6];
OPDESC=country[7];
STARTWORKTIME =country[8];
ENDWORKTIME=country[9];
PIECECNT=country[10];
BUNDLECNT =country[11];
PIECERATE=country[12];
SAM=country[13];
SKILLLEVEL =country[14];
DAILYBONUS=country[15];
NORMALRATE=country[16];
OVERTIMERATE =country[17];
WORKDURATION=country[18];
MONO=country[19];
DESIGNCODE =country[20];
DESIGNCOLOR=country[21];
DESIGNSIZE=country[22];
COMPLETEDATE =country[23];

if(i!=0) {
css.setString(1, PRODLINE);
css.setString(2, EMPID);
css.setString(3, EMPFNAME);
css.setString(4, SHIFT);
css.setString(5, STATIONID);
css.setString(6, CURWKDATE);
css.setString(7, OPCODE);
css.setString(8, OPDESC);
css.setString(9, STARTWORKTIME);
css.setString(10, ENDWORKTIME);
css.setString(11, PIECECNT);
css.setString(12, BUNDLECNT);
css.setString(13, PIECERATE);
css.setString(14, SAM);
css.setString(15, SKILLLEVEL);
css.setString(16, DAILYBONUS);
css.setString(17, NORMALRATE);
css.setString(18, OVERTIMERATE);
css.setString(19, WORKDURATION);
css.setString(20, MONO);
css.setString(21, DESIGNCODE);
css.setString(22, DESIGNCOLOR);
css.setString(23, DESIGNSIZE);

if(COMPLETEDATE == null) {
css.setString(24, "2013-06-12 00:00:00.0");
} else {
css.setString(24, COMPLETEDATE);
}
}
css.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
i++;
}
JOptionPane.showMessageDialog(null, "Data Imported Successfully");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

问题是即使 copmletedate 为空,其他部分也永远不会被执行。有什么解决办法吗?

最佳答案

您可以使用 Apache Commons 实用程序 StringUtils.isEmpty(CharSequence)检查 nullempty 字符串。顺便说一句,为什么你将日期存储为字符串,而不是日期?

if (StringUtils.isEmpty(COMPLETEDATE)) {
// set default date
} else {
// set COMPLETEDATE
}

关于java - java中处理空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22903465/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com