我使用Spring,学习servlet和所有webstuff,我试图创建一个简单的servlet来连接到mysql服务器..这是我的代码:
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>
以下是错误的痕迹:
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
错误在于:
connection = ds.getConnection();
但我无法弄清楚,正如我之前所说,我正在学习而其他线程并没有帮助我。
编辑:启动Tomcat时我也遇到此错误,如果重要则不知道:
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)
答案 0 :(得分:3)
解决了..问题是..我正在以这种方式访问数据源:
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccesoBD");
并且是:
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccesoDB");
我用BD换了DB :)无论如何都是^ ^
答案 1 :(得分:0)
将MySQL JDBC驱动程序JAR放在Tomcat server / lib目录中,而不是上下文WEB-INF / lib文件夹中。
资源名称中还有一个额外的空格:
<Resource name="jdbc/ AccessoBD " auth="Container"
删除它,看看是否有帮助。