# JSP:Scriptlet ClassNotFound



## Donar (23. Okt 2008)

Guten Morgen ihr lieben,

zur Zeit möchte ich durch eine JSP Datei meine Verbindung zur Datenbank testen. Hier der Code:


```
<jsp:directive.page import="java.sql.*" />
<jsp:directive.page import="java.util.*" />
<jsp:scriptlet>
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:80/partyservice", "jspdude", "jspdude");
Statement statement = connection.createStatement();
String SQL = "select * from GERICHTE";
ResultSet rs = statement.executeQuery(SQL);
ResultSetMetaData rsmd=rs.getMetaData();
</jsp:scriptlet>
```

Dabei entsteht jedoch folgende Fehlermeldung:



> description The server encountered an internal error () that prevented it from fulfilling this request.
> 
> exception
> org.apache.jasper.JasperException: An exception occurred processing JSP page /jsptest.jsp at line 4
> ...



Ich bin in dem Gebiet noch relativ neu, verstehe daher die Fehlermeldungen nicht. Was habe ich falsch geschrieben?


----------



## maki (23. Okt 2008)

> [root cause
> javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver


Der JDBC Treiber ist nicht im Classpath.

Welche IDE nutzt du?

Warum eigentlich Db Abfragen in JSPs? Das ist ein Job für Servlets, und diese sollte man verstanden haben bevor man JSPs schreibt.


----------



## Donar (23. Okt 2008)

Hallo maki,

der JDBC Treiber liegt im Web-inff/lib Verzeichnis. Muss ich das noch irgendwo kenntlich machen?

Die DB Abfragen schreibe ich nur Testweise in die JSP. Ich kenne die Strings für die Connection noch nicht und ich möchte einige Abfragen testen, daher benötige diese JSP.

Edit: Ich nutze für die JSP Dateien Dreamweaver CS3 und für die Servlets Eclipse 3.4.


----------



## maki (23. Okt 2008)

Web-inff/lib

gibt es nicht, es gibt nur:

WEB-INF/lib

Wenn er dort liegt, funktioniert es, wenn nicht, dann nicht.


----------



## Donar (23. Okt 2008)

Hallo nochmal,

das Verzeichnis meinte ich auch. Bitte entschuldige meine Ungenauigkeit. In dem Verzeichnis WEB-INF/lib liegt die Datei ojdbc14.jar. Leider geht die Fehlermeldung "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" nicht weg.


----------



## maki (23. Okt 2008)

>>  ojdbc14.jar

Pfui!
Was willst du denn damit?
Das ist die berüchtige JDBC-ODBC Krücke, lösch das mal ganz schnell.

Besorge dir den MySQL Connector passend zu der Version deiner MySQL DB.

http://www.mysql.com/products/connector/j/


----------



## Donar (23. Okt 2008)

Vielen Dank für den Tip. Ich habe den Treiber aus meiner Datenbank Vorlesung bekommen. Leider wurden wir auf keine weiteren Probleme hingewiesen, was darauf hinweist, dass wir selbst darauf stoßen sollen.

Nun entsteht folgendes Problem: ich benutze Java jdk 1.6.0_06 und bin froh, dass ich das überhaupt unter Windows Vista zum laufen bekommen habe (es war mein 7. Versuch auch nur irgend eine Version von Java zu starten). Wenn ich das richtig sehe, unterstützt der Treiber aber nur Version 1.5.1. (korrigier mich bitte, wenn ich das wieder mal falsch verstehe). Woher bekomme ich eine aktuelle Version des Treibers?


EDIT: mysql-connector-java-5.1.7-bin lieferte folgenden Fehler:


```
The server encountered an internal error () that prevented it from fulfilling this request.

exception 
org.apache.jasper.JasperException: javax.servlet.ServletException: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Last packet sent to the server was 0 ms ago.
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:398)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause 
javax.servlet.ServletException: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Last packet sent to the server was 0 ms ago.
	org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:850)
	org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
	org.apache.jsp.jsptest_jsp._jspService(jsptest_jsp.java:140)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

root cause 
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Last packet sent to the server was 0 ms ago.
	sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
	com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
	com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2120)
	com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:723)
	com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
	sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
	com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
	com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
	java.sql.DriverManager.getConnection(DriverManager.java:582)
	java.sql.DriverManager.getConnection(DriverManager.java:185)
	org.apache.jsp.jsptest_jsp._jspService(jsptest_jsp.java:59)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause 
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Last packet sent to the server was 0 ms ago.
	sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
	com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
	com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:667)
	com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1070)
	com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2048)
	com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:723)
	com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
	sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
	com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
	com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
	java.sql.DriverManager.getConnection(DriverManager.java:582)
	java.sql.DriverManager.getConnection(DriverManager.java:185)
	org.apache.jsp.jsptest_jsp._jspService(jsptest_jsp.java:59)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

root cause 
java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
	com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2455)
	com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:591)
	com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1070)
	com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2048)
	com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:723)
	com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
	sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
	com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
	com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
	java.sql.DriverManager.getConnection(DriverManager.java:582)
	java.sql.DriverManager.getConnection(DriverManager.java:185)
	org.apache.jsp.jsptest_jsp._jspService(jsptest_jsp.java:59)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
```


----------



## maki (23. Okt 2008)

>> Wenn ich das richtig sehe, unterstützt der Treiber aber nur Version 1.5.1. (korrigier mich bitte, wenn ich das wieder mal falsch verstehe).

Du hast es falsch verstanden.

Wichtig ist die Version des MySQL Servers, (verrate sie doch mal), denn danach richtig sich die JDBC Treiber Version.


----------



## Donar (23. Okt 2008)

MySQL
Client API library version 	5.0.51b 
Client API header version 	5.0.51a

Also muss ich "MySQL Connector/J 5.0" benutzen?


----------



## maki (23. Okt 2008)

Ja, wenn dein Server auch 5.0.x ist...


----------



## Donar (23. Okt 2008)

Nun habe ich die Datei "mysql-connector-java-5.0.8-bin" (Server Info ergab: 5.0.51b-community) in mein WEB-INF/lib Verzeichnis gelegt Tomcat neu gestartet und die JSP Datei getestet. Nach langem warten entsand folgende Fehlermeldung: 


```
org.apache.jasper.JasperException: javax.servlet.ServletException: com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

com.mysql.jdbc.CommunicationsException
MESSAGE: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.io.EOFException
MESSAGE: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.

STACKTRACE:

java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
	at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1997)
	at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:573)
	at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1044)
	at com.mysql.jdbc.Connection.createNewIO(Connection.java:2775)
	at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
	at java.sql.DriverManager.getConnection(DriverManager.java:582)
	at java.sql.DriverManager.getConnection(DriverManager.java:185)
	at org.apache.jsp.jsptest_jsp._jspService(jsptest_jsp.java:59)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	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:175)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
	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:286)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
	at java.lang.Thread.run(Thread.java:619)


** END NESTED EXCEPTION **



Last packet sent to the server was 1 ms ago.

STACKTRACE:

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.io.EOFException
MESSAGE: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.

STACKTRACE:

java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
	at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1997)
	at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:573)
	at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1044)
	at com.mysql.jdbc.Connection.createNewIO(Connection.java:2775)
	at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
	at java.sql.DriverManager.getConnection(DriverManager.java:582)
	at java.sql.DriverManager.getConnection(DriverManager.java:185)
	at org.apache.jsp.jsptest_jsp._jspService(jsptest_jsp.java:59)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	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:175)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
	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:286)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
	at java.lang.Thread.run(Thread.java:619)


** END NESTED EXCEPTION **



Last packet sent to the server was 1 ms ago.
	at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:641)
	at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1044)
	at com.mysql.jdbc.Connection.createNewIO(Connection.java:2775)
	at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
	at java.sql.DriverManager.getConnection(DriverManager.java:582)
	at java.sql.DriverManager.getConnection(DriverManager.java:185)
	at org.apache.jsp.jsptest_jsp._jspService(jsptest_jsp.java:59)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	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:175)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
	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:286)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
	at java.lang.Thread.run(Thread.java:619)


** END NESTED EXCEPTION **



Last packet sent to the server was 1 ms ago.
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:398)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
```


----------



## maki (23. Okt 2008)

Deine JDBC Connection URL ist falsch


```
localhost:80
```

Standard Por ist 3306, nicht 80.


----------



## Donar (23. Okt 2008)

Du glaubst gar nicht wie glücklich ich bin. Jetzt funktioniert die Verbindung. Du hast mir die Woche gerettet. Vielen vielen Dank!


----------

