Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

parse hashmap to sql preparedstatement

$
0
0
I am trying to parse hashmap values of country name and country rate to PreparedStatement , but I am unable to store the values. here is a link to my full class working without any errors you run it easily My full working class mysql >driver


here is my attempt

void checkRateSequential(String getRates) throws Exception {
        Connection connection =null;
        Statement statement =null;
        PreparedStatement preparedstatement = null ;
                
        System.out.println("checkRateSequential :");
                   /** String variable ccy consists of Currency elements */
                long start = System.nanoTime();
                for (String ccy : CURRENCY) {
                     /**  HashMap ccyRate consists of ccy elements */
                        HashMap ccyRate = getRates(ccy);
                         /** print elements of ccy and also ccyRate.get */
                             System.out.println("Value of £1 in " + ccy + " is " + ccyRate.get("RATE"));
                             String C_name = ccy; 
                             float C_rate = (float) ccyRate.get("Rate");
                try{
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mohamed","root", "root");
                
                String sql = "INSERT INTO currency VALUES(" + C_name + ", " +C_rate+" )";      
                
                preparedstatement=connection.prepareStatement(sql); 
                preparedstatement.execute();
                //  statement.execute("INSERT INTO currency(currency_code, rate) VALUES (?,?)");
                }
                catch(ClassNotFoundException error)
                {
                    System.out.println("error"+ error.getMessage());
                }
                catch(SQLException SQLerror)
                {
                     System.out.println("error"+ SQLerror.getMessage());
                }
                finally 
                {
                if(connection != null)try{connection.close();} catch(SQLException ignor){}
                if(connection != null)try{statement.close();} catch(SQLException ignor){}
                 }
                
                }



here are some of the output in systemprint I want to insert into the currency table
 
 AED 5.8845
 ALL 171.3793
 AMD 650.4483
 EUR 1.2272
 USD 1.602


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>