简体   繁体   English

从MySql检索并填充.jsp页面

[英]retrieve & populate .jsp page from MySql

How may i retrieve & populate output components at a .jsp page with MySQL? 如何使用MySQL在.jsp页面上检索和填充输出组件? Also, how may i retrieve records pertaining to a particular item_id? 另外,我如何检索与特定item_id有关的记录? Im using IBM Rational Application Developer. 我正在使用IBM Rational Application Developer。

The following is what i have so far, ridden with errors or missing statements I'm sure. 以下是我到目前为止所确定的错误或缺少的语句。

@DB.class @ 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;
        }

You're using PreparedStatement for the SQL: SELECT * FROM TEST.ITEMBOUGHT WHERE ITEM_ID = ? 您正在对SQL使用PreparedStatement: SELECT * FROM TEST.ITEMBOUGHT WHERE ITEM_ID = ? . That means you must tell JDBC what value to replace the question mark (?) in query string, but you put comment on it in //stmt.setString(1,item_id); 这意味着您必须告诉JDBC用什么值替换查询字符串中的问号(?),但要在//stmt.setString(1,item_id);//stmt.setString(1,item_id);注释//stmt.setString(1,item_id);

If you want to select ITEM_ID 1 and ITEM_ID is INTEGER (numeric field), you can use the following code: 如果要选择ITEM_ID 1,并且ITEM_ID是INTEGER(数字字段),则可以使用以下代码:

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
       }
   }
   ...
}

If you want to select all records, remove the WHERE clause: SELECT * FROM TEST.ITEMBOUGHT . 如果要选择所有记录,请删除WHERE子句: SELECT * FROM TEST.ITEMBOUGHT If you don't need PreparedStatement, you can rewrite the code into: 如果不需要PreparedStatement,可以将代码重写为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM