更新Web应用程序中数据库的特定记录

问题描述 投票:0回答:2

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

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

下面的代码段显示了我最近一次尝试的相关代码:

表格示例

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

bean

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>
java jsf jakarta-ee
2个回答
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

0
投票

我正在努力进行这项精确的练习,坦率地说,我绝对是n00b。要求分享完整的代码会很多吗?我已完成所有设置并运行到创建和填充表的地步。我创建了基本索引,然后将其发送到列出了我的SITES的“ deals.xhtml”(从DB调用)。谢谢您的宝贵时间。

© www.soinside.com 2019 - 2024. All rights reserved.