我需要帮助从数据库中获取数据

时间:2014-02-06 01:04:26

标签: java iteration resultset

这是我的表

FirstName----LastName----City--------State 
Ram----------Padhan------Houston-----TX 
Ram----------Padhan------Washingtom--MI 
Ram----------Padhan------Alok--------BG 
Keiry--------Lading------Azan--------BL 
Keiry--------Lading------Aror--------BS

如何获取数据以便输出:

FirstName: Ram--LastName: Padhan 
City: Houston--State:TX 
City:Washington--State:MI 
City:Alok--State:BG
FirstName: Keiry--LastName: Lading 
City: Azan--State:BL 
City:Aror--State:BS

除了获取每行的值之外:

FirstName: Ram--LastName: Padhan 
City: Houston--State:TX 
FirstName: Ram--LastName: Padhan 
City:Washington--State:MI

这就是我的所作所为。我得到的输出只是第一行的firstname和lastname,而对于州和城市列,我得到的值直到最后一行。

public class Practise 
{ 
    public static void main(String[] args) 
    { 
        Connection conn = null; 
        Statement stmt = null; 

        try { 
             Class.forName("com.mysql.jdbc.Driver") ; 
             String url = "jdbc:mysql://localhost:3306/table_name"; 

             conn = DriverManager.getConnection(url,"root","root" ); 

             stmt = (Statement) conn.createStatement(); 

             String sql = "SELECT First_Name ,Last_Name ,City_Name , State_Name, FROM table_name"; 

             ResultSet rs = stmt.executeQuery(sql); 

             while(rs.next())
             {
                String first = rs.getString("First_Name"); 
                String last = rs.getString("Last_Name"); 
                System.out.print("First: " + first); 
                System.out.println("Last: " + last);

                do 
                { 
                    String city = rs.getString("City_Name"); 
                    String state = rs.getString("State_Name"); 
                    System.out.println("City: " + city);
                    System.out.println("State: " + state); 
                    System.out.println(); 
                } while(rs.next());
             }
             rs.close(); 
        }
        catch(SQLException se)
        { 
             se.printStackTrace(); 
        }
        catch(Exception e)
        { 
             e.printStackTrace();
        }
        finally { 
             try { 
                if (stmt != null) 
                    conn.close(); 
             }
             catch(SQLException se)
             { } 
             try {
                 if (conn != null) 
                     conn.close(); 
             }
             catch(SQLException se)
             { 
                 se.printStackTrace(); 
             } 
        } 
    } 
}

3 个答案:

答案 0 :(得分:1)

你有一个while循环嵌套在另一个while循环中。

将其更改为:

while(rs.next())

{ 
String first = rs.getString("First_Name"); 

String last = rs.getString("Last_Name"); 

System.out.print("First: " + first); 

System.out.println("Last: " + last);
String city = rs.getString("City_Name"); 

String state = rs.getString("State_Name"); 

System.out.println("City: " + city);

 System.out.println("State: " + state); 

System.out.println(); 
}

编辑我最初误解了您的问题。这是伪代码,因为我没有尝试过,但您需要使用GROUP_CONCAT function

SELECT First_Name ,Last_Name , group_concat("City:",City_Name , "--State:", State_Name separator "\n") as data FROM table_name group by First_Name, Last_Name

答案 1 :(得分:1)

考虑使用2个表。一个用于人,另一个用于城市/州。首先选择人员,然后在迭代时使用钥匙查找城市/州。

答案 2 :(得分:0)

Person previousPerson = null;
Person nextPerson = null;
while(rs.next()) {
    nextPerson = new Person(rs.getString("First_Name"), rs.getString("Last_Name")); 
    if( ! nextPerson.equals(previousPerson)) {
        previousPerson = nextPerson;
        System.out.println(previousPerson); 
    }
    City city = new City(rs.getString("City_Name"), rs.getString("State_Name")); 
    System.out.println(city);
}