Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Connect to MySQL using JSP

Status
Not open for further replies.

jodwyer

Programmer
Jul 12, 2001
5
US
Hi all,

Can somebody help me out with this JSP problem below please. I cannot get a connection to a MySQL database using JSP (on Linux/JDK1.2/Apache/Tomcat) with the mm.mysqlDriver from sourceforge.net. We have installed the driver but the problem seems to be that it cannot be 'loaded'?!? Here is the URL below:

And here is the code:
html>
<head>
<title>Testing MySQL with Tomcat</title>
</head>

<%@ page import=&quot;java.sql.*&quot; %>
<%
String connectionURL = &quot;jdbc:mysql://scweb.sandiego.edu:3306/db?user=user;password=pwd&quot;;
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
%>

<%
Class.forName(&quot;org.gjt.mm.mysql.Driver&quot;).newInstance();
connection = DriverManager.getConnection(connectionURL, &quot;&quot;, &quot;&quot;);
statement = connection.createStatement();
rs = statement.executeQuery(&quot;SELECT * FROM test&quot;);

while (rs.next()) {
out.println(rs.getString(&quot;testname&quot;)+&quot;<br>&quot;);
}
rs.close();
%>

<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
Whasssssupppppppppp Tomcat?
</body>
</html>

_________________________________________________________
Internal Servlet Error:

javax.servlet.ServletException: Unable to load class org.gjt.mm.mysql.Driver
at org.apache.jasper.runtime.PageContextImpl.handlePageException(Compiled Code)
at jsp._0002fjsp_0002fdbtest_00032_0002ejspdbtest2_jsp_0._jspService(Compiled Code)
at org.apache.jasper.runtime.HttpJspBase.service(Compiled Code)
at javax.servlet.http.HttpServlet.service(Compiled Code)
at org.apache.jasper.servlet.JspServlet$JspCountedServlet.service(Compiled Code)
at javax.servlet.http.HttpServlet.service(Compiled Code)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(Compiled Code)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(Compiled Code)
at org.apache.jasper.servlet.JspServlet.service(Compiled Code)
at javax.servlet.http.HttpServlet.service(Compiled Code)
at org.apache.tomcat.core.ServletWrapper.doService(Compiled Code)
at org.apache.tomcat.core.Handler.service(Compiled Code)
at org.apache.tomcat.core.ServletWrapper.service(Compiled Code)
at org.apache.tomcat.core.ContextManager.internalService(Compiled Code)
at org.apache.tomcat.core.ContextManager.service(Compiled Code)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(Compiled Code)
at org.apache.tomcat.service.TcpWorkerThread.runIt(Compiled Code)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(Compiled Code)
at java.lang.Thread.run(Compiled Code)

Root cause:
java.lang.ClassNotFoundException: Unable to load class org.gjt.mm.mysql.Driver
at org.apache.jasper.servlet.JasperLoader.findClass(Compiled Code)
at org.apache.jasper.servlet.JasperLoader.loadClass(Compiled Code)
at java.lang.ClassLoader.loadClass(Compiled Code)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Compiled Code)
at jsp._0002fjsp_0002fdbtest_00032_0002ejspdbtest2_jsp_0._jspService(Compiled Code)
at org.apache.jasper.runtime.HttpJspBase.service(Compiled Code)
at javax.servlet.http.HttpServlet.service(Compiled Code)
at org.apache.jasper.servlet.JspServlet$JspCountedServlet.service(Compiled Code)
at javax.servlet.http.HttpServlet.service(Compiled Code)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(Compiled Code)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(Compiled Code)
at org.apache.jasper.servlet.JspServlet.service(Compiled Code)
at javax.servlet.http.HttpServlet.service(Compiled Code)
at org.apache.tomcat.core.ServletWrapper.doService(Compiled Code)
at org.apache.tomcat.core.Handler.service(Compiled Code)
at org.apache.tomcat.core.ServletWrapper.service(Compiled Code)
at org.apache.tomcat.core.ContextManager.internalService(Compiled Code)
at org.apache.tomcat.core.ContextManager.service(Compiled Code)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(Compiled Code)
at org.apache.tomcat.service.TcpWorkerThread.runIt(Compiled Code)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(Compiled Code)
at java.lang.Thread.run(Compiled Code)

Thanks for any help - I'd really appreciate it!
 
It looks like the driver is not in the CLASSPATH of the servlet engine because it is looking in java.lang for the driver. Wushutwist
 
Hi again,

Thanks for the reply. I appreciate it. I seem to have gotten over the connection with a new driver install but there seems to be yet another problem. 'Invalid Authorization'... Does the code below look okay:

==============================================
<%@ page import=&quot;java.sql.*&quot; %>
<%
String connectionURL = &quot;jdbc:mysql://scweb.sandiego.edu:3306/db?user=user;password=pwd&quot;;
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
%>

<%
Class.forName(&quot;org.gjt.mm.mysql.Driver&quot;).newInstance();
connection = DriverManager.getConnection(connectionURL, &quot;&quot;, &quot;&quot;);
statement = connection.createStatement();
rs = statement.executeQuery(&quot;SELECT * FROM test&quot;);

==============================================

Here's the stack trace:

Thanks again for any help!

Error: 500
Location: /examples/jsp/dates/dbtest.jsp
Internal Servlet Error:

