Java JDBC PostgreSQL Connection getPostgresSQLConnection(String hostname, int portNumber, String databaseName, String dbuser, String dbpasswort)

Here you can find the source of getPostgresSQLConnection(String hostname, int portNumber, String databaseName, String dbuser, String dbpasswort)

Description

Gets the postgres sql connection from the given parameters.

License

Apache License

Parameter

Parameter Description
hostname the hostname
portNumber the port number
databaseName the database name
dbuser the dbuser
dbpasswort the dbpasswort

Exception

Parameter Description
ClassNotFoundException the class not found exception
SQLException the sQL exception

Return

the postgres sql connection

Declaration

public static Connection getPostgresSQLConnection(String hostname, int portNumber, String databaseName,
        String dbuser, String dbpasswort) throws ClassNotFoundException, SQLException 

Method Source Code

//package com.java2s;
/**//  w  w w . j a  va 2 s  . co m
 * Copyright (C) 2007 Asterios Raptis
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

public class Main {
    /** Constant for the drivername from PostgreSQL-database. */
    public static final String POSTGRESQL_DRIVERNAME = "org.postgresql.Driver";
    /** Constant for the urlprefix from PostgreSQL-database. */
    public static final String POSTGRESQL_PREFIX_URL = "jdbc:postgresql://";

    /**
     * Gets the postgres sql connection from the given parameters.
     * 
     * @param hostname
     *            the hostname
     * @param portNumber
     *            the port number
     * @param databaseName
     *            the database name
     * @param dbuser
     *            the dbuser
     * @param dbpasswort
     *            the dbpasswort
     * @return the postgres sql connection
     * @throws ClassNotFoundException
     *             the class not found exception
     * @throws SQLException
     *             the sQL exception
     */
    public static Connection getPostgresSQLConnection(String hostname, int portNumber, String databaseName,
            String dbuser, String dbpasswort) throws ClassNotFoundException, SQLException {
        StringBuilder sb = new StringBuilder();
        sb.append(POSTGRESQL_PREFIX_URL);
        sb.append(hostname);
        sb.append(":");
        sb.append(portNumber);
        sb.append("/");
        sb.append(databaseName);
        Class.forName(POSTGRESQL_DRIVERNAME);
        return DriverManager.getConnection(sb.toString().trim(), dbuser, dbpasswort);
    }
}

Related

  1. getDBConnection()
  2. getListDatabaseFromMaster()
  3. getPostgresConnection()
  4. getPostgresConnection()
  5. getPostgresJDBCConnection(String host, int port, String nameDB, String userDB, String passwordDB)
  6. resetTestDB()