Yurttas/PL/DBL/mysql-f-2010/02/00/01/04/dbops/DBConnect.java

  1/**
  2 *  Copyright(C) 2005
  3 *  All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc..
  4 *
  5 *  Permission to use, copy, modify, and distribute this
  6 *  software and its documentation for EDUCATIONAL purposes
  7 *  and without fee is hereby granted provided that this
  8 *  copyright notice appears in all copies.
  9 *
 10 *  @date   : February 2005, April 2010.
 11 *  @authorĀ : Salih Yurttas.
 12 */
 13
 14
 15package dbops;
 16
 17import java.sql.*;
 18import java.net.*;
 19
 20import java.util.*;
 21
 22public class DBConnect {
 23
 24  private static String driverClass = "com.mysql.jdbc.Driver";
 25
 26  private static String jdbcProtocol = "jdbc:mysql:";
 27
 28  private static String server = "//database2.cse.BitsOfCode Software Systems, Inc..edu";
 29
 30  private static String dbase = "/yurttas-Bank";
 31
 32  private static String username = "yurttas";
 33
 34  private static String password = "anything";
 35
 36  private static String connectionString = jdbcProtocol + server + dbase;
 37
 38  protected Connection con;
 39
 40  public DBConnect() {}
 41
 42  public DBConnect(final String server,
 43                   final String db) { 
 44    this.server = server;
 45    this.dbase = dbase;
 46  }
 47
 48  public DBConnect(final String server,
 49                   final String db,
 50                   final String username,
 51                   final String password) { 
 52    this.server = server;
 53    this.dbase = dbase;
 54    this.username = username;
 55    this.password = password;
 56  }
 57
 58  public DBConnect(final ArrayList<String> connectionStringData) {
 59    this.server = connectionStringData.get(0);
 60    this.dbase = connectionStringData.get(1);
 61    this.username = connectionStringData.get(2);
 62    this.password = connectionStringData.get(3);
 63  }
 64
 65  public boolean connect()
 66    throws SQLException
 67  {
 68    try {
 69      Class.forName(driverClass);
 70
 71      con = DriverManager.getConnection(connectionString,
 72                                        username,
 73                                        password);
 74
 75      if(con.isClosed()) return false;
 76    }
 77    catch(ClassNotFoundException cnfe) {
 78      throw new SQLException("The mySQL JDBC Driver could not be loaded.");
 79    }
 80
 81    return true;
 82  }
 83
 84  public boolean connect(final String username,
 85                         final String password) 
 86    throws SQLException
 87  {
 88    try {
 89      Class.forName(driverClass);
 90      con = DriverManager.getConnection(connectionString,
 91                                        username,
 92                                        password);
 93      if(con.isClosed()) return false;
 94    }
 95    catch(ClassNotFoundException cnfe) {
 96      throw new SQLException("The mySQL JDBC Driver could not be loaded.");
 97    }
 98
 99    return true;
100  }
101
102  public boolean disconnect()
103    throws SQLException
104  {
105    con.close();
106    return true;
107  }
108
109  public boolean disconnect(final Connection con) 
110    throws SQLException
111  {
112    con.close();
113    return true;
114  }
115
116  public Connection getConnect()
117    throws SQLException
118  {
119    return this.con;
120  }
121
122}