tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sonja Löhr <sonjalo...@arcor.de>
Subject Re: Cannot connect to database using DataSource
Date Wed, 14 Sep 2005 09:16:42 GMT
Hi!


For TC 5.5.x, put the name-value-pairs of all <parameter> elements
inside the Resource start tag as attributes.

Good luck!


Am Mittwoch, den 14.09.2005, 16:30 +0800 schrieb Michael Lai:
> 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:
> 
> <Context path="" docBase="/home/tomcat/applications/"
>          debug="0" reloadable="true" >
>     <Resource name="jdbc/DBTest" type="javax.sql.DataSource"
>               auth="Container" />
>     <ResourceParams name="jdbc/DBTest">
>          <parameter>
>              <name>username</name>
>              <value>javauser</value>
>          </parameter>
>          <parameter>
>              <name>password</name>
>              <value>javadude</value>
>          </parameter>
>          <parameter>
>              <name>driverClassName</name>
>              <value>com.mysql.jdbc.Driver</value>
>          </parameter>
>          <parameter>
>              <name>url</name>
>              
> <value>jdbc:mysql://localhost/javatest?autoReconnect=true</value>
>          </parameter>
>     </ResourceParams>
> </Context>
> 
> 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 {
>    
>     response.setContentType("text/html");
>     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) {
>         e.printStackTrace();
>     }
>    
>     if (conn != null) {
>         out.println("Connection successful");
>     } else {
>         out.println("Unsuccessful");
>     }
> }
> 
> 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="http://java.sun.com/xml/ns/j2ee" >
>   <resource-ref>
>       <res-ref-name>jdbc/DBTest</res-ref-name>
>       <res-type>javax.sql.DataSource</res-type>
>       <res-auth>Container</res-auth>
>   </resource-ref>
> 
>   <servlet>
>     <servlet-name>TestSQL</servlet-name>
>     <servlet-class>TestSQL</servlet-class>
>   </servlet>
>   <servlet-mapping>
>     <servlet-name>TestSQL</servlet-name>
>     <url-pattern>/servlet/TestSQL</url-pattern>
>   </servlet-mapping>
> </web-app>
> 
> 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.
> 
> Michael.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message