0

im using Spring, learning with servlets and all that webstuff, i'm trying to create a simple servlet for connecting to mysql server.. here is my code:

AccesoDB:

package es.prueba.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

/**
 * Servlet implementation class AccesoDB
 */
public class AccesoDB extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private DataSource ds = null;
    ResultSet rs;
    Context ctx;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AccesoDB() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public void init(ServletConfig conf) throws ServletException {
        super.init(conf);

        try {
            ctx = new InitialContext();
            ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccesoBD");
        } catch ( NamingException e) {
            e.printStackTrace();
        }

    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");

        Connection connection = null;

        synchronized(ds) {
            try{
                connection = ds.getConnection();

                PrintWriter out = response.getWriter();
                String query = null;
                Statement stmt;
                query = "SELECT * FROM test";
                stmt = connection.createStatement();

                rs = stmt.executeQuery(query);
                out.println(rs.toString());

                connection.close();
            }catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

}

context.xml:

 <Context path="/AccesoDB" docBase=" AccesoBD "
    debug="5" reloadable="true" crossContext="true">
    <Resource name="jdbc/ AccessoBD " auth="Container"
        type="javax.sql.DataSource" maxActive="100"
        maxIdle="30" maxWait="10000" username="test"
        password="test"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/dbaplicacion"/>
</Context>

web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>elTiempo</display-name>
  <servlet>
    <display-name>AjaxExample</display-name>
    <servlet-name>AjaxExample</servlet-name>
    <servlet-class>es.prueba.servlet.AjaxExample</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>AjaxExample</servlet-name>
    <url-pattern>/AjaxExample</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>holaMundo.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <resource-ref>
    <res-ref-name>jdbc/AccesoBD</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
  <servlet>
    <description></description>
    <display-name>AccesoDB</display-name>
    <servlet-name>AccesoDB</servlet-name>
    <servlet-class>es.prueba.servlet.AccesoDB</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>AccesoDB</servlet-name>
    <url-pattern>/AccesoDB</url-pattern>
  </servlet-mapping>
</web-app>

Here is the trace of the error:

    org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
    at es.prueba.servlet.AccesoDB.doGet(AccesoDB.java:62)
    at es.prueba.servlet.AccesoDB.doPost(AccesoDB.java:83)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
    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)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
    at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:524)
    at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:493)
    at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307)
    at java.sql.DriverManager.getDriver(DriverManager.java:262)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
    ... 18 more

The error is in the line:

connection = ds.getConnection();

But i can't figure it out, as i said before, i'm learning and the other threads doesnt help me.

EDIT: I also have this error when launching Tomcat, dunno if it matters:

java.lang.IllegalArgumentException: El Documento base C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\context no existe o no es un directorio legible
    at org.apache.naming.resources.FileDirContext.setDocBase(FileDirContext.java:142)
    at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:4320)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4489)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:675)
    at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:601)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:502)
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
4
  • "java.lang.IllegalArgumentException: El Documento base C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\context no existe o no es un directorio legible" - yes, this matters. Commented Jan 7, 2014 at 18:27
  • i added my context.xml to that directory, but stills the same.. also if i connect using: Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/dbaplicacion", "test", "test"); My code works.. Commented Jan 7, 2014 at 18:35
  • This suggests that it's your JNDI and connection pool config are the problems. Commented Jan 7, 2014 at 19:08
  • 1
    I'd solved it, read my answer, was just some writing missmatch, but thanks for your comment anyway :) Commented Jan 7, 2014 at 19:11

2 Answers 2

4

Solved.. the problem was.. i was accessing the datasource this way:

ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccesoBD");

and was:

ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccesoDB");

i swapped DB by BD :) thx all anyway ^^

Sign up to request clarification or add additional context in comments.

Comments

0

Put the MySQL JDBC driver JAR in the Tomcat server /lib directory, not your context WEB-INF/lib folder.

You also have an extra space in the Resource name:

<Resource name="jdbc/ AccessoBD " auth="Container"

Remove it and see if those help.

1 Comment

The space isn't the problem, i putted the "mysql-connector-java-5.0.8-bin.jar" in the /lib of Tomcat, is that the JAR i need? stills the same error :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.