【发布时间】:2012-09-24 04:53:34
【问题描述】:
我正在尝试更新已经存在的 MySQL 表中的列。这些值必须从文本文件中读取,然后必须插入到指定的列中。
我尝试了以下方法:
int counter=0;
while((fileLine=in.readLine())!=null)
{
System.out.println("Line read is: "+fileLine);
//execute db insertion
try {
//insert in the database
Query= "update db.table set col4=?"; //database
preparedStmt3 = DBConnection.con.prepareStatement(Query);
preparedStmt3.setString (1, fileLine);
preparedStmt3.executeUpdate();
System.out.println("Complete update statement for row: "+counter);
counter++;
} catch (Exception e) {
System.out.println("DB_Error:_"+ e.toString());
}
} //end while loop
我试图输出fileLine 值,它似乎是正确的,并且在每一轮循环中都会正确更改。但是,在我运行程序并检查数据库之后,我发现插入的值只是最后一行(并且这在所有记录中重复,这不是我应该做的,因此在记录中插入每一行) .这个问题的原因是什么?
编辑: 文本文件包含以下几行: 啊啊啊 bbb ccc ddd eee fff
我添加了一些printline 语句以查看调试后的运行输出如下:
而数据库只包含插入的最后一行
【问题讨论】:
-
您的列似乎与表中的其余列无关?那么你可能应该分开到不同的表。