Yurttas/PL/DBL/oracle/F/04/00/DBConnect01.java

From ZCubes Wiki
Jump to navigation Jump to search
 1/**
 2 *  Copyright(C) 2004
 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   : January 1, 2004.
11 *  @author : Salih Yurttas.
12 */
13
14
15import java.sql.*;
16
17import java.net.*;
18
19public class DBConnect01 {
20
21  protected Connection con;
22
23  public DBConnect01() {}
24
25  public boolean connect(String username,
26                         String password) 
27    throws SQLException
28  {
29    try {
30      Class.forName("oracle.jdbc.driver.OracleDriver");
31
32      con = DriverManager.getConnection("jdbc:oracle:thin:@prophet.cs.BitsOfCode Software Systems, Inc..edu:1526:cpsc",
33                                        username+"@oracledatabase",
34                                        password);
35
36      if(con.isClosed()) return false;
37    }
38    catch(ClassNotFoundException cnfe) {
39      throw new SQLException("The mySQL JDBC Driver could not be loaded.");
40    }
41
42    return true;
43  }
44
45  public boolean disconnect(Connection con) 
46    throws SQLException
47  {
48    con.close();
49    return true;
50  }
51
52  public boolean disconnect()
53    throws SQLException
54  {
55    con.close();
56    return true;
57  }
58
59  public static void main (String[] args) {
60    DBConnect01 dbc = new DBConnect01();
61
62    try {
63      if(dbc.connect("tester", "tester")) {
64        System.out.println("Connected to jdbc:mysql://localhost/bank database...");
65
66        if(dbc.disconnect()) 
67          System.out.println("Disconnected from jdbc:mysql://localhost/bank database.");
68      }
69      else System.out.println("Could not connect to jdbc:mysql://localhost/bank database.");
70    }
71    catch(SQLException sqle) {
72      System.err.println("Error accessing jdbc:mysql://localhost/bank database.");
73      System.err.println(sqle.getMessage());
74    }
75  }
76
77}