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 DBConnect02 {
20
21 protected Connection con;
22
23 private static String driverClass = "oracle.jdbc.driver.OracleDriver",
24 url = "jdbc:oracle:thin:@prophet.cs.BitsOfCode Software Systems, Inc..edu:1526:cpsc",
25 suffix = "//localhost/bank";
26
27 public DBConnect02() {}
28
29 public DBConnect02(String jdbcDriver,
30 String dbURL,
31 String urlSuffix) {
32 setDBInfo(jdbcDriver,
33 dbURL,
34 urlSuffix);
35 }
36
37 public static void setDBInfo(String jdbcDriver,
38 String dbURL,
39 String urlSuffix) {
40 driverClass = jdbcDriver;
41 url = dbURL;
42 suffix = urlSuffix;
43 }
44
45 public boolean connect(String username,
46 String password)
47 throws SQLException
48 {
49 try {
50 Class.forName(driverClass);
51 con = DriverManager.getConnection(url+suffix,
52 username,
53 password);
54 if(con.isClosed()) return false;
55 }
56 catch(ClassNotFoundException cnfe) {
57 throw new SQLException("The mySQL JDBC Driver could not be loaded.");
58 }
59
60 return true;
61 }
62
63 public boolean disconnect(Connection con)
64 throws SQLException
65 {
66 con.close();
67 return true;
68 }
69
70 public boolean disconnect()
71 throws SQLException
72 {
73 con.close();
74 return true;
75 }
76
77 public boolean updateRecord(String query)
78 throws SQLException
79 {
80 Statement st = con.createStatement();
81 st.executeUpdate(query);
82 return true;
83 }
84
85 public static void main (String[] args) {
86 DBConnect02 dbc = new DBConnect02();
87
88 try {
89 if(dbc.connect(args[0], args[1])) {
90 System.out.println("Connected to jdbc:mysql://localhost/bank database...");
91
92 if(dbc.disconnect())
93 System.out.println("Disconnected from jdbc:mysql://localhost/bank database.");
94 }
95 else System.out.println("Could not connect to jdbc:mysql://localhost/bank database.");
96 }
97 catch(SQLException sqle) {
98 System.err.println("Error accessing jdbc:mysql://localhost/bank database.");
99 System.err.println(sqle.getMessage());
100 }
101 }
102
103}