Пример #1
0
  public static LinkedList<User> getAll() {
    String query = "SELECT username, password, email, number, isIT FROM tomcat_user";
    LinkedList<User> items = new LinkedList<>();

    try (Connection connection = Config.getConnection();
        Statement statement = connection.createStatement();
        ResultSet result = statement.executeQuery(query); ) {
      while (result.next()) {
        items.add(
            new User(
                result.getString("username"),
                result.getString("password"),
                result.getString("email"),
                result.getString("number"),
                query));
      }
    } catch (SQLException e) {
      System.err.println("Couldn't get items from database.");
    }

    return items;
  }
Пример #2
0
  public static List<Movie> getAllMovies() {
    String query = "SELECT * FROM movie";
    List<Movie> movies = new LinkedList<>();
    try (Connection connection = Config.getConnection(); // step 1
        Statement statement = connection.createStatement(); // step 2
        ResultSet result = statement.executeQuery(query); ) { // step 3 and 4
      while (result.next()) { // step 5
        Movie movie = new Movie();
        // you should be validating the following,
        // this is just an example to get you started
        movie.setName(result.getString(1));
        movie.setYear(result.getDate(2).getYear());
        movie.setUrl(result.getString(3));
        movies.add(movie);
      }
    } catch (SQLException e) {
      System.err.println(e.getMessage());
      System.err.println(e.getStackTrace());
    }

    return movies;
  }