0

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);
        }
    }

}
2
  • Are you wanting to store each record as a single array item? If so, how are you wanting it stored? As an object, string, etc? Commented Sep 14, 2019 at 13:46
  • I need as string Commented Sep 14, 2019 at 13:49

1 Answer 1

1

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);
            }

        }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

howto print this values like this inside do : -------------------------------------------------------------------------------------------------------------------------------------------------------- System.out.println("\nThe iterator values" + " of list are: "); while (itemList .hasNext()) { System.out.print(iter.next() + " "); }
An ArrayList implements the Collection interface. You can simply loop through it using a for(String s: itemList) {} contstruct. See here: docs.oracle.com/javase/tutorial/collections/interfaces/…
KellyM what I need is I need to pass this values to graph in Adjacency List ,is there any method I can do this...???
@James.R, unfortunately, it is a little hard for me to suggest an exact solution without knowing more about your specific requirements and surrounding code. I would advise looking at docs for ArrayList (docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html or perhaps other classes that implement Collection. If nothing in the standard Java libraries seems to fit your needs, take a look at Apache's Common Collections: commons.apache.org/proper/commons-collections

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.