javax.servlet.ServletException: Invalid authorization specification:
Access denied for user: 'username;password@localhost.localdomain'
(Using password: YES)
at
org.apache.jasper.runtime.PageContextImpl.handlePageException(Compiled
Code)
at
jsp.dates._0002fjsp_0002fdates_0002fdbtest_0002ejspdbtest_jsp_7._jspService(Compiled
Code)
at org.apache.jasper.runtime.HttpJspBase.service(Compiled Code)
at javax.servlet.http.HttpServlet.service(Compiled Code)
at
org.apache.jasper.servlet.JspServlet$JspCountedServlet.service(Compiled
Code)
at javax.servlet.http.HttpServlet.service(Compiled Code)
at
org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(Compiled
Code)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(Compiled
Code)
at org.apache.jasper.servlet.JspServlet.service(Compiled Code)
at javax.servlet.http.HttpServlet.service(Compiled Code)
at org.apache.tomcat.core.ServletWrapper.doService(Compiled Code)
at org.apache.tomcat.core.Handler.service(Compiled Code)
at org.apache.tomcat.core.ServletWrapper.service(Compiled Code)
at org.apache.tomcat.core.ContextManager.internalService(Compiled
Code)
at org.apache.tomcat.core.ContextManager.service(Compiled Code)
at
org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(Compiled
Code)
at org.apache.tomcat.service.TcpWorkerThread.runIt(Compiled Code)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(Compiled
Code)
at java.lang.Thread.run(Compiled Code)
________________________________________________

Root cause:
java.sql.SQLException: Invalid authorization specification: Access
denied
for user: 'interaction;password@localhost.localdomain' (Using password:
YES)
at org.gjt.mm.mysql.MysqlIO.init(Compiled Code)
at org.gjt.mm.mysql.Connection.(Compiled Code)
at org.gjt.mm.mysql.Driver.connect(Compiled Code)
at java.sql.DriverManager.getConnection(Compiled Code)
at java.sql.DriverManager.getConnection(Compiled Code)
at
jsp.dates._0002fjsp_0002fdates_0002fdbtest_0002ejspdbtest_jsp_7._jspService(Compiled
Code)
at org.apache.jasper.runtime.HttpJspBase.service(Compiled Code)
at javax.servlet.http.HttpServlet.service(Compiled Code)
at
org.apache.jasper.servlet.JspServlet$JspCountedServlet.service(Compiled
Code)
at javax.servlet.http.HttpServlet.service(Compiled Code)
at
org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(Compiled
Code)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(Compiled
Code)
at org.apache.jasper.servlet.JspServlet.service(Compiled Code)
at javax.servlet.http.HttpServlet.service(Compiled Code)
at org.apache.tomcat.core.ServletWrapper.doService(Compiled Code)
at org.apache.tomcat.core.Handler.service(Compiled Code)
at org.apache.tomcat.core.ServletWrapper.service(Compiled Code)
at org.apache.tomcat.core.ContextManager.internalService(Compiled
Code)
at org.apache.tomcat.core.ContextManager.service(Compiled Code)
at
org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(Compiled
Code)
at org.apache.tomcat.service.TcpWorkerThread.runIt(Compiled Code)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(Compiled
Code)
at java.lang.Thread.run(Compiled Code)
 
Fixed the problem. For anybody who may be frustrated by the same problems as mentioned above, I first re-installed the mm.mysql driver and left the .jar file well alone, reflecting this in my classpaths.

The code is as follows for a simple MySQL connection using JSP:

String connectionURL = &quot;jdbc:mysql://localhost:3306/yourdatabasename?user=username&password=yourdbpassword&quot;;
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
%>


<html>
<head>
<title>Testing MySQL with Tomcat</title>
<%
Class.forName(&quot;org.gjt.mm.mysql.Driver&quot;).newInstance();
connection = DriverManager.getConnection(connectionURL);
statement = connection.createStatement();
rs = statement.executeQuery(&quot;SELECT * FROM test&quot;);

while (rs.next()) {
out.println(rs.getString(&quot;testname&quot;)+&quot;<br>&quot;);

_______________________

this should work for you.

 
Hi,

I have been struggling with the mm.mysql driver for a couple of weeks. I have the same problem as you. I tried pasting in your source code but it appears to be incomplete and I can't get it to work. Could you possibly post your working test code?

Once I get a successful connection I will be able to figure things out. I seem to be completely flummoxed right now.

Thanks a lot.
Stan
 
I am having the same problem with connectivity. ClassNotFound etc. The Driver comes as a .jar file and I have added a path to it in the System env and via the Tomcat startup (CLASSPATH that is ). I still get the same error. Am I doing something very wrong---Be gentle I'm a novice---!!!
 
Hi there

This might help with some of the problems above


I found the second option outlined at this address worked for me with Win 98, Tomcat 3.3 and mm.mysql-2.0.11

I'm not really sure if this is the correct way to do things however as I havent edited my CLASSPATH settings at all but every where else I read it said you had to do this...

Any feedback on whether this is the best way to get the driver to work are most appreciated.

Well at least it works.

Thanks
Justin X-) &quot;Creativity is the ability to introduce order into the randomness of nature.&quot; Eric Hoffer

Visit me at
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top