Java 从MySql检索并填充.jsp页面

Java 从MySql检索并填充.jsp页面,java,mysql,Java,Mysql,如何使用MySQL在.jsp页面检索和填充输出组件?此外,我如何检索与特定项目id相关的记录?我正在使用IBMRationalApplicationDeveloper 以下是我到目前为止所做的,我确信其中充满了错误或遗漏的陈述 @DB.class public Items getItemDetails() { Connection con = connect(true); PreparedStatement stmt = null

如何使用MySQL在.jsp页面检索和填充输出组件?此外,我如何检索与特定项目id相关的记录?我正在使用IBMRationalApplicationDeveloper

以下是我到目前为止所做的,我确信其中充满了错误或遗漏的陈述

@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;
        }

您正在为SQL使用PreparedStatement:SELECT*FROM TEST.itemBuild,其中ITEM_ID=?。这意味着您必须告诉JDBC用什么值替换查询字符串中的问号,但您要在//stmt.setString1,item_id

如果要选择ITEM_ID 1且ITEM_ID为整数数字字段,可以使用以下代码:

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
       }
   }
   ...
}
如果要选择所有记录,请删除WHERE子句:select*FROM TEST.ITEMBUILD。如果不需要PreparedStatement,可以将代码重写为:

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

没关系,但是你发布的代码到底有什么问题?@Lion:我无法在输出组件上显示它们。首先,我甚至不知道sql语句是否正确&返回任何结果。请注意,您必须更准确地回答您的问题。否则,并不是说有人会复制您的代码并进行相应的测试,而且确实很难进行测试,因为这样做需要在数据库中有相关的表,而这些表是不可能在不知道您想要实现什么的情况下一眼创建的。Alrite,我会这样做,谢谢。我甚至不知道sql语句。。返回任何结果。如果找到语句//System.out.printlnRecord!;没有被注释掉,您检查了日志!或者使用IDE设置进行web/DB开发,并使用调试器逐步完成。假设我希望使用WHERE子句(即item_id为1)填充输出组件,那么如何编辑上述代码?对不起,我很困惑。您还可以向我介绍一些教程吗?如果item_id是整数,您可以使用stmt.setInt1,1;。但是,如果item_id是VARCHAR,则可以使用stmt.setString1,1;。你可以在网上阅读更多