使用PreparedStatement连接两个表

时间:2014-11-21 13:32:34

标签: sql jsf java-ee prepared-statement

我有两张桌子;地图和住宿。我想从两个表中选择,将它们连接在一起,然后在JSF页面上显示它们。但是,我是SQL和JSF的新手,我在使用SQL查询的语法时遇到了麻烦。

PLOT
PLOTNO, SITENO(foreign), ACCOMNO(foreign), STARTDATE, ENDDATE

ACCOMODATION
ACCOMNO(primary), ACCOMTYPE, SLEEPINGCAPACITY, TOILETFACILITY, PRICE 

期望的结果是:

PLOTNO, ACCOMTYPE, SLEEPINGCAPACITY, TOILETFACILITY, PRICE, STARTDATE, ENDDATE

其中结果按PLOTNO

排序

下面的代码显示了我最近尝试抛出此错误: "语法错误:遇到" p"在第1行,第135栏。"我尝试了很多变化,但似乎都抛出了某种语法错误。

豆:

try {
        // create a PreparedStatement to get plot details
        PreparedStatement getPlotDetails = connection.prepareStatement
            ("SELECT "
                    + "p.PLOTNO, "
                    + "a.ACCOMTYPE, "
                    + "a.SLEEPINGCAPACITY, "
                    + "a.TOILETFACILITY, "
                    + "a.PRICE, "
                    + "p.STARTDATE, "
                    + "p.ENDDATE "
                    + "FROM PLOT p "

                    //+ "WHERE SITENO = 2; "

                    + "JOIN ACCOMODATION a"
                    + "ON p.ACCOMNO = a.ACCOMNO "

                    + "ORDER BY p.PLOTNO "
                    );
        CachedRowSet rowSet = new com.sun.rowset.CachedRowSetImpl();
        rowSet.populate(getPlotDetails.executeQuery());
        return rowSet; 
    } // end try

JSF页面:

<ui:define name="content">
        <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 No.</f:facet>
                    #{item.ACCOMNO}
            </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 Facility?</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>

1 个答案:

答案 0 :(得分:1)

+ "JOIN ACCOMODATION a"
+ "ON p.ACCOMNO = a.ACCOMNO "

&#34之间没有空间...... ....住宿a&#34;和&#34; ON p.AC .....&#34;

用以下内容替换它们:

+ "JOIN ACCOMODATION a  "
+ "ON p.ACCOMNO = a.ACCOMNO "