2012-01-07 13 views
1

&の出力コンポーネントをMySQLで.jspページで取得するにはどうすればよいですか?また、特定のitem_idに関連するレコードを取得する方法はありますか? IBM Rational Application Developerを使用しています。MySqlから.jspページを取得して取り込みます

以下は私が今までに持っていたものですあなたはSQLのためのPreparedStatementを使用しているDB.class

public Items getItemDetails() { 
        Connection con = connect(true); 
      PreparedStatement stmt = null; 
      ResultSet rs = null; 
      String select = "SELECT * FROM TEST.ITEMBOUGHT WHERE ITEM_ID = ?"; 
      Items item = null; 

      try { 
       stmt = con.prepareStatement(select); 
       //stmt.setString(1,item_id); 
       rs = stmt.executeQuery(); 
       while (rs.next()) { 
        //System.out.println("Record Found!"); 
        item = new Items(); 
        //item.setItem_id(item_id); 
        item.setItem_name(rs.getString("item_name")); 
        item.setItem_bidding_start_price(rs.getDouble("item_bidding_starting_price")); 
        item.setItem_bidding_highest_price(rs.getDouble("item_bidding_highest_price")); 
        item.setItem_description(rs.getString("item_description")); 
        item.setItem_quantity(rs.getInt("item_quantity")); 
        item.setItem_closing_date(rs.getDate("item_closing_date")); 
        item.setItem_image(rs.getString("item_image")); 
        item.setItem_status(rs.getString("item_closing_date")); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
       item = null; 
      } finally { 
       try { 
        if (rs != null) rs.close(); 
        if (stmt != null) stmt.close(); 
        if (con != null) con.close(); 
       } catch (SQLException e) {} 
      } 
      return item; 
     } 
+2

これは問題ありませんが、投稿したコードの問題点は何ですか? – Lion

+0

@Lion:出力コンポーネントで表示できません。第1の場所では、SQLステートメントが正しいかどうかわからず、結果を返します。 – brainsfrying

+1

より正確な質問が必要です。それ以外の場合は、誰かがあなたのコードをコピーしてそれに応じてテストすることではなく、実際にテストするのは難しいです。データベースにテーブルを作成して、一目で作成することは不可能であり、達成する。 – Lion

答えて

0

@

SELECT * FROM TEST.ITEMBOUGHT WHERE ITEM_ID = ?。つまり、クエリー文字列内の疑問符(?)を置き換える値をJDBCに伝える必要がありますが、それにコメントを付けます。//stmt.setString(1,item_id);

ITEM_ID 1を選択したい場合はITEM_IDをINTEGER(数値フィールド)にします。 SELECT * FROM TEST.ITEMBOUGHT:あなたは、すべてのレコードを選択するWHERE句を削除したい場合は

public Items getItemDetails() { 
    Connection con = connect(true); 
    PreparedStatement stmt = null; 
    ResultSet rs = null; 
    String select = "SELECT * FROM TEST.ITEMBOUGHT WHERE ITEM_ID = ?"; 
    Items item = null; 

    try { 
     stmt = con.prepareStatement(select); 
     stmt.setInt(1,1); 
     rs = stmt.executeQuery(); 
     while (rs.next()) { 
      ... // and so on 
     } 
    } 
    ... 
} 

:次のコードを使用します。 PreparedStatementが必要ない場合は、コードを次のように書き直すことができます。

Statement stmt = null; 
... 
try { 
... 
    stmt = con.createStatement() ; 
    rs = stmt.executeQuery(select) ; 
... 
} 
+0

私は出力コンポーネントにWHERE句、すなわち、 1のitem_id、上記のコードをどのように編集できますか?混乱した、すみません。チュートリアルを教えてください。 – brainsfrying

+0

item_idがINTEGERの場合は、 'stmt.setInt(1,1);'を使用できます。しかし、item_idがVARCHARの場合、 'stmt.setString(1、" 1 ");'を使うことができます。詳細は、「Oracle JDBCチュートリアル」(http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html)を参照してください。 – jocki