如何使用Java从Mysql DB中的表中获取所有数据

时间:2021-07-31 17:07:04

How can I get all data in Mysql DB with JAVA

如何使用JAVA获取Mysql DB中的所有数据

I Have this code but he get only first row

我有这个代码,但他只获得第一排

if (connection != null) {
System.out.println("You made it, take control your database now!");
        Statement st = connection.createStatement();
        String sql = ("SELECT * FROM nc;");
        ResultSet rs = st.executeQuery(sql);

        if(rs.next()) {
        int id = rs.getInt("id");
        String str1 = rs.getString("Name");
        String str2 = rs.getString("City");
System.out.format("%s, %s, %s\n", id, str1, str2);                 

and i get in the screen : id 0 , Name Nizar, City Casablanca

我进入屏幕:id 0,名字Nizar,City Casablanca

so how ican get all rows in my database and thank you

所以ican如何获​​取我的数据库中的所有行,谢谢

4 个解决方案

#1


2  

If you use if the block will execute only once.If you use while the block will execute until the condition false

如果您使用该块只执行一次。如果您使用,则块将执行,直到条件为false

use while instead of if

使用while而不是if

while(rs.next()) {
        int id = rs.getInt("id");
        String str1 = rs.getString("Name");
        String str2 = rs.getString("City");
System.out.format("%s, %s, %s\n", id, str1, str2);  
}


rs.next()  

will return false when all the records are completed

所有记录完成后将返回false

#2


0  

Change your if to a while...

改变你的if一段时间......

while(rs.next()) {
    int id = rs.getInt("id");
    String str1 = rs.getString("Name");
    String str2 = rs.getString("City");
    System.out.format("%s, %s, %s\n", id, str1, str2);      
}

#3


0  

Replace if(rs.next()) by while(rs.next()).

用while(rs.next())替换if(rs.next())。

if(rs.next()) means "if we have one result, move to it and do the following"

if(rs.next())表示“如果我们有一个结果,请转到它并执行以下操作”

while(rs.next()) means "while we still have results, move to the next one and do the following".

而(rs.next())的意思是“当我们仍然有结果时,移动到下一个并执行以下操作”。

#4


0  

You want to change your if (rs.next()) { To be a while(rs.next()) {

你想改变你的if(rs.next()){待一会儿(rs.next()){

You're only iterating through one row as it is, that will iterate through all of them.

你只是按原样迭代一行,它将迭代所有这些行。

As a best practice, don't make the contents of the while(...) loop too fancy. You want to minimize the per-row processing while iterating, so the connection doesn't stay open too long. Many people just read rows into a List, or print them out (like you're doing).

作为最佳实践,不要使while(...)循环的内容过于花哨。您希望在迭代时最小化每行处理,因此连接不会保持打开太长时间。许多人只是将行读入List,或者将它们打印出来(就像你正在做的那样)。

#1


2  

If you use if the block will execute only once.If you use while the block will execute until the condition false

如果您使用该块只执行一次。如果您使用,则块将执行,直到条件为false

use while instead of if

使用while而不是if

while(rs.next()) {
        int id = rs.getInt("id");
        String str1 = rs.getString("Name");
        String str2 = rs.getString("City");
System.out.format("%s, %s, %s\n", id, str1, str2);  
}


rs.next()  

will return false when all the records are completed

所有记录完成后将返回false

#2


0  

Change your if to a while...

改变你的if一段时间......

while(rs.next()) {
    int id = rs.getInt("id");
    String str1 = rs.getString("Name");
    String str2 = rs.getString("City");
    System.out.format("%s, %s, %s\n", id, str1, str2);      
}

#3


0  

Replace if(rs.next()) by while(rs.next()).

用while(rs.next())替换if(rs.next())。

if(rs.next()) means "if we have one result, move to it and do the following"

if(rs.next())表示“如果我们有一个结果,请转到它并执行以下操作”

while(rs.next()) means "while we still have results, move to the next one and do the following".

而(rs.next())的意思是“当我们仍然有结果时,移动到下一个并执行以下操作”。

#4


0  

You want to change your if (rs.next()) { To be a while(rs.next()) {

你想改变你的if(rs.next()){待一会儿(rs.next()){

You're only iterating through one row as it is, that will iterate through all of them.

你只是按原样迭代一行,它将迭代所有这些行。

As a best practice, don't make the contents of the while(...) loop too fancy. You want to minimize the per-row processing while iterating, so the connection doesn't stay open too long. Many people just read rows into a List, or print them out (like you're doing).

作为最佳实践,不要使while(...)循环的内容过于花哨。您希望在迭代时最小化每行处理,因此连接不会保持打开太长时间。许多人只是将行读入List,或者将它们打印出来(就像你正在做的那样)。