root/trunk/ConDB.java

Revision 109, 2.7 kB (checked in by rajlich, 3 years ago)

*** empty log message ***

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /* ConDB.java
2  * The Monitors connection class
3  *
4  */
5
6 import java.util.Vector;
7 import java.io.*;
8 import java.sql.*;
9 import java.util.*;
10 import java.text.*;
11
12
13 /**
14  * ConDB
15  * The following class is for DB connection
16  */
17 public class ConDB {
18     /** The connection object (connects to the database) */
19     Connection connection;
20
21
22     /**
23      * ConDB
24      * Logs on to the monitors database using username: monitors
25      * and password 2172442757 This is the default constructor for the class
26      */
27     public ConDB(){
28         System.out.println("Trying to connect to the Monitors database");
29         login("monitors", "2172442757");
30         System.out.println("Connected to the Monitors database");
31     }
32
33     /**
34      * ConDB
35      * Constructor Logs in into the database adn creates a new
36      * @param username the username for the database
37      *        password the password for the database
38      */
39     public ConDB (String username, String password) {
40         System.out.println("Trying to connect to the db");
41         login(username, password);
42         System.out.println("Connected to the database");
43     }
44
45
46     /**
47      * Login
48      * logs into the Monitors database using the specified verification details
49      * including username and password
50      * @param username the username of the database
51      *        password the password of the database for log on verification
52      */
53     void login(String username, String password) {
54         final String DRIVER = "org.gjt.mm.mysql.Driver";
55         final String URL ="jdbc:mysql://www3.isrl.uiuc.edu/monitors?user="+username+"&password="+password;
56
57         try{
58             Class c= Class.forName(DRIVER);
59             //COMMENT - COMPILE HACK
60             //DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
61             connection = DriverManager.getConnection(URL);
62         }
63         catch(ClassNotFoundException e) {
64             System.err.println("Cannot load Driver");
65             System.exit(1);
66         }
67         catch(SQLException e) {
68             e.printStackTrace();
69             System.err.println("Cant Connect");
70             System.exit(1);
71         }
72         catch (Exception e) {
73             System.err.println(e.getMessage());
74             System.exit(0);
75         }
76     }//end of login
77
78
79     /**
80      * getConnection
81      * returns the current connection object
82      * @return connection a Connection object for the current connection
83      *                    to the database
84      *
85      */
86     public Connection getConnection() {
87         return connection;
88     }//end getConnection
89
90
91     /**
92      * close
93      * This method closes the connection to the database
94      * This is very essential or else the database would be
95      * eveloaded with useless open connections.
96      */
97     public void close() {
98         try {
99             connection.close();
100         }
101         catch (Exception e) {
102             e.printStackTrace();
103         }
104         System.out.println("Closed DB connection");
105     }
106
107
108 } //class ConDB
109
Note: See TracBrowser for help on using the browser.