DriverManager: getConnection(String url, Properties info) : DriverManager « java.sql « Java by API






DriverManager: getConnection(String url, Properties info)

 

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

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

    Properties props = new Properties();
    props.put("user", user);
    props.put("password", password);

    props.put("autoReconnect", "true");

    return DriverManager.getConnection(dbURL, props);
  }

  public static void main(String[] args) {
    Connection conn = null;
    try {
      conn = getConnection("jdbc:mysql://localhost/empDB", "root", "pass");
    } catch (Exception ex) {
      System.out.println("SQLException: " + ex.getMessage());
    }
  }
}

   
  








Related examples in the same category

1.DriverManager: getConnection(String url) (jdbc:odbc:technical_library)
2.DriverManager: getConnection(String url, String user, String password)
3.DriverManager: getDrivers()
4.DriverManager: registerDriver(Driver driver)
5.DriverManager: setLogWriter(PrintWriter out)