Using the DriverManager Class
Connecting to your DBMS with the DriverManager class involves calling the method DriverManager.getConnection. The following method, JDBCTutorialUtilities.getConnection, establishes a database connection:
public Connection getConnection() throws SQLException {
Connection conn = null;
Properties connectionProps = new Properties();
connectionProps.put("user", this.userName);
connectionProps.put("password", this.password);
if (this.dbms.equals("mysql")) {
conn = DriverManager.
getConnection("jdbc:" + this.dbms + "://" + this.serverName +
":" + this.portNumber + "/", connectionProps);
} else if (this.dbms.equals("derby")) {
conn = DriverManager.
getConnection("jdbc:" + this.dbms + ":" + this.dbName + ";create=true", connectionProps);
}
System.out.println("Connected to database");
return conn;
}