tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Lai>
Subject Cannot connect to database using DataSource
Date Wed, 14 Sep 2005 08:30:39 GMT
I have a MySQL database in which I created a database named, javatest, 
and I am using Tomcat 5.5.9.  I have unable to connect my database using 
Java's DataSource method.  Here is my ROOT.xml setup for my application 

<Context path="" docBase="/home/tomcat/applications/"
         debug="0" reloadable="true" >
    <Resource name="jdbc/DBTest" type="javax.sql.DataSource"
              auth="Container" />
    <ResourceParams name="jdbc/DBTest">

I created a simple servlet to test the database connection:

public class TestSQL extends HttpServlet {
    private Connection conn = null;

    public void doGet(HttpServletRequest request,
              HttpServletResponse response) throws IOException, 
ServletException {
    PrintWriter out = response.getWriter();

    try {
        Context ctx = new InitialContext();
        Context appContext = (Context)ctx.lookup("java:comp/env");
        DataSource ds = (DataSource)appContext.lookup("jdbc/DBTest");
        conn = ds.getConnection();
    } catch (Exception e) {
    if (conn != null) {
        out.println("Connection successful");
    } else {

It prints out "Unsuccessful".  I also tried using the traditional 
DriverManager and that works.  Here is my web.xml in case it is needed 
for analysis:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="2.4" uri="" >


I would appreciate it very much if someone can have a look at this and 
tell me what I am doing wrong with the DataSource alternative.


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message