Java JDBC MySQL Connection getDbConnection()

Here you can find the source of getDbConnection()

Description

Gets a connection to the database.

License

Open Source License

Exception

Parameter Description
ClassNotFoundException If the MySQL connector cannot be found.
SQLException If the connection cannot be established.

Return

The connection to the database.

Declaration

public static Connection getDbConnection() throws ClassNotFoundException, SQLException 

Method Source Code

//package com.java2s;
/**//  w  w  w. ja v a2s  .c o m
 *  This file is part of the LDIRBackend - the backend for the Let's Do It
 *  Romania 2011 Garbage collection campaign.
 *  Copyright (C) 2011 by the LDIR development team, further referred to 
 *  as "authors".
 *
 *  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/>.
 *
 *  Filename: DatabaseHelper.java
 *  Author(s): Stefan Guna, svguna@gmail.com
 *
 */

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

public class Main {
    /** The host where the database is located. */
    public static final String DB_HOST = "localhost";
    /** The database name. */
    public static final String DB_NAME = "ldir";
    /** The password to access the database. */
    public static final String DB_PASSWORD = "ldir";
    /** The username to access the database. */
    public static final String DB_USER = "ldir";

    /**
     * Gets a connection to the database.
     * 
     * @return The connection to the database.
     * @throws ClassNotFoundException
     *             If the MySQL connector cannot be found.
     * @throws SQLException
     *             If the connection cannot be established.
     */
    public static Connection getDbConnection() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connect = DriverManager.getConnection(
                "jdbc:mysql://" + DB_HOST + "/" + DB_NAME + "?" + "user=" + DB_USER + "&password=" + DB_PASSWORD);
        return connect;
    }
}

Related

  1. getConnection(String ip, String db, String user, String passWord)
  2. getConnetion(String url, String username, String password)
  3. getcount()
  4. getDBConn()
  5. getDBConnection()
  6. getDBConnection(String database)
  7. getDBConnection(String dbServer, String dbName, String dbUser, String dbPass)
  8. getDBConnection(String urlFormat, String host, Integer port, String database, String user, String password)
  9. getDMPConn()