【问题标题】:Updating a specific record in a database in a web app在 Web 应用程序中更新数据库中的特定记录
【发布时间】:2015-01-26 09:21:29
【问题描述】:

我有一个数据库,其中包含多个站点的地块详细信息。我的网络应用程序显示了可供预订的地块,并提供了一个按钮,单击该按钮应预订特定地块。但是我无法让我的 book() 方法工作 - 它没有更新表格。当点击h:commandButton 时,它应该更新表格特定行上的BOOKINGNO 列,用随机的6 位预订号覆盖<null>

我看不出哪里出错了。我正在使用 NetBeans 和 GlassFish(如果重要的话)。

下面的 sn-p 显示了我最近尝试的相关代码:

表格样本

PLOT PLOTNO ACCOMTYPE STARTDATE ENDDATE BOOKINGNO 1 Tent 2014-02-03 2014-02-06 <null>

public String book(int plotNo) throws SQLException
{
    // check whether dataSource was injected by the server
    if (dataSource == null)
        throw new SQLException("Unable to obtain DataSource");
    // obtain a connection from the connection pool
    Connection connection = DriverManager.getConnection
        ("jdbc:derby://localhost:1527/2Day4U", "APP", "APP");
    // check whether connection was successful
    if (connection == null)
        throw new SQLException("Unable to connect to DataSource");
    try
    {
        int newBookingNo = generateBookingNo();
        setBookingNo(newBookingNo);        
        String updateTableSQL = "UPDATE PLOT SET BOOKINGNO = ? WHERE PLOTNO = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(updateTableSQL);
        preparedStatement.setInt(1, getBookingNo());
        preparedStatement.setInt(2, plotNo);
        preparedStatement.executeUpdate();    
        return "ConfirmBooking"; // go to ConfirmBooking.xhtml page
    } // end try
    finally
    {
        connection.close(); // return this connection to pool
    } // end finally
} // end method book

public int generateBookingNo(){
    Random r = new Random();
    int rand = r.nextInt((999999 - 1) + 1);
    setBookingNo(rand);
    return bookingNo;
}

public int getBookingNo(){
    return bookingNo;
}
public void setBookingNo(int bookingNo){
    this.bookingNo = bookingNo;
}

JSF 页面

<h:dataTable value="#{dealsBean.plotDetailsLiverpool}" var="item"
                 rowClasses="oddRows, evenRows" headerClass="header"
                 styleClass="table" cellpadding="5" cellspacing="0">
        <h:column>
            <f:facet name="header">Plot</f:facet>
                #{item.PLOTNO}
        </h:column>
        <h:column>
            <f:facet name="header">Accom Type</f:facet>
                #{item.ACCOMTYPE}
        </h:column>
        <h:column>
            <f:facet name="header">Sleeps</f:facet>
                #{item.SLEEPINGCAPACITY}
        </h:column>
        <h:column>
            <f:facet name="header">Toilet?</f:facet>
                #{item.TOILETFACILITY}
        </h:column>
        <h:column>
            <f:facet name="header">Price (£)</f:facet>
                #{item.PRICE}
        </h:column>
        <h:column>
            <f:facet name="header">Start Date</f:facet>
                #{item.STARTDATE}
        </h:column>
        <h:column>
            <f:facet name="header">End Date</f:facet>
                #{item.ENDDATE}
        </h:column>
        <h:column>
            <f:facet name="header">Book?</f:facet>
            <h:commandButton onclick="if (!confirm('Do you want to book this holiday?')) return false"
                             value="Book"
                             action="#{dealsBean.book(dealsBean.plotNo)}"
                             rendered="#{dealsBean.bookingNo >= 0 }">
            </h:commandButton>
        </h:column>
    </h:dataTable>

这样可以吗?

我尝试过做同样的事情,但使用了持久性。但我得到语法错误: java.lang.RuntimeException: java.lang.IllegalArgumentException: Object: 750,573 is not a known entity type.

@PersistenceContext(unitName = "2Day4UPU")
private EntityManager em;
@Resource
private javax.transaction.UserTransaction utx;

private int bookingNo;

public int getBookingNo(){
    return bookingNo;
}
public void setBookingNo(int bookingNo){
    this.bookingNo = bookingNo;

public String updateEntity(Object object){
    int newBookingNo = generateBookingNo();
    setBookingNo(newBookingNo);

    try {
        utx.begin();
        em.merge(bookingNo);
        utx.commit();
        return "ConfirmBooking";
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
public int generateBookingNo(){
    Random r = new Random();
    int rand = r.nextInt((999999 - 1) + 1);
    setBookingNo(rand);
    return bookingNo;
}

和 JSF:

<h:commandButton onclick="if (!confirm('Do you want to book this holiday?')) return false"
                             value="Book"
                             action="#{dealsBean.updateEntity(item.bookingNo)}"
                             rendered="#{dealsBean.bookingNo != null}">
            </h:commandButton>

【问题讨论】:

  • 你能把setBookingNo 的代码贴出来吗?
  • @MikeLaren 好的 - 我已经为 bookingNo 添加了 get 和 set 方法
  • 这是整个代码吗?您没有在 book() 方法中的任何地方使用 connection
  • 您的代码中没有updateinsert。怎么会期望它更新数据库???
  • @PredragMaric 不,这不是完整的代码。只是我认为相关的部分。但我明白你的意思——当我尝试不同的方式(使用PreparedStatement)时,我使用的是connection。我暂时把它留了下来,以防万一我回去使用它。

标签: java jsf jakarta-ee


【解决方案1】:

如果其他一切都正常,要使用 JDBC 连接更新数据库中的一行,试试这个

public String book(int plotNo) throws SQLException
{
    ...

    try
    {
        int newBookingNo = generateBookingNo();
        setBookingNo(newBookingNo);        
        String updateTableSQL = "UPDATE PLOT SET BOOKINGNO = ? WHERE PLOTNO = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(updateTableSQL);
        preparedStatement.setInt(1, getBookingNo());
        preparedStatement.setInt(2, plotNo);
        preparedStatement.executeUpdate();    
        return "ConfirmBooking"; // go to ConfirmBooking.xhtml page
    } // end try
    finally
    {
        connection.close(); // return this connection to pool
    } // end finally
} // end method book

【讨论】:

  • 这很有帮助 - 但它并没有解决我的问题。运行代码后,数据库仍未更新。不知道h:commandButton的代码会不会有问题?
  • @user3046730 你只有tryfinally,加catch块看看有没有异常。如果,当您单击一个按钮时,它进入book() 方法并传递正确的plotNo,那么该按钮完成了它的工作,并且您的Java 代码在某个地方失败了。
  • 我添加了一条 catch 语句 catch (Exception e) { System.out.println(e.getMessage()); .. 但我没有看到任何错误 - 我认为它们会出现在 NetBeans 的控制台中是否正确?
  • @user3046730 该方法是否已执行?你能确认一下吗?
  • 我不确定。数据库没有更改,我没有收到任何错误,我还能如何确认?有没有办法检查?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-31
  • 2016-07-19
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2012-12-23
相关资源
最近更新 更多