Reputation: 1163
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:
Upvotes: 0
Views: 10191
Reputation: 1
It should be like this:
products.add(bean)
bean is the object of ProductBean
class ProductBean bean = new ProductBean();
Upvotes: 0
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
Reputation: 328598
Your ProductBean
class does not have a constructor that takes three strings as parameters.
Upvotes: 2