java.sql.SQLSyntaxErrorException: Table/View 'x' does not exist

▼魔方 西西 提交于 2019-12-20 04:38:05

问题


I am trying to run a web application called AddressBook (JSF with Facelets) in the NetBeans 8.0.2 IDE with GlassFish 4.1 that accesses a relational database via the following code in the managed bean:

@Resource( name="jdbc/addressbook" )
DataSource dataSource;

When I run the application I get the following error in the browser:

java.sql.SQLSyntaxErrorException: Table/View 'ADDRESSES' does not exist
    at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
    at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
    at org.apache.derby.client.am.Connection.prepareStatement(Unknown Source)
    at com.sun.gjc.spi.base.ConnectionHolder.prepareStatement(ConnectionHolder.java:586)
    at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareCachedStatement(ConnectionWrapper40.java:255)
    at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareCachedStatement(ConnectionWrapper40.java:52)
    at com.sun.gjc.spi.ManagedConnectionImpl.prepareCachedStatement(ManagedConnectionImpl.java:992)
    at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareStatement(ConnectionWrapper40.java:173)
    at addressbook.AddressBean.getAddresses(AddressBean.java:157)

AddressBean.java line 157 is:

PreparedStatement getAddresses = connection.prepareStatement(
            "SELECT FIRSTNAME, LASTNAME, STREET, CITY, STATE, ZIP " +
            "FROM ADDRESSES ORDER BY LASTNAME, FIRSTNAME" );

The addressbook database which contains the ADDRESSES table is created via the following steps:

  1. Create Connection Pool

    1. Right click on GlassFish server and select View Domain Admin Console
    2. In the GlassFish web page in Common Tasks on the left column click on
    3. JDBC Connection Pools
    4. Clicked the New Button and added the following entries:
      • AddressBookPool for the Name
      • javax.sql.DataSource for Resource Type
      • JavaDB for the Database Vendor
    5. Click Next and scroll to Additional Properties and set the following fields:
      • Attributes: ;create=true
      • DatabaseName: addressbook
      • Password: APP
      • Click Finish
  2. Create Data Source Name

    1. In the GlassFish web page in Common Tasks on the left column click on
      JDBC Resources
    2. Click the New Button and specify JDBC/addressbook as the JNDI name
    3. Select AddressBookPool as the Pool name

When I return to the NetBeans Services tab, addressbook has been created under Java DB.

I successfully connect to the database: jdbc:derby://localhost:1527/addressbook [APP on APP] and make APP the default schema

I open a file in NetBeans: addressbook.sql which contains SQL to populate addressbook. I run it on the above connection and ADDRESSES table is created and I am able to view the data.

The AddressBook properties has Java DB Driver as a library.


回答1:


Make sure that your web.xml file (in Configuration Files folder) has the resource reference. Example:

    <resource-ref>
        <res-ref-name>jdbc/db1</res-ref-name>
        <res-type>javax.sql.ConnectionPoolDataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>

Where db1 is your jdbc resource name. Also ensure that the value has the correct resource type, a connection pool data source in the example.



来源:https://stackoverflow.com/questions/29184866/java-sql-sqlsyntaxerrorexception-table-view-x-does-not-exist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!