package cart.dbUtils; import cart.Item; import cart.ItemBuilder; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; //ItemDBUtil //copyright - Lawrence Truett - FluffyCat.com //date - June 13, 2003 - San Diego, CA public class ItemDBUtil { public static Item getItemForKey(long key, DBConnection dbc) { Item itemToReturn = null; try { Connection connection = dbc.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement( "Select * from item where key = ?"); preparedStatement.setLong(1, key); ResultSet resultSet = preparedStatement.executeQuery(); resultSet.first(); itemToReturn = ItemBuilder.buildItem(dbc, resultSet); resultSet.close(); preparedStatement.close(); } catch (java.sql.SQLException sqlException) { System.out.println("The following error occured in "+ "ItemDBUtil.getItemForKey: " + sqlException); } return itemToReturn; } public static ArrayList getAllItems(DBConnection dbc) { ArrayList itemsToReturn = new ArrayList(); try { Connection connection = dbc.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement("Select * from item"); ResultSet resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { itemsToReturn.add(ItemBuilder.buildItem(dbc, resultSet)); } resultSet.close(); preparedStatement.close(); } catch (java.sql.SQLException sqlException) { System.out.println("The following error occured in "+ "ItemDBUtil.getAllItems: " + sqlException); } return itemsToReturn; } public static void insert(long key, double price, String description, String type, String shortImage, String longImage, Connection connection) { try { PreparedStatement preparedStatement = connection.prepareStatement( "INSERT INTO item values(?,?,?,?,?,?);"); preparedStatement.setLong(1, key); preparedStatement.setDouble(2, price); preparedStatement.setString(3, description); preparedStatement.setString(4, type); preparedStatement.setString(5, longImage); preparedStatement.setString(6, shortImage); preparedStatement.execute(); preparedStatement.close(); } catch (java.sql.SQLException sqlException) { System.out.println("The following error occured in "+ "ItemDBUtil.insert: " + sqlException); } } }