问题
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:
Create Connection Pool
- Right click on GlassFish server and select View Domain Admin Console
- In the GlassFish web page in Common Tasks on the left column click on
- JDBC Connection Pools
- Clicked the New Button and added the following entries:
- AddressBookPool for the Name
- javax.sql.DataSource for Resource Type
- JavaDB for the Database Vendor
- Click Next and scroll to Additional Properties and set the following
fields:
- Attributes: ;create=true
- DatabaseName: addressbook
- Password: APP
- Click Finish
Create Data Source Name
- In the GlassFish web page in Common Tasks on the left column click on
JDBC Resources - Click the New Button and specify JDBC/addressbook as the JNDI name
- Select AddressBookPool as the Pool name
- In the GlassFish web page in Common Tasks on the left column click on
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