1

I need to get all the entries of a column in a database (mysql server) by using java code. Please find the code below:

public class DBConnection {

    private String name = null;
    private String path = null;

    public void DbValues(){

        try {
            Class.forName("driver");
            Connection con = DriverManager.getConnection(
                    "jdbc:sqlserver:..",
                    "username",
                    "password");
            if (!con.isClosed()) {
                Statement st = con.createStatement();
                ResultSet rs = st.executeQuery("select name, path from [testtable].[tbl_details]");

                while (rs.next()) {
                    name = rs.getString("name");
                    path = rs.getString("path");
                }
                con.close();
            } else
                System.out.println("failed");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace(); }
    }

        public String getName() {
            return this.name;
        }

        public String getPath() {
            return this.path;
        }
}

//in main class

public class DBTest {
    public static void main(String[] args) { 

        DBConnection dbcon = new DBConnection();
        dbcon.DbValues();
        String path = dbcon.getPath();
        System.out.println("value is .." +path);
    }
}

Here it displays only last value of path. Means if it has 20 entry, it will display only 20th value of "path" I need to get all the entries in that particular column. Please help.

1 Answer 1

1

You are replacing the values of name and path variable for each entry. Therefore finally you will get the values of last entry.

Add values to some data structure or change your logic

P.S

You can have 2 arraylist for names and paths or one single arraylist of arrays. e.g.

ArrayList<String[]> values = new ArrayList<String[]>();
while (rs.next()) {
     values.add(new String[]{rs.getString("name"), rs.getString("path")});
}
Sign up to request clarification or add additional context in comments.

3 Comments

If possible can you please update my code as you said?
Have given as u said. Gives error this : The method add(String[]) in the type ArrayList<String[]> is not applicable for the arguments (String) - The method addAll(Collection<? extends String[]>) in the type ArrayList<String[]> is not applicable for the arguments and displays suggestion as "addAll()" --When give addAll() it displays error The method addAll(Collection<? extends String[]>) in the type ArrayList<String[]> is not applicable for the arguments (String)
Have done as you said ArrayList list = new ArrayList(); ArrayList list1 = new ArrayList(); Created two arraylist--list.add(rs.getString("path")); list1.add(rs.getString("name")); added value to arraylists.. Thanks sir

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.