glassfish JDBC资源

时间:2017-10-02 09:53:36

标签: java jdbc glassfish

我有一个ProductDAO课程:

package model;

import java.sql.*; 
import javax.sql.*; 
import java.util.*; 
import javax.naming.*;

public class ProductDAO { 
    private DataSource datasource;
public ProductDAO(){ 
    try{ 
        Context ctx = new InitialContext();
        datasource = (DataSource) ctx.lookup("jdbc/productDS"); 
} catch (Exception e) { 
    System.out.println("DataSource error!!!"); 
    e.printStackTrace(); 
}
}

public List<Product> getAllProducts(){ 
    String query ="select * from product";
    List<Product> list = new ArrayList<Product>(); 
    Connection con = null; 
    Statement stmt = null; 
    ResultSet rs = null; 
    try{ 
        con = datasource.getConnection(); 
        stmt = con.createStatement();
        rs = stmt.executeQuery(query); 
        while( rs.next()){ 
            int id = rs.getInt(1);
            String name = rs.getString(2); 
            String description = rs.getString(3); 
            double price = rs.getDouble(4); 
            list.add( 
                    new Product( id, name, description, price));
        } } 
    catch( SQLException se ){ 
        se.printStackTrace();
    } 
    finally { 
        if(rs != null) { 
            try { rs.close();}
            catch (Exception e) { e.printStackTrace(); } 
        }
        if(stmt != null) { 
            try { stmt.close();}
            catch (Exception e) { e.printStackTrace(); }
        }
        if(con != null){ 
            try { con.close();} 
            catch (Exception e) { e.printStackTrace(); }
            } 
        } 
        return list;
}
}

glassfish-resources.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
  <jdbc-resource enabled="true" jndi-name="jdbc/productDS" object-type="user" pool-name="connectionPool">
    <description/>
  </jdbc-resource>

  <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="table" datasource-classname="org.apache.derby.jdbc.ClientDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="connectionPool" non-transactional-connections="false" ping="false" pool-resize-quantity="2" pooling="true" res-type="javax.sql.DataSource" statement-cache-size="0" statement-leak-reclaim="false" statement-leak-timeout-in-seconds="0" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="true">
    <property name="URL" value="jdbc:derby://localhost:1527/productDB"/>
    <property name="serverName" value="localhost"/>
    <property name="PortNumber" value="1527"/>
    <property name="DatabaseName" value="productDB"/>
    <property name="User" value="root"/>
    <property name="Password" value="root"/>
  </jdbc-connection-pool>
</resources>

然后我得到了这个例外:

Severe:   javax.naming.NamingException:
Lookup failed for 'jdbc/productDS' in
SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory,
 java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl,

 java.naming.factory.url.pkgs=com.sun.enterprise.naming}
    [Root exception is javax.naming.NameNotFoundException: productDS not found]

0 个答案:

没有答案