Java:如何打印数据库中的行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24932374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:48:48  来源:igfitidea点击:

Java: how to print the rows from database?

javamysqldatabase

提问by Narayan

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?

谁能解释一下如何打印这个数据库信息System.out.println

采纳答案by user2640782

You can create a ResultSet.

您可以创建一个ResultSet.

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

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

然后您可以遍历 ResultSet,并获取行。

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 MainProgramcalls, you can return the ResultSetand iterate over it in your main method.

如果要从MainProgram调用中打印数据库信息,可以返回ResultSet并在 main 方法中对其进行迭代。

In this case you should create a method in MainProgram.

在这种情况下,您应该在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.
        }
}

回答by Narayan

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.

好吧,我在 ResultSet 实例中捕获了从数据库返回的行。您可以使用getInt(xxx),getString(xxx)等来获取相关值,然后打印它们。

回答by SparkOn

One basic fault you did is opened the connection but never closedit. 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 ResultSetobject what you need to do is just iterate the ResultSetand get the rows.

在您的代码中,首先st.executeQuery("select * from contacts");将返回一个ResultSet对象,您需要做的只是迭代ResultSet并获取行。

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
 }