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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pulling my hair out with MySql and tomcat!!

Status
Not open for further replies.

gwu

MIS
Dec 18, 2002
239
US
I have been able to get mysql to work with Java but not with tomcat. When i pull up index.jsp in my browser, I get this: "The user is equal to: aaaa" (in TestSQLLoad.java, the variable "user" is init'd with a value of "aaaa"). Can somone tell me why tomcat is not correctly working with mysql???

thanks

mysql-connector-java-3.0.11-stable-bin.jar is in currently in /usr/local/jakarta/common/lib


index.jsp:
Code:
<html>
  <body>
  <%package1.TestSQLLoad tst = new package1.TestSQLLoad();
    tst.init();%>
    The user is equal to: <%= tst.getUser() %>
  </body>
</html>

part of WEB-INF/web.xml:
Code:
<web-app>
 <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/mysql</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
</web-app>

part of server.xml:
Code:
<Host name="localhost" debug="0" appBase="webapps" unpackWARs="true" autoDeploy="true"  xmlValidation="false" xmlNamesp$
<Logger className="org.apache.catalina.logger.FileLogger" directory="logs"  prefix="localhost_log." suffix=".txt" tim$t" timestamp="true"/>
<Context path="" docBase="/usr/local/apache2/htdocs" debug="0" reloadable="true" crossContext="true">
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/mysql">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>5</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<parameter>
<name>username</name>
<value>****</value>
</parameter>
<parameter>
<name>password</name>
<value>****</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/mysql?autoReconnect=true</value>
</parameter>
</ResourceParams>
</Context>
</Host>

WEB-INF/classes/package1/TestSQLLoad.java:
Code:
package package1;

import javax.naming.*;
import javax.sql.*;
import java.sql.*;

public class TestSQLLoad{

        String user = "nothing yet!!!";
        public void init() {
                try{
                        String user = "aaaa";
                        Context ctx = new InitialContext();
                        if(ctx == null ) {
                                throw new Exception("Boom - No Context");
                        }
                        DataSource ds = (DataSource)ctx.lookup("java:jdbc/mysql");
                        if (ds != null) {
                                Connection conn = ds.getConnection();
                                if(conn != null) {
                                        user = "Got Connection "+conn.toString();
                                        Statement stmt = conn.createStatement();
                                        String q = "select user from user";
                                        ResultSet rst = stmt.executeQuery(q);
                                        if(rst.next()) {
                                                user=rst.getString(1);
                                        }
                                        conn.close();
                                }
                        }
                }catch(Exception e) {
                        e.printStackTrace();
                }
        }
        public String getUser() {
                return user;
        }
}
 
Put the following in your code to determine where the process is failing...

Code:
if (ds != null) {
      Connection conn = ds.getConnection();
      [COLOR=red]user="Datasource ok";[/color]
      if(conn != null) {...


It's hard to tell if the problem is with your datasource or with your connection.

I've gotten this to work when running under windows, but have not been successful running under linux. For so me reason, the datasource comes back null when I run it under linux.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top