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

    Unanswered: MYSQL and Tomcat Connection

    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());
    }



    /*

    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
    Oct 2003
    Location
    Slovakia
    Posts
    482

    Re: MYSQL and Tomcat Connection

    Originally posted by tej
    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());
    }



    /*

    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;

    }
    }
    This is Java related question, please go to JAVA section

  3. #3
    Join Date
    Mar 2004
    Posts
    16

    Re: MYSQL and Tomcat Connection

    try loading the class before defining the connection.

Posting Permissions

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