简体   繁体   English

javax.naming.NameNotFoundException:无法连接到derby数据库

[英]javax.naming.NameNotFoundException : Unable to connect to derby database

I am trying to connect to derby database while using Tomcat . 我正在尝试使用Tomcat连接到derby数据库。 I try to do this through a servlet. 我尝试通过servlet做到这一点。 But i am unable to connect and get the following exception : 但是我无法连接并得到以下异常:

javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
at org.apache.naming.NamingContext.lookup(NamingContext.java:803)
at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:158)
at javax.naming.InitialContext.lookup(InitialContext.java:392)

at servlets.servlet_1.doGet(servlet_1.java:21) //---> I have marked this line in the below servlet

at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)

when the following servlet gets run : 当以下servlet运行时:

package servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.http.*;
import javax.servlet.*;
import javax.sql.DataSource;

public class servlet_1 extends HttpServlet{

@Override 
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
    try {
       // String queryString = request.getQueryString();
        Context context = new InitialContext();
        DataSource ds = (DataSource)context.lookup("jdbc/PollDatasource");
        Connection connection = ds.getConnection(); // ----> LINE 21
        String sqlQuery = "select * from PollResult";
        PreparedStatement statement = connection.prepareStatement(sqlQuery);
        ResultSet set = statement.executeQuery();
        System.out.println("after the final statement");
    }catch(Exception exc) {
        exc.printStackTrace();
    }
}

} }

Why do i get this exception ? 为什么会出现此异常?

I added the following lines in context.xml of Tomcat : 我在Tomcat context.xml中添加了以下几行:

    <Resource name="jdbc/PollDatasource" auth="Container" type="javax.sql.DataSource"
    driverClassName="org.apache.derby.jdbc.EmbeddedDriver" url="connection.url"
    username="suhail" password="suhail" maxActive="20" maxIdle="10" maxWait="-1" />

and the following lines in web.xml : 以及web.xml的以下几行:

 <resource-ref>
<description>my connection</description>
<res-ref-name>jdbc/PollDatasource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

What am i missing ? 我想念什么?

NOTE : I am using Netbeans 7.1 注意:我正在使用Netbeans 7.1

You have to obtain the naming/environment context: 您必须obtain 命名/环境上下文:

Context initContext = new InitialContext();
Context envContext  = (Context)initContext.lookup("java:comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/PollDatasource");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM