Java JDBC Derby Connection getDatabaseConnection()

Here you can find the source of getDatabaseConnection()

Description

This function establishes the connection to the database.

License

Open Source License

Exception

Parameter Description
Exception If there are problems connecting to the database.

Return

The opened database connection.

Declaration

protected static Connection getDatabaseConnection() throws Exception 

Method Source Code


//package com.java2s;
/*//from w  w w  . j  a v  a 2 s  .  c  o m
 * Copyright (C) 2014 Frank Steiler <frank.steiler@steilerdev.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Main {
    /**
     * The URL to the database.
     */
    protected static String DBASE_URL = "jdbc:derby://localhost:1527/TheNetwork";
    /**
     * The username for the database.
     */
    protected static final String databaseUsername = "db_user";
    /**
     * The password for the database.
     */
    protected static final String databasePassword = "Password1!";

    /**
     * This function establishes the connection to the database.
     * @return The opened database connection.
     * @throws Exception If there are problems connecting to the database.
     */
    protected static Connection getDatabaseConnection() throws Exception {
        Connection con = null;

        try {
            con = DriverManager.getConnection(DBASE_URL, databaseUsername, databasePassword);
        } catch (SQLException sqle) {
            String msg = "Cannot establish a connection to the database";
            throw new Exception(msg, sqle);
        } finally {
            return con;
        }
    }
}

Related

  1. createXATransactionView(Statement s)
  2. deleteTestData()
  3. existsInSessionTable(String id, boolean verbose)
  4. getConnection()
  5. getConnection()
  6. getDefaultDerbyConnection()
  7. getDerbyConnection(String filename)
  8. getDerbyDatabaseProperty(Statement derbyStatementForQuerying, String propertyKey)
  9. getDerbyUnitConnection()