Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public List<Car> getAllCars() throws SQLException {
- Connection conn = null;
- Statement stm = null;
- String getAllCarsQuery = "SELECT * FROM CARS";
- List<Car> cars = null;
- try {
- conn = DBConnection.getConnection();
- stm = conn.createStatement();
- ResultSet rs = stm.executeQuery(getAllCarsQuery);
- cars = new ArrayList<>();
- while(rs.next()) {
- Car car = new Car();
- car.setId(rs.getInt("ID"));
- car.setBrand(rs.getString("BRAND"));
- car.setModel(rs.getString("MODEL"));
- car.setYear(rs.getInt("YEAR"));
- car.setCarNumber(rs.getString("CAR_NUMBER"));
- car.setColor(rs.getString("COLOR"));
- cars.add(car);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- if(stm != null) {
- stm.close();
- }
- if(conn != null) {
- conn.close();
- }
- }
- return cars;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement