DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   JSP (http://www.daniweb.com/forums/forum24.html)
-   -   Please help me in connecting JSP to MySql (http://www.daniweb.com/forums/thread73714.html)

A Yasir Mar 29th, 2007 6:09 am
Please help me in connecting JSP to MySql
 
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:63: 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.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 ** 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)




peter_budo Mar 29th, 2007 12:54 pm
Re: Please help me in connecting JSP to MySql
 
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

jwenting Mar 29th, 2007 2:53 pm
Re: Please help me in connecting JSP to MySql
 
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.

peter_budo Mar 29th, 2007 3:40 pm
Re: Please help me in connecting JSP to MySql
 
Quote:

Originally Posted by jwenting (Post 336180)
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..."

A Yasir Mar 30th, 2007 5:18 pm
Re: Please help me in connecting JSP to MySql
 
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

masijade Mar 31st, 2007 5:18 pm
Re: Please help me in connecting JSP to MySql
 
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 Apr 2nd, 2007 1:34 pm
Re: Please help me in connecting JSP to MySql
 
Quote:

Originally Posted by peter_budo (Post 336192)
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.

A Yasir Apr 3rd, 2007 9:47 am
Re: Please help me in connecting JSP to MySql
 
Quote:

Originally Posted by masijade (Post 337013)
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 'root'@'localhost:8009'
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.

masijade Apr 3rd, 2007 10:19 am
Re: Please help me in connecting JSP to MySql
 
What DB version are you using, and what version of the driver are you using.

A Yasir Apr 3rd, 2007 10:40 am
Re: Please help me in connecting JSP to MySql
 
Quote:

Originally Posted by masijade (Post 338143)
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


All times are GMT -4. The time now is 8:16 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC