javax.naming.NamingException:无法创建资源实例问题

时间:2012-08-21 03:20:18

标签: java jdbc database-connection

在eclipse中运行此示例代码时(问题与JSF无关)我收到错误javax.naming.NamingException:无法创建资源实例

Java文件

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
@SessionScoped
public class CustomerBean implements Serializable{

    //resource injection
    @Resource(name="jdbc/SSHD")
    private DataSource ds;

    //if resource inject is not support, you still can get it manually.
    public CustomerBean(){
        try {
            System.out.println("11111111111111");

            Context ctx = new InitialContext();
            System.out.println("222222222222222222");
            ds = (DataSource)ctx.lookup("java:comp/env/jdbc/SSHD");
        } catch (NamingException e) {
            System.out.println("3333333333333333333");
            e.printStackTrace();
        }

    }
}

web应用/ WEB-INF / web.xml中

<resource-ref>
   <description>SSHD Connection</description>
   <res-ref-name>jdbc/SSHD</res-ref-name>
   <res-type>oracle.jdbc.pool.OracleDataSource</res-type>
   <res-auth>Container</res-auth>
 </resource-ref>

web应用/ META-INF / context.xml中

<Context>

  <Resource name="jdbc/SSHD" auth="Container" type="oracle.jdbc.pool.OracleDataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="ABCD" password="XYZZ" driverClassName="oracle.jdbc.driver.OracleDriver"
               url="jdbc:oracle:thin:@XX.XX.XX.XXXX:1521:SSHD"/>

</Context>

web应用/ WEB-INF / lib中

classes12.jar

错误堆栈

11111111111111
javax.naming.NamingException: Cannot create resource instance
222222222222222222
3333333333333333333
    at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:146)
    at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:843)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:154)

请告知

0 个答案:

没有答案