我是 Java 的新手,我的问题是我正在尝试将货币汇率插入名为货币的 MySQL 数据库表中,该表包含两列“货币名称”和“汇率”。该程序从金融雅虎 API 获取货币汇率,我面临着将数据存储到数据库中的问题。
void checkRateAllAtEnd() throws Exception {
/** print message */
System.out.println("checkRateAllAtEnd :");
long start = System.nanoTime();
List<Callable<HashMap>> tasks = new ArrayList<Callable<HashMap>>();
for (final String ccy : CURRENCY) {
tasks.add(new Callable<HashMap>() {
public HashMap call() throws Exception {
return getRates(ccy);
}
});
}
ExecutorService executorPool = Executors.newCachedThreadPool();
final List<Future<HashMap>> listRates = executorPool.invokeAll(tasks, 3600, TimeUnit.SECONDS);
for (Future<HashMap> rate : listRates) {
HashMap ccyRate = rate.get();
System.out.println("Value of £1 in " + ccyRate.get("CCY") + " is " + ccyRate.get("RATE"));
}
我缺乏将值从 hashmap 传递到 preparedStatement 的能力。
String sql="update currency set currency_name='"+Value1+"'";
preparedstatement=connection.prepareStatement(sql);
preparedstatement.execute();
我让全类同学在此链接上充分发挥作用,以保持问题清晰>> linke <<<
这是我想存储到 MYSQL 中的程序的输出,理想情况下我想存储
ccyRate.get("CCY") and ccyRate.get("RATE")
checkRateAllAtEnd :
Value of £1 in AED is 5.9201
Value of £1 in AFN is 82.4359
Value of £1 in ALL is 171.681
Value of £1 in AMD is 650.4296
Value of £1 in ANG is 2.8849
Value of £1 in AOA is 154.4766
Value of £1 in ARS is 7.9438
Value of £1 in AUD is 1.5345
最佳答案
您的 addCurrency 方法是错误的 - 不仅您通过提供 preparedStatmenet 作为参数没有获得任何内容,而且在尝试关闭它时也没有获得任何内容,
因为此时它为空。
请向我们展示您遇到的确切错误/异常,我将相应地编辑我的答案。
关于java - 带 HashMap 的PreparedStatement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14188885/