prakash_d22
prakash_d22

Reputation: 1163

adding data to hashmap from database

Hello I am creating an application where I can store data in a Hashmap and List for a shopping cart module.But I want to fetch this data from my MS-Access database.I tried the following code but its not compiling.Please give me guidance.

Code:

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Program {
    public static void main(String [] args){}

    public static HashMap getProductsAsMap() {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:pd");
            ResultSet rs = null;
            Statement st = con.createStatement();
            String sql = ("select * from products");
            rs=st.executeQuery(sql);
            while (rs.next()) { 
                HashMap<String, ProductBean> products= new HashMap<String, ProductBean>();
                String name=rs.getString("pname");
                String desc=rs.getString("pdesc");
                String image=rs.getString("pimage");
                products.put("P1", new ProductBean(name,desc,image));
                return products;
            }
            rs.close();
            st.close();
            con.close();
        }
        catch(Exception e){}   
    }

    public static List getProductsAsList() {
        List<ProductBean> products = new ArrayList<ProductBean>();
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:pd");
            ResultSet rs = null;
            Statement st = con.createStatement();
            String sql = ("select * from products");
            rs=st.executeQuery(sql);
            while (rs.next()) { 
                String name=rs.getString("pname");
                String desc=rs.getString("pdesc");
                String image=rs.getString("pimage");
                products.add(new ProductBean(name,desc,image));
            }
            rs.close();
            st.close();
            con.close();
        }
        catch(Exception e){}
        return products;  
    }
}

I get 2 errors as follows:

enter image description here

Upvotes: 0

Views: 10191

Answers (3)

It should be like this:

products.add(bean)

bean is the object of ProductBean class ProductBean bean = new ProductBean();

Upvotes: 0

Reimeus
Reimeus

Reputation: 159754

As the error shows, the class ProductBean is missing a constructor with signature with 3 Strings - ProductBean(String, String, String).

Check this class again to match the existing constructor or add a new one to match your code here.

Upvotes: 1

assylias
assylias

Reputation: 328598

Your ProductBean class does not have a constructor that takes three strings as parameters.

Upvotes: 2

Related Questions