forked from johnrogers104/PointOfSaleSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataBaseConnection.java
176 lines (161 loc) · 4.87 KB
/
DataBaseConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//John Rogers (the j-tees)
// cse216
// DBConnection
package ProcessSale;
import java.sql.*;
import java.util.ArrayList;
public class DataBaseConnection {
private static Connection con;
private static DataBaseConnection db = null;
/*
The DataBaseConnection constructor, which opens a connection to our database.
Note that the connection MUST be closed by the close() method listed below after
the DataBaseConnection object that this constructor creates is done being used.
*/
private DataBaseConnection(){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@edgar1.cse.lehigh.edu:1521:cse241", "jeg416cse216", "jtees216");
}
catch(ClassNotFoundException ex){
System.out.println("error with driver");
}
catch(SQLException ex){
System.out.println("log in error");
}
}
/*
static method to return the single instance of DataBaseConnection to be used
by our system.
*/
public static DataBaseConnection getInstance(){
if(db == null){
db = new DataBaseConnection();
return db;
}
else{
return db;
}
}
/*
The close() method is a simple method to close the connection
to the database that was opened when we create a new
DataBaseConnection object. Returns true if success, false
on failure.
*/
public void commit(){
try{
con.commit();
}
catch(SQLException ex){
System.out.println("could not commit!");
System.exit(1);
}
}
public boolean close(){
try{
con.close();
return true;
}
catch(SQLException ex){
System.out.println("error closing connection");
return false;
}
}
/*
newUpdateQuery (below) is the method to call when an we want
to perform a query that in any way updates a relation. This
can be either an insert, delete, or update query (not a generic
select ... where ... from ... query). Does not return anything.
*/
public boolean newUpdateQuery(String q){
try{
Statement s = con.createStatement();
s.executeUpdate(q);
s.close();
commit();
}
catch(SQLException ex){
ex.printStackTrace(System.out);
return false;
}
return true;
}
/*
The newQuery() method is for those generic select ... from ...
where ... queries that needs to return a ResultSet. Since returning
ResultSets causes problems with keeping statements open, and the event in
which a query returns an empty result (since ResultSets can not be null),
this method instead places the result relation in an ArrayList of ArrayLists,
so that statements can be properly closed and there is no data leakage of
relations between classes. Note that in order to do this, you must provide
with the query the number of columns that the query returns (the number of
attributes in the select clause) so that the inner ArrayList (which simulates
each row) knows how long it needs to be.
*/
public ArrayList<ArrayList<String>> newQuery(String q, int count){
ArrayList<ArrayList<String>> table = new ArrayList<ArrayList<String>>();
try{
Statement s = con.createStatement();
ResultSet r = s.executeQuery(q);
ResultSetMetaData rsmd = r.getMetaData();
while(r.next()){
ArrayList<String> row = new ArrayList<String>();
for(int i=1; i<= count; i++){
row.add(r.getString(rsmd.getColumnName(i)));
}
table.add(row);
}
s.close();
return table;
}
catch(SQLException ex){
ex.printStackTrace();
}
table = null;
return table;
}
public boolean isInDataBase(String str, String tableName){
try{
Statement s = con.createStatement();
ResultSet r = s.executeQuery("select * from "+tableName);
ResultSetMetaData rsmd = r.getMetaData();
String primaryKey = rsmd.getColumnName(1);
String query = "select * from "+tableName+" where "+primaryKey+" = '"+str+"'";
int c = rsmd.getColumnCount();
if(!newQuery(query, c).isEmpty()){
return true;
}
else{
return false;
}
}
catch(SQLException ex){
System.out.println("error in isInDataBase");
return false;
}
}
public boolean verifyMatch(String attribute1, String attribute2, String tableName, String columnName1, String columnName2){
try{
Statement s = con.createStatement();
ResultSet r = s.executeQuery("select * from "+tableName);
ResultSetMetaData rsmd = r.getMetaData();
int c = rsmd.getColumnCount();
String query = "select * from "+tableName+" where "+columnName1+" = '"+attribute1+"' and "+columnName2+" = '"+attribute2+"'";
if(!newQuery(query, c).isEmpty()){
return true;
}
else{
return false;
}
}
catch(SQLException ex){
System.out.println("error in verifyMatch");
return false;
}
}
<<<<<<< Updated upstream
}
=======
}
>>>>>>> Stashed changes