1

I have this program:

class DataRetrieve {

    DataRetrieve() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/phonebook", "root", "1234");
            Statement st = con.createStatement();
            st.executeQuery("select * from contacts");
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}

public class MainProgram {

    public static void main(String[] args) {
        DataRetrieve dr = new DataRetrieve();
        //here i want to print that table rows into Console using this
        System.out.println(); // How do you print here that table rows?
    }
} 

Can anyone explain how to print this database information in System.out.println?

2
  • You'll probably want to start with some introductory Java tutorials. Your DataRetrieve class doesn't provide any way to get any data within it. It executes a query in the constructor, but doesn't store the results anywhere outside the scope of that constructor. Nor does it have any other methods for accessing such stored data. Commented Jul 24, 2014 at 11:33
  • Check out the answer from Frans in this post: stackoverflow.com/questions/10903206/… Commented Jul 24, 2014 at 11:41

3 Answers 3

8

You can create a ResultSet.

ResultSet rs = st.executeQuery("select * from contacts");

Then you can iterate over ResultSet, and get the rows.

while (rs.next()) {
    System.out.println(rs.getString(1)); //gets the first column's rows.
}

To get all the column's datas:

ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();

while (rs.next()) {
    for(int i = 1; i < columnsNumber; i++)
        System.out.print(rs.getString(i) + " ");
    System.out.println();
}

If you want to print the database information from MainProgram calls, you can return the ResultSet and iterate over it in your main method.

In this case you should create a method in MainProgram.

class DataRetrieve {

    DataRetrieve() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/phonebook", "root", "1234");
            Statement st = con.createStatement();
            rs = st.executeQuery("select * from contacts");
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    public ResultSet getResultSet() {
          return rs;
    }
private ResultSet rs = null;
}

And in your main method:

public static void main(String[] args) {
        DataRetrieve dr = new DataRetrieve();
        //here i want to print that table rows into Console using this
        System.out.println(); // How do you print here that table rows?

        ResultSet rs = dr.getResultSet();
        while (rs.next()) {
            System.out.println(rs.getString(1)); //gets the first column's rows.
        }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Small Issue Identified: The for loop should be changed from: for(int i = 1; i < columnsNumber; i++) to for(int i = 1; i <= columnsNumber; i++) This will allow the last column from the db query to be returned.
2

Use the following code:

class DataRetrieve {

    DataRetrieve() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/phonebook", "root", "1234");
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from contacts");
            System.out.println(rs.getInt(1));
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}

public class MainProgram {

    public static void main(String[] args) {
        DataRetrieve dr = new DataRetrieve();
        //here i want to print that table rows into Console using this
        System.out.println(); // How do you print here that table rows ?
    }
} 

Well I have caught the rows returned from database in ResultSet instance. You can use getInt(xxx), getString(xxx) and etc to get the related values and then to print them.

1 Comment

It's better to show full example with printing out values in MainProgram.main
1

One basic fault you did is opened the connection but never closed it. Its not a good practice you should always close it for releasing the resources.

In your code first st.executeQuery("select * from contacts"); will return a ResultSet object what you need to do is just iterate the ResultSet and get the rows.

DataRetrieve() {
try {
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("yourUrl", "userName", "password");
    Statement st = con.createStatement();
    ResultSet resultSet = st.executeQuery("select * from contacts");
    while(resultSet.next())
        {
        String columnName1= rs.getString("nameOfColumn1");
        int columnName2= rs.getInt("nameOfColumn2");
        float columnName3= rs.getFloat("nameOfColumn3");
        System.out.println(columnName1+"\t"+ columnName2+"\t"+ columnName3);
      }
  }
}
catch(SQLException e) {
     e.printStackTrace();
}
finally{
   //dont forget the closing statements
 }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.