Yurttas/PL/DBL/postgres/F/02/00/01/03/dbops/DBConnect.java

From ZCubes Wiki
Jump to navigation Jump to search
  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 1, 2005.
 11 *  @author : Salih Yurttas.
 12 */
 13
 14
 15package dbops;
 16
 17import java.sql.*;
 18import java.net.*;
 19
 20public class DBConnect {
 21
 22  private static String driverClass = "com.mysql.jdbc.Driver",
 23                        url = "jdbc:mysql:",
 24                        suffix = "//database.cs.BitsOfCode Software Systems, Inc..edu/yurttas-Bank";
 25
 26  protected Connection con;
 27
 28  public DBConnect() {}
 29
 30  public DBConnect(String jdbcDriver,
 31                   String dbURL,
 32                   String urlSuffix) { 
 33    this.setDBInfo(jdbcDriver,
 34                   dbURL,
 35                   urlSuffix);
 36  }
 37
 38  public void setDBInfo(String jdbcDriver,
 39                        String dbURL,
 40                        String urlSuffix) { 
 41    driverClass = jdbcDriver;
 42    url = dbURL;
 43    suffix = urlSuffix;
 44  }
 45
 46  public boolean connect()
 47    throws SQLException
 48  {
 49    try {
 50      Class.forName("com.mysql.jdbc.Driver");
 51
 52      con = DriverManager.getConnection("jdbc:mysql://database.cs.BitsOfCode Software Systems, Inc..edu/yurttas-Bank",
 53                                        "tester",
 54                                        "tester");
 55
 56      if(con.isClosed()) return false;
 57    }
 58    catch(ClassNotFoundException cnfe) {
 59      throw new SQLException("The mySQL JDBC Driver could not be loaded.");
 60    }
 61
 62    return true;
 63  }
 64
 65  public boolean connect(String username,
 66                         String password) 
 67    throws SQLException
 68  {
 69    try {
 70      Class.forName(driverClass);
 71      con = DriverManager.getConnection(url+suffix,
 72                                        username,
 73                                        password);
 74      if(con.isClosed()) return false;
 75    }
 76    catch(ClassNotFoundException cnfe) {
 77      throw new SQLException("The mySQL JDBC Driver could not be loaded.");
 78    }
 79
 80    return true;
 81  }
 82
 83  public boolean disconnect(Connection con) 
 84    throws SQLException
 85  {
 86    con.close();
 87    return true;
 88  }
 89
 90  public boolean disconnect()
 91    throws SQLException
 92  {
 93    con.close();
 94    return true;
 95  }
 96
 97  public Connection getConnect()
 98    throws SQLException
 99  {
100    return this.con;
101  }
102
103}