0

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

2
  • 1
    The answer is in the screen shot itself. Cannot find symbol constructor. I am guessing you are very new to programming! You should definitely pick up some beginner books on Java. Commented Sep 15, 2012 at 12:31
  • yes i corrected that and now its showing missing return statement. Commented Sep 15, 2012 at 12:53

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

It should be like this:

products.add(bean)

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.