Question: How would I load data from a database into a HashMap? (I'm talking about java specifically in NetBeans by the way). Say for example, I

How would I load data from a database into a HashMap? (I'm talking about java specifically in NetBeans by the way).

Say for example, I have a series of products. The database has the following columns:

Product ID

Product Manufacturer

Product Name

Product Year

I want to load the information under those columns into a hashmap. How would I do so? This is what I currently have but it doesnt work

public void loadHash(Product product) { HashMap productOrder = hash.getProductMap(); //this is calling a method from another class FYI try { con = DriverManager.getConnection(URL); Statement st = con.createStatement(); String sql = "SELECT * FROM PRODUCTS"; ResultSet rs =st.executeQuery(sql); while (rs.next()) { Product foundProduct = new Product(rs.getString("PRODUCTID"),rs.getString("PRODUCTMANUFACTURER"),rs.getString("PRODUCTMODEL"),rs.getString("PRODUCTYEAR")); productOrder.put(foundProduct.getProductID(), product); } } catch (Exception e) { } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!