我将以下代码作为从 CSV 文件读取并通过生成插入语句并执行它们将其内容存储到 DB 的方法的一部分。
....
try
{
while( (strLine = br.readLine()) != null)
{
query = baseQuery;
st = new StringTokenizer(strLine, ",");
while(st.hasMoreTokens())
{
token = st.nextToken();
if("TIMESTAMP".equals(values.get(tokenNumber)))
query += "'" + GeneralMethods.dateFormat(token) + "' , ";
else
query += "'" + token + "' , ";
}
query = query.substring(0, query.length()-2) + ")";
ds.insertData(query );
}
} catch (IOException e)
{
logger.error("IOException Occured while trying to read lines of CSV file: \n " + e.getMessage());
status = false;
}
一切正常,除了查询是用空值生成的。这将打印到日志中:
Failed to execute query: INSERT INTO c1_ds1 ( TIME , USER ) VALUES ('' , '' )
我相信问题出在这两行:
if("TIMESTAMP".equals(values.get(tokenNumber)))
query += "'" + GeneralMethods.dateFormat(token) + "' , ";
else
query += "'" + token + "' , ";
我已经在日志中打印了 (token) 变量,并且它正在从 CSV 文件中获取它应该做的值。有谁知道问题出在哪里?
我也试过这个:
query += "'"; query += token;