Q:

How to Return all Resultset value from method. Java

I have a database that contains:
title of the show:
Avenger
Deadpool
etc.

And I have a method that gets all the title of the show
    public String shows2(){
    try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ticket","root",null);
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("select * from events");  
        while(rs.next()){
            String title = rs.getString("title");
            return "Shows: " +title;
        }
    }catch(Exception e){
        e.printStackTrace();
    }
    return "something";
}

But when I use this method it only returns one row/show
"Shows: Avenger"

A:

You'll have to return a List<String> instead of a single String.
public List<String> shows2(){
    List<String> result = new ArrayList<>();
    try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ticket","root",null);
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("select * from events");  
        while(rs.next()){
            String title = rs.getString("title");
            result.add(title);
        }
    }catch(Exception e){
        e.printStackTrace();
    }
    return result;
}

