Java 从字符串数组更新mysqldatabase

Java 从字符串数组更新mysqldatabase,java,mysql,database,Java,Mysql,Database,我是mysql新手,从java程序更新sql数据库时遇到问题。java程序执行所有计算并将要更新的值存储在大小为2000的字符串数组中。我的sql数据库包含以下列 名价高低 我的字符串数组存储价格,高,低,用逗号分隔。(我实际上查询了yahoo finance并将csv文件存储在字符串中)。 现在我需要使用字符串中的数据更新价格,高,低。我该怎么做。或者可以直接将雅虎财经返回的数据上传到我的数据库中 代码 我用来更新单个股票的代码 Statement stmt = conn.createSta

我是mysql新手,从java程序更新sql数据库时遇到问题。java程序执行所有计算并将要更新的值存储在大小为2000的字符串数组中。我的sql数据库包含以下列 名价高低 我的字符串数组存储价格,高,低,用逗号分隔。(我实际上查询了yahoo finance并将csv文件存储在字符串中)。 现在我需要使用字符串中的数据更新价格,高,低。我该怎么做。或者可以直接将雅虎财经返回的数据上传到我的数据库中

代码

我用来更新单个股票的代码

 Statement stmt = conn.createStatement() ;
   // Execute the Update
  int rows = stmt.executeUpdate( "UPDATE tablename SET id = 9842 WHERE name='name'" ) 

构造一个准备好的语句:

String sql = "update stock set price = ?, high = ?, low = ? where name = ?"; 
PreparedStatement stmt = connection.prepareStatement(sql);
while ((inputLine = in.readLine()) != null) {  
    StockLine line = parseStockLine(inputLine);
    stmt.setBigDecimal(1, line.getPrice());
    stmt.setBigDecimal(2, line.getHigh());
    stmt.setBigDecimal(3, line.getLow());
    stmt.setString(4, line.getName());
    stmt.executeUpdate();
}
然后遍历CSV文件的行,并将每行解析为包含4个字段的数据结构(或简单数组):

while ((inputLine = in.readLine()) != null) {  
    StockLine line = parseStockLine(inputLine);
}
对于每一行,绑定参数并执行以下语句:

String sql = "update stock set price = ?, high = ?, low = ? where name = ?"; 
PreparedStatement stmt = connection.prepareStatement(sql);
while ((inputLine = in.readLine()) != null) {  
    StockLine line = parseStockLine(inputLine);
    stmt.setBigDecimal(1, line.getPrice());
    stmt.setBigDecimal(2, line.getHigh());
    stmt.setBigDecimal(3, line.getLow());
    stmt.setString(4, line.getName());
    stmt.executeUpdate();
}
要加快速度,您可以使用批处理:

while ((inputLine = in.readLine()) != null) {  
    StockLine line = parseStockLine(inputLine);
    stmt.setBigDecimal(1, line.getPrice());
    stmt.setBigDecimal(2, line.getHigh());
    stmt.setBigDecimal(3, line.getLow());
    stmt.setString(4, line.getName());
    stmt.addBatch();
}
stmt.executeBatch();
使用加载数据填充


我遇到了一个问题。当我尝试更新一个带有负号的double时,它会抛出以下错误:您的SQL语法中有一个错误;检查与您的MySQL服务器版本对应的手册,了解与原始问题无关的第1行“change=-0.1 where SYMBOL=”附近使用的正确语法,并且不显示代码。请试着自己理解它,如果你不理解,用你的代码问另一个问题。