2016-04-26 19 views
0

これまでのところ、次のコードを使用してテーブルからn番目の行を選択することができます。
SELECT * FROM table ORDER BY column_name LIMIT n-1,1 ;
次のコードでは、 ID、名前、姓、メール、パスワードの列を持つresponsableという名前のテーブルのすべての行の各列の他のカラムの型が文字列
テーブルの各行からすべての列のすべての属性を取得するmysql java

java.sql.Connection connection = null; 
int rowCount; 
try {connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name","root",""); 
     Statement stmt = null; 
     ResultSet result = null; 
     ResultSet rs = null; 
     rs = stmt.executeQuery("SELECT COUNT(*) FROM responsable"); 
     rowCount = rs.getInt(1); 
     if(rowCount!=0) 
     { 
     for(int i=0;i<rowCount;i++) 
     {result= stmt.executeQuery("SELECT * FROM responsable ORDER BY id LIMIT"+i+",1"); 
      System.out.put(result.getInt(1)); 
      System.out.put(result.getString(2)); 
      System.out.put(result.getString(3)); 
      System.out.put(result.getString(4)); 
      System.out.put(result.getString(5));} 
     } 
}catch (Exception ex) { 
out.println("Unable to connect to database"); 
} 


いる間にidが整数である私は、このコードを実行しようとしていますが、私は何の結果を得ていないのです。 ご協力いただければ幸いです。

+0

責任がFROM SELECT COUNT(*)は0行を返すことがあります。 – randominstanceOfLivingThing

+0

テーブルが空ではないと確信しています –

+0

条件に入れる前にrowCountに何が入っているのを見てみませんか?それが0ならあなたのテーブルは正しいか何かが綴られていません。あなたの全体の出力は、1つの変数に基づいています、あなたは本当にそれが設定されているか把握する必要があります。 – Dresden

答えて

0

stmt変数がnullです。最初に文を作成する必要があります。

Statement stmt = connection.createStatement(); 

また、結果セットから次の行を取得するには、next()を呼び出す必要があります。 など。

if (rs.next()) { 
    rowCount = rs.getInt(1); 
} 

....

if (result.next()) { 
    System.out.put(result.getInt(1)); 
    System.out.put(result.getString(2)); 
    System.out.put(result.getString(3)); 
    System.out.put(result.getString(4)); 
    System.out.put(result.getString(5)); 
} 
関連する問題