Hi ! I have a problem in connecting JSP pages with My SQL database using JDBC connector. I m using:

  • For TOMCAT -- apache-tomcat-5.5.23
  • For JRE -- Java EE 5 SDK
  • For database -- MySQL 5.0.37
  • For JDBC -- mysql-connector-java-3.0.17

JSP pages other than database working fine. I have copied mysql-connector-java-3.0.17-ga-bin.jar file in apache-tomcat-5.5.23\Tomcat 5.5\common\lib folder also for it. It is giving me error on connection line.

My Database structure is : Database: register, table: login, fields are: name, password, message.

I don’t know whether it’s a problem with compatibilities of different applications or there is something wrong with the code. Here is the code:

<%@ page import="java.sql.*" %>
<html><body>

<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:8009/login? user=root&password=asma");
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery("SELECT message FROM users");

while (rs.next()) {
out.println(rs.getString("message")+"<br>");
}

rs.close();statement.close();con.close();
%>

</body></html>

If anyone can help me I’ll be really greateful
And the error is :

HTTP Status 500 -


type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Exception in JSP: /connect1.jsp:6 3: 4: <%5: Class.forName("com.mysql.jdbc.Driver").newInstance();6: Connection con = DriverManager.getConnection("jdbc:mysql://localhost:8009/login? user=root&password=asma");7: Statement statement = con.createStatement();8: ResultSet rs = statement.executeQuery("SELECT message FROM users");9: Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:467) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:371) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265) javax.servlet.http.HttpServlet.service(HttpServlet.java:803)root cause
javax.servlet.ServletException: Communication link failure: java.io.EOFException, underlying cause: null ** BEGIN NESTED EXCEPTION ** java.io.EOFException STACKTRACE: java.io.EOFException at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1395) at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:1414) at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:625) at com.mysql.jdbc.Connection.createNewIO(Connection.java:1808) at com.mysql.jdbc.Connection.<init>(Connection.java:452) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at org.apache.jsp.connect1_jsp._jspService(connect1_jsp.java:48) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174) 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:151) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870) 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:685) at java.lang.Thread.run(Unknown Source) ** END NESTED EXCEPTION ** org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:846) org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779) org.apache.jsp.connect1_jsp._jspService(connect1_jsp.java:67) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265) javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

Make shure your MySQL database is runnig ( I sometimes forget I switch it off)
I'm using Windows XP and my JDBC connector is in "C:\Program Files\Java\jdk1.6.0\jre\lib\ext" plus you should place there servlet-api.jar and jsp-api.jar which you can find in your Tomcat directory (in my case C:\Tomcat 5.5\common\lib)
This should take care of the problem, if no you still can chceck if you installed everything correctly on this site

One thing, you SHOULD NOT use JSP for connection to your database but you should use servlets. JSP is still capable of connecting to DB but this is left there for back compactibility with some old components. If you use book you should get newer edition, if is it teacher tell him/her to update his/her notes

Do NOT put ANYTHING in the lib/ext directory of your JRE or JDK.
VERY VERY VERY bad!

Instead set up your classpath properly, as indicated in the documentation of the JDK. JRE, appservers, etc. etc. etc.

Do NOT put ANYTHING in the lib/ext directory of your JRE or JDK.
VERY VERY VERY bad!

jwenting this is not hopefull ! Please can you explain...
It is the case that teacher at school tell you set up Java this way and that is. We do not question them. So you as the programmer with years of experience should explain or point to the place where appropriate resources avaible(and no just go to sun website and find it). Not to just drop a shell bomb saing "that is bad, bad..."

Hello !!
Thankx for all ur reply's. Actually i m very new to this jsp programming , i dont really know much abt all this, just followng a few books and tutorials to make it work, but i think u r right on all that they used the older versions that is not even available online, and may be i m having version incompatibility or version conflict problem.

Anyways can anyone tell me that if i am using apache-tomcat-5.5.23, so do i still need to install apache server separately and make connection with tomcat later on??

As I m using different working codes from some books and websites, they usually give me error on my connection string and some root errors like:

org.apache.jasper.JasperException: Unable to compile class for JSP:

org.apache.jasper.JasperException: Communication link failure: java.io.EOFException, underlying cause: null

My connection string is

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:8009/login? user=root&password=asma");

Root would be the default username as i haven't set it and "asma" would be the password that i set wile installing MySQL server.

My all other jsp pages working fine , only having problem in connecting with database.

I have tried ur suggestion of putting the files as well as seting CLASSPATH but neither of it worked.

I'll be thankful if anyone can help in this regard

In mysql you not only have to create a user and set a password, you also have to designate from where (and how) that user is allowed to connect the DB. Per default, root can only connect from localhost using the sock file, but, in Java, you will be attempting to connect over TCP. So, read the MySQL documentation (namely the GRANT statement) and add a root@localhost entry.

jwenting this is not hopefull ! Please can you explain...
It is the case that teacher at school tell you set up Java this way and that is. We do not question them. So you as the programmer with years of experience should explain or point to the place where appropriate resources avaible(and no just go to sun website and find it). Not to just drop a shell bomb saing "that is bad, bad..."

placing anything there will mask the real issues, which is that you have no clue about configuring your runtime properly (including classpath settings especially).
It's also guaranteed to lead to the Java equivalent of DLL Hell in short order, as many applications require different versions of the same libraries and those often conflict.

By placing one version there you're shoving it down the throat of every Java application you run on your system as the one and only true version, leading to all kinds of problems.

