Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package org.apache.geronimo.samples.inventory.dao;
 
 import java.util.List;
 
 
 
 
 public class ItemDAO {
 
     private final DataSource ds;
 
     public ItemDAO() throws ItemException {
         try {
             InitialContext ctx = new InitialContext();
              = (DataSourcectx.lookup("java:comp/env/jdbc/InventoryDS");
         } catch (NamingException e) {
             throw new ItemException(e);
         }
     }
 
     public List<ItemgetItems() throws ItemException {
         ArrayList<Itemitems = new ArrayList<Item>();
 
         try {
             Connection con = .getConnection();
             try {
 
                 PreparedStatement pStmt = con.prepareStatement("SELECT i.item_id as item_id, i.item_name as item_name, i.description as description, im.quantity as quantity FROM item i, item_master im WHERE im.item_id = i.item_id");
                 try {
                     ResultSet rs = pStmt.executeQuery();
                     try {
                         while (rs.next()) {
                             Item item = new Item();
 
                             item.setItemNo(rs.getString("item_id"));
                             item.setItemName(rs.getString("item_name"));
                             item.setDescription(rs.getString("description"));
                             item.setQuantity(rs.getInt("quantity"));
 
 
                             items.add(item);
                         }
                     } finally {
                         rs.close();
                     }
                 } finally {
                     pStmt.close();
                 }
 
             } finally {
                 con.close();
             }
         } catch (SQLException e) {
             throw new ItemException(e);
         }
 
         return items;
     }
 
     public int getQOH(String itemNothrows ItemException {
 
         int qoh = 0;
         try {
             Connection con = .getConnection();
             try {
 
                 PreparedStatement pStmt = con.prepareStatement("SELECT quantity FROM item_master WHERE item_id =? ");
                 try {
                     pStmt.setString(1, itemNo);
 
                     ResultSet rs = pStmt.executeQuery();
                     try {
                        if (rs.next()) {
                            qoh = rs.getInt("quantity");
                        }
                    } finally {
                        rs.close();
                    }
                } finally {
                    pStmt.close();
                }
            } finally {
                con.close();
            }
        } catch (SQLException e) {
            throw new ItemException(e);
        }
        return qoh;
    }
    public boolean isItemIdExists(String itemNothrows ItemException {
        boolean isExists = false;
        try {
            Connection con = .getConnection();
            try {
                PreparedStatement pStmt = con.prepareStatement("SELECT item_id FROM item WHERE item_id =? ");
                try {
                    pStmt.setString(1, itemNo);
                    ResultSet rs = pStmt.executeQuery();
                    try {
                        if (rs.next()) {
                            isExists = true;
                        }
                    } finally {
                        rs.close();
                    }
                } finally {
                    pStmt.close();
                }
            } finally {
                con.close();
            }
        } catch (SQLException e) {
            throw new ItemException(e);
        }
        return isExists;
    }
    public void updateQOH(String itemNoint qohthrows ItemException {
        try {
            Connection con = .getConnection();
            try {
                PreparedStatement pStmt = con.prepareStatement("UPDATE item_master SET quantity=? WHERE item_id=?");
                try {
                    pStmt.setInt(1, qoh);
                    pStmt.setString(2, itemNo);
                    pStmt.executeUpdate();
                } finally {
                    pStmt.close();
                }
            } finally {
                con.close();
            }
        } catch (SQLException e) {
            throw new ItemException(e);
        }
    }
    public void addItem(Item itemthrows ItemException {
        try {
            Connection con = .getConnection();
            try {
                PreparedStatement pStmt = con.prepareStatement("INSERT INTO item VALUES(?, ?, ?)");
                try {
                    pStmt.setString(1, item.getItemNo());
                    pStmt.setString(2, item.getItemName());
                    pStmt.setString(3, item.getDescription());
                    pStmt.executeUpdate();
                    pStmt = con.prepareStatement("INSERT INTO item_master VALUES(?, ?)");
                    pStmt.setString(1, item.getItemNo());
                    pStmt.setInt(2, 0);
                    pStmt.executeUpdate();
                } finally {
                    pStmt.close();
                }
            } finally {
                con.close();
            }
        } catch (SQLException e) {
            throw new ItemException(e);
        }
    }
New to GrepCode? Check out our FAQ X