James.R
James.R

Reputation: 23

How to get(retrieve) mysql database data to array list using Java & print using indexes

I need to get(retrieve) mysql databse data to java array list and print one by one using indexes...

I already tried normal do-while for retrieve, but i need to take values to array list...

public class Portdetails 
{

    public static void main(String[] args) {
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();

                    try ( Connection cn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/airportinfo","root",""))
                    {
                        Statement smt=(Statement) cn.createStatement();

                        //query to display all records from table employee
                        String q="SELECT * FROM airportinfo";

                        //to execute query
                        ResultSet rs=smt.executeQuery(q);

                        //to print the resultset on console
                        if(rs.next()){
                            do{
                                System.out.println(rs.getString(1)+"--"+rs.getString(2)+"--"+rs.getString(3)+"--"+rs.getString(4));
                            }while(rs.next());
                        }
                        else{
                            System.out.println("Record Not Found...");
                        }
                    }
        }
        catch(Exception e)
                {
            System.out.println(e);
        }
    }

}

Upvotes: 0

Views: 2068

Answers (1)

KellyM
KellyM

Reputation: 2522

If I am understanding you correctly, and presuming your code is otherwise working as anticipated, you simply need to create an ArrayList and add the items in the loop:

public static void main(String[] args) {
            List<String> itemList = new ArrayList<String>();
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();

                try (Connection cn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/airportinfo", "root",
                        "")) {
                    Statement smt = (Statement) cn.createStatement();

                    // query to display all records from table employee
                    String q = "SELECT * FROM airportinfo";

                    // to execute query
                    ResultSet rs = smt.executeQuery(q);

                    // to print the resultset on console
                    if (rs.next()) {
                        do {
                            String resString = rs.getString(1) + "--" + rs.getString(2) + "--" + rs.getString(3) + "--"
                                    + rs.getString(4);
                            itemList.add(resString);
                        } while (rs.next());
                    } else {
                        System.out.println("Record Not Found...");
                    }
                }
            } catch (Exception e) {
                System.out.println(e);
            }

        }
    }

Upvotes: 1

Related Questions