If you need a library, get it from where you can get it (which differs for each library) and add it to your classpath as required for the lifetime of the JVM when running the application.
So NO placing anything in lib/ext, NO global classpath settings.

In mysql you not only have to create a user and set a password, you also have to designate from where (and how) that user is allowed to connect the DB. Per default, root can only connect from localhost using the sock file, but, in Java, you will be attempting to connect over TCP. So, read the MySQL documentation (namely the GRANT statement) and add a root@localhost entry.

Hello

I have tried that option as well with command
GRANT ALL PRIVILEGES ON 'login'.* TO [EMAIL="'root'@'localhost:8009'"]'[EMAIL="'root'@'localhost:8009"]root'@'localhost:8009[/EMAIL]'
IDENTIFIED BY 'mdas' WITH GRANT OPTION;

but it still giving the error on connection string with the same communication link root cause:


exception org.apache.jasper.JasperException: Exception in JSP: /connect1.jsp:63: 4: <%5: Class.forName("com.mysql.jdbc.Driver").newInstance();6: Connection con = DriverManager.getConnection("jdbc:mysql://localhost:8009/login? user='root'&password='mdas'");7: Statement statement = con.createStatement();8: ResultSet rs = statement.executeQuery("SELECT message FROM users");9: Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:467) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:371) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265) javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

root cause javax.servlet.ServletException: Communication link failure: java.io.EOFException, underlying cause: null** BEGIN NESTED EXCEPTION ** java.io.EOFExceptionSTACKTRACE:java.io.EOFException at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1395) at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:1414) at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:625) at com.mysql.jdbc.Connection.createNewIO(Connection.java:1808) at com.mysql.jdbc.Connection.<init>(Connection.java:452) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at org.apache.jsp.connect1_jsp._jspService(connect1_jsp.java:48) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174) 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:151) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870) 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:685) at java.lang.Thread.run(Unknown Source)** END NESTED EXCEPTION **
whta can i do please HELP ME.

What DB version are you using, and what version of the driver are you using.

What DB version are you using, and what version of the driver are you using.

For database -- MySQL 5.0.37
For JDBC -- mysql-connector-java-3.0.17

Try downloading a newer version of the Driver.

I have tried using it with the newer version but its giving error again at the connection string and having root cause:
javax.servlet.ServletException: Communications link failure due to underlying exception:

A few ambiguities more to ask

1-- Can any one tell me that if i am using apache-tomcat-5.5.23, so do i still need to install apache server separately and make connection with tomcat later on

2-- If WEB-INF subdirectory of tomcat is to be targeted to create a WEB-INF\web.xml file and if i search this subdirectory i can find it in many flders like
webapps\ROOT, tomcat-docs, jsp-examples, servlets-examples, balance, webdav, tomcat-docs\appdev\sample\web,
server\webapps\manager, host-manager ,
so which one exactly has to be target. what i think is ROOT but need a confirmation.

3-- Do i need to add anything like modifying server.xml in tomcat directory , and i got 2 different resourse declaration in MySQL documentation and one in tomcat documentation, so which one to use and i cant get this
<Resource name="jdbc/MySQLDB"
auth="Container"
type="javax.sql.DataSource"/>
what would be MySQLDB in my case???
4-- I have my Jre folder both in C:\Program Files and within jdk folder as i installed java at some other folder rather than c:\ , so which one would my tomcat should use?

Now that you have a decent driver, I would say to change ("jdbc:mysql://localhost:8009/login? user='root'&password='mdas'"); to ("jdbc:mysql://localhost:8009/login", "root", "mdas"); Also, that space between the ? and the word user in the original url will cause problems anyway, even if the DriverManager does accept the url with user and password as parameters on the url, as there should not be a space there.

The web.xml is placed directly under WEB-INF, not any of its subdirectories.

And, no you do not need to install apache, but it is advised to use a real browser as the front end (in this case apache) and connect to the tomcat locally through the webserver (in this case using mod_jk).

Now that you have a decent driver, I would say to change ("jdbc:mysql://localhost:8009/login? user='root'&password='mdas'"); to ("jdbc:mysql://localhost:8009/login", "root", "mdas"); Also, that space between the ? and the word user in the original url will cause problems anyway, even if the DriverManager does accept the url with user and password as parameters on the url, as there should not be a space there.

The web.xml is placed directly under WEB-INF, not any of its subdirectories.

And, no you do not need to install apache, but it is advised to use a real browser as the front end (in this case apache) and connect to the tomcat locally through the webserver (in this case using mod_jk).

Tried that as well :sad: , i think the problem might not be with the code , it might be with the configuration, as i was looking at tomcat-apache documentation it says to create a WEB-INF/web.xml file even given teh code is given

<web-app 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"
version="2.4">
<description>MySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>

just need to ask a few questions before starting any new thing

1. the tomcat directory already has many WEB-INF/web.xml files at different locations , as i have mentioned before so do i need to modify any of it or do i have to create a new folder name WEB-INF and create the web.xml in it. and if this is teh case where should i create that WEB-INF folder with in $CATALINA_HOME/.

2. teh second question is what is jdbc/TestDB as it is also used in $CATALINA_HOME/conf/server.xml resourse tag as well and what would be it in my case??

<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="root" password="mdas" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:8009/login?autoReconnect=true"/>

3. I have my Jre folder both in C:\Program Files and within jdk folder as i installed java at some other folder rather than c:\ , so which one would my tomcat should use while directing for installaion?

if u can answer my ambiguities i'll be really thankful

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.