Sajmon

Example of select

May 8th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. private static String QUERY_SELECT_ALL = "select * from Osoba";
  2.  
  3. public List<OsobaDTO> getAll() {
  4.    
  5.     List<OsobaDTO> data = new ArrayList<OsobaDTO>();
  6.     OsobaDTO member;
  7.  
  8.     Connection con = null;
  9.     PreparedStatement ps = null;
  10.     ResultSet rs = null;
  11.  
  12.     try {
  13.         con = getOracleConnection();
  14.         ps = con.prepareStatement(QUERY_SELECT_ALL);
  15.         rs = ps.executeQuery();
  16.         while (rs.next()) {
  17.             member = new OsobaDTO();
  18.             member.setId(rs.getInt(1));
  19.             member.setName(rs.getString(2));
  20.             member.setAdresa(rs.getString(3));
  21.             ...
  22.             data.add(member);
  23.         }
  24.         System.out.println("Persons imported successfully.");
  25.         return data;
  26.     }
  27.     catch (SQLException ex) {
  28.         Logger.getLogger(OracleDodavatelDAO.class.getName()).log(Level.SEVERE, null, ex);
  29.         return null;
  30.     }
  31.     finally {
  32.         if (con != null) {
  33.             try {
  34.                 con.close();
  35.             }
  36.             catch (SQLException ex) {
  37.                 Logger.getLogger(OracleDodavatelDAO.class.getName()).log(Level.SEVERE, null, ex);
  38.             }
  39.         }
  40.     }
  41.    
  42. }
Advertisement
Add Comment
Please, Sign In to add comment