Passing 'user' and 'password' Parameters as a java.util.Properties Object to Connection object : Connection « Database « Java Tutorial






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








20.3.Connection
20.3.1.Connection Object
20.3.2.Connection Object Requirements
20.3.3.Connection Properties: Pass additional properties using java.util.Properties:
20.3.4.Passing 'user' and 'password' Parameters as a java.util.Properties Object to Connection object
20.3.5.Test to See if Your Connection is Alive
20.3.6.Vendor URL Formats
20.3.7.Connection's Method to Print Native SQL