Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problems with connecting to mySQL using org.gjt.mm.mysql.Driver with Tomcat

843854Jul 4 2002 — edited Sep 28 2003
Hi Everybody,

I am using Tomcat, mySQL and the org.gjt.mm.mysql.Driver JDBC driver.

I am just doing some testing at the moment, with an example servlet from an online tutorial (source below). When I try to use the servlet by accessing:

http://localhost:8080/mine/DBtest

I get the text below:


Hi There again
retrieving data now
Caught SQL Exception: java.sql.SQLException: Server configuration denies access to data source

So Tomcat is working, as the page displays. The JDBC driver is functioning properly as it connects to the server, but is denied access. All I can assume is that I need to reconfigure the mySQL server in some way to allow access by Tomcat through the JDBC driver.

Any help anyone?

Best,

Joel

(Code listing follows)




// Copyright � 2001 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.


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

/**
* My test servlet
*
* @author Liz Warner
*/

public class DBtest extends HttpServlet {


public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{

response.setContentType("text/html");
PrintWriter out = response.getWriter();

//print the HTML header </font>
out.println("<html>");
out.println("<head>");
out.println("<title> Hola </title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<h1> Hi There again </h1>");
out.println("retrieving data now <br>");


// try to make a JDBC connection to the "test" database
// and retrieve some data

String DBUrl = "jdbc:mysql:///test";
Connection conn = null;
Statement stmt = null;
try
{
try
{
//create a new instance of the mysql driver
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}
catch (Exception Ex)
{
out.println("Unable to Load Driver: " + Ex.toString() + "<br>");
}
// get a new connection object
conn = DriverManager.getConnection(DBUrl);
// get a new statement object
stmt = conn.createStatement();
// and execute a simple query
ResultSet rs = stmt.executeQuery("SELECT * from foo");

while (rs.next())
{
// iterate through the result set and print
// the query results to the page
out.println("bar = " + rs.getString("bar") + "<br>");
}

}
catch (SQLException sqlEx)
{
out.println("Caught SQL Exception: " + sqlEx.toString() + "<br>");
}

// now close the statement and connection if they exist
if (stmt != null)
{
try
{
stmt.close();
}
catch (SQLException sqlEx)
{
out.println("Could not close: " + sqlEx.toString() + "<br>");
}
}
if (conn != null)
{
try
{
conn.close();
}
catch (SQLException sqlEx)
{
out.println("Could not close: " + sqlEx.toString() + "<br>");
}
}
out.println("</body>");
out.println("</html>");
}
}

(Code listing ends)

And I have created the test database and filled it with some data:

mysql> create table foo (bar varchar(16), foo_id int auto_increment not null primary key);
mysql> insert into foo (bar) values ("hi");
mysql> insert into foo (bar) values ("hola");

(Tutorial being followed is http://developer.apple.com/internet/java/tomcat2.html)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 26 2003
Added on Jul 4 2002
6 comments
1,037 views