JDBC Tutorial - JDBC Select Database








The following code shows how to select a database using the JDBC connection URL.

The code hardcoded the database name in the JDBC connection URL as follows.

jdbc:mysql://localhost/STUDENTS

Example

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
/*from   w  w  w  . j a va 2 s.co m*/
public class Main {
  static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";

  static final String USER = "username";
  static final String PASS = "password";

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

    Class.forName(JDBC_DRIVER);
    conn = DriverManager.getConnection(DB_URL, USER, PASS);

    stmt.close();
    conn.close();
  }
}