2017-04-14 12 views
1

私は、Javaに新たなんだと私はSQL DBからデータを取得し、彼がログインしたときにそれを表示しようとしているときに私はこの問題を抱えているが返されます。のJava + SQLのexecuteQueryと結果

sql = "select nom from adherent where id_adherent=3"; 
try { 
    pst = con.prepareStatement(sql); 
    ResultSet rs = pst.executeQuery(); 
} catch (SQLException e) { 
    JOptionPane.showMessageDialog(null, "here 1"); 
} 

try { 
    if (rs.next()) { 
     String sum = rs.getString("select *"); 
     nom.setText(" " + sum); 
    } 
} catch (SQLException e) { 
    JOptionPane.showMessageDialog(null, "here 2"); 
} 

はありがとうございました。

あなたは、あなたがこれを使用することはできませんので、あなたは、最初のtryブロックであなたの ResultSetを定義しているため、2つのtryおよびcatchを必要とする他に試すので、代わりにこのことはありません
+1

私は 'rs.getString(" select * ")'がうまくいくとは思っていません。 – Berger

+0

2つの別々のtry catchブロックは必要ありませんが、実際の問題は何ですか? –

+0

rs.getString( "nom")やrs.getString(1)のようなカラム名(またはインデックス)でデータを取得するだけです。また、データを切り取った同じtryブロック内でResultSetを使用することをお勧めします。 –

答えて

0

私はこの意志を考えます助けてください:

sql = "select nom from adherent where id_adherent=3"; 

try { 
    pst = con.prepareStatement(sql); 
    ResultSet rs = pst.executeQuery(); 

    if (rs.next()) { 
     String sum = rs.getString("nom"); 
     nom.setText(" " + sum); 
    } 
} 
catch (SQLException e) { 
    JOptionPane.showMessageDialog(null, "here 1"); 
} 
+0

yeah iv'eはあなたに@everyone <3 –

1

sql = "select nom from adherent where id_adherent=3"; 
try { 
    pst = con.prepareStatement(sql); 
    ResultSet rs = pst.executeQuery(); 
    if (rs.next()) { 
     String sum = rs.getString("nom"); 
     nom.setText(" " + sum); 
    } 
} catch (SQLException e) { 
    JOptionPane.showMessageDialog(null, "here 1"); 
} 

あなたは完璧と任意の構文エラー、またはSQLインジェクションの遠何かをしたい場合は、たとえばのPreparedStatementのオペレータ?を使用する必要があります。

sql = "select nom from adherent where id_adherent = ?"; 
//--------------------------------------------------^ 
try { 
    pst = con.prepareStatement(sql); 
    pst.setInt(1, id_adherent);//you can set any number in id_adherent in your case 3 
    .... 
+0

また、PreparedStatementを使用するときは、 "select nom from adhere where id_adherent =?"とSQLを書く方がはるかに意味があり(安全です) pst.setInt(1、3)でパラメータを設定します。 –

+0

私はすでにこの文脈で何かを書いています@dsp_userただちに –

+0

@dsp_userはあなたが私の編集をチェックすることを意味していました); –

関連する問題