Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2004
    Posts
    2

    Unanswered: Mysql and Tomcat Connection

    We are having problems while connecting to database(MYSQL) with tomcat application server.
    The same connection works well when tomcat is not used.
    The version of tomcat used is 4.1 and jdk1.4.0 is used.
    We would like to know whether we have to use another connector (driver)
    We also tried putting the driver in tomcat\webinf\lib and putting it in classpath as well but in vain.
    Kindly let us know the solution .Its urgent.

    This is our class which provides connection to db.

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;



    public class Connect
    {
    public static Connection conn;

    /**
    * This method will return a connection to the database.
    * @return Connection: A connection object to the database.
    */

    Connection getConnection() throws Exception
    {
    if (conn == null)
    {
    String driverName = new String();
    if ((null == driverName) || "".equals(driverName))
    {
    driverName = "com.mysql.jdbc.Driver";
    }

    String connString = new String();
    if ((null == connString) || "".equals(connString))
    {
    connString = "jdbc:mysql://localhost:3306/project";
    //project is database name . 3306 is port number
    }

    String userName = new String();
    if ((null == userName) || "".equals(userName))
    {
    userName = "vartej";
    }


    String password = new String();
    if ((null == password) || "".equals(password))
    {
    password = "vartej";
    }

    // Driver driver = null;




    try
    {
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/project?user=vartej&password=vartej");

    // Do something with the Connection

    // ....
    }
    catch (SQLException ex)
    {
    // handle any errors
    System.out.println("SQLException: " + ex.getMessage());
    System.out.println("SQLState: " + ex.getSQLState());
    System.out.println("VendorError: " +ex.getErrorCode());
    //gives an error as: SQLExceptionriver not found SQL state: 08001
    }



    /*

    try
    {
    //Driver driver = (Driver)Class.forName(driverName).newInstance();
    driver = (Driver)Class.forName(driverName).newInstance();
    }
    catch(Exception e)
    {
    System.out.println("Driver Error");
    System.out.println(e.getMessage());
    }

    DriverManager.registerDriver(driver);
    conn = DriverManager.getConnection(connString, userName ,password);*/

    }

    return conn;

    }
    }
    Attached Files Attached Files

  2. #2
    Join Date
    May 2004
    Posts
    45
    I think you should be putting the driver in the $CATALINA_HOME/common/lib directory...
    Keith Brownlow
    ServerMetrics DB Monitoring (www.servermetrics.com)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •