Results 1 to 2 of 2
- 03-19-2012, 11:20 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 1
- Rep Power
- 0
Is it possible to set entries from a database into an ArrayList?
So, basically, what I'd like for this to do is display the entirety of the catalog (currently, there are 48 entries) from my items database and make it possible to select an item, which will then be stored in a cart. My problem currently is that I don't know how to select an item from that catalog, though I'm having a few other issues as well. There are other pieces of code that I can post if it helps.
This is from my main menu.
Java Code:System.out.println("Welcome to Imaginaryplacethatsellscoolstuff.com!"); System.out.println("1. Browse Catalog"); System.out.println("2. Register"); System.out.println("3. Log in"); System.out.println("4. Forgot Password/Username"); System.out.println("5. Quit"); System.out.print("\nEnter your choice: "); choice = sc.numInput(); { // Choose an option switch (choice) { case 1:
And this is from my Catalog class.
JJava Code:{ try { // Connects to the items database Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/items", "root", "root"); // Connects to the users database String sql = ("SELECT idItems, Itemname,Itemprice, Itemrating, ItemGenre FROM items"); PreparedStatement prest = conn.prepareStatement(sql); prest.executeQuery(sql); ResultSet rs = prest.getResultSet(); System.out.println("B = Blu-ray, D = DVD, 3DB = 3D Blu-ray \n"); while (rs.next()) { int idItemsVal = rs.getInt("idItems"); String itemNameVal = rs.getString("Itemname"); int itemPriceVal = rs.getInt("Itemprice"); String itemRatingVal = rs.getString("ItemRating"); String itemGenreVal = rs.getString("ItemGenre"); System.out.println("" + idItemsVal + ". " + itemNameVal + ",\n" + itemGenreVal + "\n" + itemRatingVal + "\n$" + itemPriceVal + "\n"); } // Gets the int and String values for each item and prints them // out. rs.close(); prest.close(); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } } }
- 03-20-2012, 12:22 AM #2
Similar Threads
-
Load objects from DataBase into an ArrayList, then into a jComboBox in a GUI
By jeata in forum Advanced JavaReplies: 11Last Post: 08-08-2011, 04:01 PM -
removing repeated entries in arraylist
By ankit1801 in forum New To JavaReplies: 1Last Post: 04-15-2011, 06:34 AM -
update arraylist to mysql database
By sks in forum JDBCReplies: 11Last Post: 04-01-2011, 08:26 AM -
How can i store ArrayList objects in Access database
By frankycool in forum Advanced JavaReplies: 3Last Post: 11-04-2009, 06:55 AM -
Not able to delete MSSql database table entries
By wickedrahul9 in forum Advanced JavaReplies: 3Last Post: 10-17-2008, 12:14 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks