Here you can find the source of getConnection(String driverName, String urlprefix, String host, String port, String database, String user, String password)
Parameter | Description |
---|---|
driverName | a parameter |
urlprefix | a parameter |
host | a parameter |
port | a parameter |
database | a parameter |
user | a parameter |
password | a parameter |
Parameter | Description |
---|---|
ClassNotFoundException | an exception |
SQLException | an exception |
public static Connection getConnection(String driverName, String urlprefix, String host, String port, String database, String user, String password) throws ClassNotFoundException, SQLException
//package com.java2s; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; public class Main { /**/*from w ww .jav a 2s . c o m*/ * JDBC driver name */ public static final String DRIVER_NAME = "DRIVER_NAME"; /** * URL prefix for JDBC access */ public static final String URL_PREFIX = "URL_PREFIX"; /** * Host computer for the database */ public static final String HOST = "HOST"; /** * Port where database access is available */ public static final String PORT = "PORT"; /** * Name of the database */ public static final String DATABASE = "DATABASE"; /** * User name */ public static final String USER = "USER"; /** * Password */ public static final String PASSWORD = "PASSWORD"; /** * Returns a database connection given the specified properties * * @param properties * @return * @throws ClassNotFoundException * @throws SQLException */ public static Connection getConnection(Properties properties) throws ClassNotFoundException, SQLException { String driverName = properties.getProperty(DRIVER_NAME); String urlprefix = properties.getProperty(URL_PREFIX); String host = properties.getProperty(HOST); String port = properties.getProperty(PORT); String database = properties.getProperty(DATABASE); String user = properties.getProperty(USER); String password = properties.getProperty(PASSWORD); if (!(urlprefix.endsWith("://") || urlprefix.endsWith(":@//"))) { throw new RuntimeException("URL prefix must end with ://"); } return getConnection(driverName, urlprefix, host, port, database, user, password); } /** * Creates a database connection specific to the input arguments * * @param driverName * @param urlprefix * @param host * @param port * @param database * @param user * @param password * @return * @throws ClassNotFoundException * @throws SQLException */ public static Connection getConnection(String driverName, String urlprefix, String host, String port, String database, String user, String password) throws ClassNotFoundException, SQLException { Class.forName(driverName); String url = urlprefix + host + ":" + port + "/" + database; return DriverManager.getConnection(url, user, password); } }