더북(TheBook)

제품이 MySQL 데이터베이스에 남아 있어 역시 맵(Map)으로 저장하는 메서드가 필요하다.


public static Map<Integer, String> loadItems() {
        Map<Integer, String> products = new HashMap<Integer, String>();
        try {
            Connection con = DriverManager.getConnection(“jdbc:mysql://localhost/apriori”,“root”,””);
            PreparedStatement pstmt = con.prepareStatement(“SELECT * FROM products”);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()) {
                products.put(new Integer(rs.getInt(“id”)), rs.getString(“productname”));
            }
            rs.close();
            pstmt.close();
            con.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
        return products;
    }

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.