How to Get a MySQL Connection from the Driver Manager

Started by karthikn, Jul 18, 2008, 08:26 PM

Previous topic - Next topic

karthikn

Get a MySQL Connection from the Driver Manager


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

public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = null;

    conn = getConnection("jdbc:mysql://localhost/tempDB", "root1", "rootpass");

    conn.close();
  }

  public static Connection getConnection(String dbURL, String user, String password)
      throws SQLException, ClassNotFoundException {
    Class.forName("com.mysql.jdbc.Driver");
    return DriverManager.getConnection(dbURL, user, password);
  }

}