1

我是 Java 的新手,我的问题是我试图将货币汇率插入我的 MySQL 数据库表中,称为货币,其中包含两列 Currency_name 和 rate。该程序从财务 yahoo 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") 和 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
4

1 回答 1

0

您的 addCurrency 方法是错误的 - 通过提供preparedStatmenet 作为参数,您不仅没有获得任何东西,而且在尝试关闭它时您也没有获得任何东西,
因为此时它为空。
请向我们展示您遇到的确切错误/异常,我将相应地编辑我的答案。

于 2013-01-07T02:04:48.660 回答