Using datasource in a Java class file
I am using data source explorer in eclipse. the connection is setup and now I want to use it in the normal Java file. I have used the same datasource explorer in a JSP file.
As per this article under the web project section I have tried doing the same.
I tried creating a datasource object as per this Java code
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("jdbc/mysqltest");
con = ds.getConnection();
but it throws an exception as follows
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context at org.apache.naming.NamingContext.lookup(NamingContext.java:770) at org.apache.naming.NamingContext.lookup(NamingContext.java:153) at org.apache.naming.SelectorContext.lookup(SelectorContext.java:152) at javax.naming.InitialContext.lookup(InitialContext.java:392) at logic.Leave.(Leave.java:42) at org.apache.jsp.Leaveform_jsp._jspService(Leaveform_jsp.java:549) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 开发者_StackOverflow社区 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:662)
Setting up the datasource in the datasource explorer has no influence on your server environment.
You will need to define the datasource in Tomcat: either in your context.xml
or in the server.xml
.
you have to configure your JNDI properties for this datasource properly. for example: if you are using JBOSS app server then it can be done in db2-ds.xml file.
精彩评论