/Users/lyon/j4p/src/net/server/servlets/DatabaseReader.java
|
1 package net.server.servlets;
2
3 import java.sql.*;
4
5 /**
6 * DatabaseReader Class
7 */
8
9 public class DatabaseReader {
10
11 // This is from mid-term
12 // deltaNew.setDriver("org.gjt.mm.mysql.Driver");
13 // System.out.println( deltaNew.getDriver() );
14 // deltaNew.setUrl("jdbc:mysql://JHVY501/test");
15
16 private String driver = net.server.db.SqlBean.getDriver();
17
18 private String url = net.server.db.SqlBean.getUrl();
19
20
21 private Connection cn;
22 private Statement stmt;
23 private ResultSet rs;
24
25 private String user = net.server.db.SqlBean.getUserId();
26 private String password = net.server.db.SqlBean.getPassword();
27
28
29 /**
30 * Sets the Driver
31 *
32 * @parm String driver
33 * @return void
34 */
35
36 public void setDriver(String _driver) {
37 driver = _driver;
38 }
39
40 /**
41 * Sets the URL
42 *
43 * @parm String url
44 * @return void
45 */
46
47 public void setUrl(String _url) {
48 url = _url;
49 }
50
51 /**
52 * Sets the Connection
53 *
54 * @parm String connection
55 * @return void
56 */
57
58 public void setConnection(String _cn) {
59
60 }
61
62 /**
63 * Sets the User
64 *
65 * @parm String user
66 * @parm String password
67 * @return void
68 */
69
70 public void setUser(String _user, String _password) {
71 user = _user;
72 password = _password;
73 }
74
75 /**
76 * Establishes the Database Connection
77 *
78 * @return void
79 * @throws java.lang.ClassNotFoundException, Exception
80 */
81
82 public void openDatabase() throws ClassNotFoundException,
83 Exception {
84
85 System.out.println("Connecting to Database in openDatabase Method");
86 System.out.println("URL is " + url + " Driver is " + driver);
87 //User is " + user + "\npassword is " + password );
88
89 try {
90 Class.forName(driver);
91 } catch (Exception E) {
92 System.out.println("Failed on the class name");
93 }
94
95 try {
96 cn = DriverManager.getConnection(url, user, password);
97 } catch (Exception E) {
98 System.out.println("Failed on the connection");
99 }
100
101 stmt = cn.createStatement();
102
103 }
104
105 /**
106 * Closes the Database Connection
107 *
108 * @return void
109 */
110
111 public void closeDatabase() {
112 try {
113 stmt.close();
114 cn.close();
115 } catch (SQLException sqle) {
116 sqle.printStackTrace();
117 }
118 }
119
120 /**
121 * Return true/false
122 *
123 * @parm String sql
124 * @return boolean
125 */
126
127 public boolean checkRecordExists(String sql) {
128
129 try {
130 openDatabase();
131 rs = stmt.executeQuery(sql);
132 closeDatabase();
133 return rs.next();
134 } catch (ClassNotFoundException cnf) {
135 System.out.println("\n ClassNotFound");
136 cnf.printStackTrace();
137 return false;
138 } catch (SQLException sqle) {
139 System.out.println("\n checkRecordExists Failure");
140 sqle.printStackTrace();
141 return false;
142 } catch (Exception e) {
143 System.out.println("\n Exception");
144 e.printStackTrace();
145 return false;
146 }
147 }
148
149 }
150
151
152
153