Announcement

Collapse
No announcement yet.

Connecting to Mysql from tomcat

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Connecting to Mysql from tomcat

    I currently have a website with eukhost which uses php/mysql to gather appropriate information from users.

    Separately I am running a tomcat web app back home.

    I wish to interrogate the mysql DB from my Tomcat web app, but cant seem to make the connection. I have connected to mysql DBs locally, but connecting to my online website I get - Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL '92.48.108.230 (my server IP)'.

    I am using the following attributes to make the connection
    <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="92.48.108.230" />
    <property name="username" value="xxxxxxx" />
    <property name="password" value="xxxxxxx" />
    </bean>

    If anyone can see any obvious answer please let me know!

  • #2
    Hi,

    There are several reasons for this error. One being misplaced/incorrect file/folder location.
    Make sure you have the JDBC jar file containing the driver placed in your WEB-INF/lib [%Tomcat Home%/common/lib] directory..

    Did you add the <Context> element to your scripts so that it looks like:
    <context path="/yourApp" docBase="yourApp" debug="1" reloadable="true">

    Also, can you try change the <property name="url" value="92.48.108.230" /> to <property name="url" value="localhost" /> ?
    Rock _a.k.a._ Jack Daniel

    Follow eUKhost on Twitter || Join eUKhost Community on Facebook

    Comment


    • #3
      Thanks for your efforts. In the end the url needed to be 'jdbc:mysql://92.48.108.230' - it wasnt going to be localhost as the database was remote, just needed the full syntax. Lack of sensible error messages are the plague of developers lives!

      Comment


      • #4
        Originally posted by Stu Trigg View Post
        Thanks for your efforts. In the end the url needed to be 'jdbc:mysql://92.48.108.230' - it wasnt going to be localhost as the database was remote, just needed the full syntax. Lack of sensible error messages are the plague of developers lives!
        Glad that you got it sorted

        Here's the code I use when connecting to the MySQL server locally, but if you need to connect it to the remote MySQL server, replace localhost with the IP address of the remote MySQL server
        PHP Code:
        import java.sql.*;

           public class 
        Connect
           
        {
               public static 
        void main (String[] args)
               {
                   
        Connection conn null;

                   try
                   {
                       
        String userName "testuser";
                       
        String password "testpass";
                       
        String url "jdbc:mysql://localhost/test";
                       Class.
        forName ("com.mysql.jdbc.Driver").newInstance ();
                       
        conn DriverManager.getConnection (urluserNamepassword);
                       
        System.out.println ("Database connection established");
                   }
                   catch (
        Exception e)
                   {
                       
        System.err.println ("Cannot connect to database server");
                   }
                   finally
                   {
                       if (
        conn != null)
                       {
                           try
                           {
                               
        conn.close ();
                               
        System.out.println ("Database connection terminated");
                           }
                           catch (
        Exception e) { /* ignore close errors */ }
                       }
                   }
               }
           } 
        Compile n enjoy :wink:
        Rock _a.k.a._ Jack Daniel

        Follow eUKhost on Twitter || Join eUKhost Community on Facebook

        Comment


        • #5
          Hi,

          First I would like to apologise for digging up an old thread, but i think I have a very similar problem to the OP and despite countless discussions with wonderful tech people here, I am not getting any further along.

          Basically I have a created an questionnaire application that saves peoples answers to a database. I have the application up an running at www. strathegy .com but I am unable to connect the application to the database. Basically whenever someone tries to submit their answers to the questionnaire, the following error message is thrown:

          Code:
          javax.servlet.ServletException: Cannot open connection
          	org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:535)
          	org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:433)
          	org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
          	org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
          	org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
          	javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
          	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
          
          root cause
          
          java.sql.SQLException: Access denied for user 'stratheg'@'localhost' (using password: YES)
          	com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
          	com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
          	com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
          	com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
          	com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:910)
          My database connection file (hibernate.cfg.xml) contains the following:

          Code:
          <?xml version='1.0' encoding='UTF-8'?>
          <!DOCTYPE hibernate-configuration PUBLIC
                    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
          
          <!-- Generated by MyEclipse Hibernate Tools.                   -->
          <hibernate-configuration>
          
          	<session-factory>
          		<property name="connection.username">stratheg_admin</property>
          		<property name="connection.url">jdbc:mysql://localhost:3306/stratheg_Questionnaire</property>
          		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
          		<property name="myeclipse.connection.profile">stratheg_Questionnaire</property>
          		<property name="connection.password">XXXXXXXXX</property>
          		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
          		<mapping resource="com/questionnaire/core/hibernate/Questionnaire.hbm.xml" />
          
          	</session-factory>
          
          </hibernate-configuration>
          Does anyone know what might be wrong? I am guessing the problem lies with the connection url, but I am not 100% sure.
          Also, my connection username is stratheg_admin, but the error message throws 'Access denied for user 'stratheg'@'localhost' ' which is puzzling...

          The technical ppl here are telling me to consult my application developer... but I am the developer that wrote this thing and it works fine on my local machine... I am just trying to deploy it now and I am having difficulties.

          Been stuck with this problem for most of the day, any ideas guys?

          Thanks,
          -Ko

          Comment


          • #6
            hi,

            I guess your JVM is not getting the MySQL classes for connectivity. So why don't you check the classpath first.

            Thank you.

            Comment

            Working...
            X