# class not found database Driver Servlet WEB-INF



## needJavaProz (2. Dez 2008)

Hallo zusammen,

ich habe folgendes problem:

ide:eclipse

Versuch Datenbankverbindung hestellen mit dem Servlet, über eine normale Klasse funktioniert das, aber über den Webserver nicht. Ich hab so was gelesen wie muss im WEB-INF/lib folder sein. nun ja hab ich das mysql.jar reinkopiert in den folder aber eclipse erkennts nicht.

Natürlich hab ich im build path, das mysql connector jar geadded. aber es gibt da noch ne rubik mit Web app libraries... nur krieg ichs nicht hin das jar dort darunter reinzuhängen, fals das überhaupt was bringen soll.

Es ist echt zum verzweifeln, was mache ich falsch? Ich wär echt dankbar für eure Hilfe.

Gruss
Thomas




Fehlermeldung:
INFO: Server startup in 1375 ms
TestServlet:Initializing Servlet
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1386)
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1232)
	at java.lang.ClassLoader.loadClassInternal(Unknown Source)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Unknown Source)
	at TestServlet.init(TestServlet.java:40)
	at javax.servlet.GenericServlet.init(GenericServlet.java:212)
	at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1139)
	at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:791)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:127)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:875)
	at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
	at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
	at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
	at java.lang.Thread.run(Unknown Source)


das web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>
	mistest</display-name>
	<servlet>
		<description>
		</description>
		<display-name>
		TestServlet</display-name>
		<servlet-name>TestServlet</servlet-name>
		<servlet-class>
		TestServlet</servlet-class>
		<init-param>
			<description>
			</description>
			<param-name>username</param-name>
			<param-value>admin</param-value>
		</init-param>
		<init-param>
			<description>
			</description>
			<param-name>driverclassname</param-name>
			<param-value>com.mysql.jdbc.Driver</param-value>
		</init-param>
		<init-param>
			<description>
			</description>
			<param-name>dburl</param-name>
			<param-value>jdbc:mysql://localhost:3306/test</param-value>
		</init-param>
		<init-param>
			<description>
			</description>
			<param-name>password</param-name>
			<param-value></param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>TestServlet</servlet-name>
		<url-pattern>/TestServlet</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.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>
</web-app>




code des Servlets:


```
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;

/**
 * Servlet implementation class TestServlet
 */
public class TestServlet extends HttpServlet {
	
	Connection dbConnection;
	
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public TestServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init() throws ServletException {
		
		System.out.println(getServletName() + ":Initializing Servlet");
		ServletConfig config = getServletConfig();
		
		String driverClassName = config.getInitParameter("driverclassname");
		
		String dbURL = config.getInitParameter("dburl");
		String username = config.getInitParameter("username");
		String password = config.getInitParameter("password");
		
		try {
			Class.forName(driverClassName);
			System.out.println("Driver loaded!");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			dbConnection = DriverManager.getConnection(dbURL, username, password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("Initialized");
		
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	public void destroy() {
		
		try {
			dbConnection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
```


code der Klasse die funktioniert:


```
package basic;

import java.sql.*;
import java.io.*;
import java.util.*;

public class dbConnection {

	
	Connection cn;
	
	void doConnection() {
		
		try {
			String driverName = "com.mysql.jdbc.Driver";
//			Class.forName("com.mysql.jdbc.Driver");
			Class.forName(driverName);
//			DriverManager.registerDriver(new com.mysql.jdbc.Driver());
			System.out.println("Driver found!");
//		} catch (SQLException e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
		} catch (ClassNotFoundException ce) {
			
		}
		
		try {
			cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "admin", "");
			System.out.println("DB accessed!");
			System.exit(0);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	
	
	public static void main(String[] args) {
		dbConnection dc = new dbConnection();
		dc.doConnection();
	}

}
```


----------



## rico (2. Dez 2008)

Hi,

ich bin mir zwar nicht hundertprozentig sicher, aber probier mal den mysql-connector nach "<TOMCAT-HOME>/lib" zu kopieren. Dann sollte tomcat den Treiber finden.

LG


----------



## maki (2. Dez 2008)

Die mysql.jar ist wohl nicht im Eclipse Build Path.


----------



## needJavaProz (3. Dez 2008)

jo genau in lib vom tomcat und dann läufts... ich frag mich zwar ob das nicht im WEB_INF sein soll... aber könnte sein das man dafür da beim tomcat den in der server.xml da den verweis auf ein wie heisst das out-of-tomcat root verzeichnis setzen müsste und dann könnte es auch so gehen?

Auf jeden fall läufts so. Thanks a lot.


----------

