title: JDBC - Retrieving Resultset into a list
body: <p>The aim of the code here is to Read a record by selecting a column.</p>
  
  <p>I have following columns in the SQL DB.</p>
  
  <pre><code>------------------------------
  |id | Topic | Comment | Time |
  ------------------------------
  </code></pre>
  
  <p>What I want to do is:-</p>
  
  <ol>
  <li>Read the DB</li>
  <li>Able to select rows by Topic</li>
  <li>Show or Return the values of selected rows by Topic </li>
  </ol>
  
  <p>The Topic could be duplicated, however the duplicated values should not been seen during the process of selection but the values of all column should be retrieved (as well as duplicated ones).</p>
  
  <pre><code>public class ReadRecordTopic{
         // JDBC driver name and database URL
         static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
         static final String DB_URL = "jdbc:mysql://localhost:3306/STUDENTS?user=root&amp;password=";
  
         //  Database credentials
         //static final String USER = "username";
         //static final String PASS = "password";
  
         public static void main(String[] args) {
  
             Connection conn = null;
             Statement stmt = null;
  
             try{
            //STEP 2: Register JDBC driver
            Class.forName("com.mysql.jdbc.Driver");
  
            //STEP 3: Open a connection
            System.out.println("Connecting to a selected database...");
            conn = DriverManager.getConnection(DB_URL);
            System.out.println("Connected database successfully...");
  
            //STEP 4: Execute a query
            System.out.println("Creating statement..." + "\n");
            stmt = conn.createStatement();
  
            String sql = "SELECT id, Topic, Comment, Time FROM Registration";
            ResultSet rs = stmt.executeQuery(sql);
  
            //STEP 5: Extract data from result set
            while(rs.next()){
               //Retrieve by column name
               int id  = rs.getInt("id");
               String Topic = (String) rs.getString("Topic");
               String Comment = rs.getString("Comment");
               String Time = rs.getString("Time");
  
                String menu [] = Topic.split(",");
                Object[] selectionValues = menu;
                String initialSelection = "";
  
                Object selection = JOptionPane.showInputDialog(null,
                              "Please select the Topic.", "Reseach Forum Menu",
                              JOptionPane.QUESTION_MESSAGE, null, selectionValues,
                              initialSelection);
  
               String a = "Research Topic: " + Topic + "\n";
               String b = "[" + Time + "]" + " Comment:" + Comment + "\n";
  
               if(selection == Topic){JOptionPane.showMessageDialog(null, a + b);
               }     
          }
  
            rs.close();
         }catch(SQLException se){
            //Handle errors for JDBC
            se.printStackTrace();
         }catch(Exception e){
            //Handle errors for Class.forName
            e.printStackTrace();
         }finally{
            //finally block used to close resources
            try{
               if(stmt!=null)
                  conn.close();
            }catch(SQLException se){
            }// do nothing
            try{
               if(conn!=null)
                  conn.close();
            }catch(SQLException se){
               se.printStackTrace();
            }//end finally try
         }//end try
         System.out.println("Goodbye!");
      }//end main
      }//end JDBCExample
  </code></pre>