-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProg4.java
230 lines (186 loc) · 6.89 KB
/
Prog4.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* Prog3.java -- A program that connects to an Oracle DB to access
* tables that are queried given a users input.
* To compile and execute this program on lectura:
*
* Add the Postgres JDBC driver to your CLASSPATH environment variable:
*
* export CLASSPATH=/opt/oracle/product/10.2.0/client/jdbc/lib/ojdbc14.jar:${CLASSPATH}
*
* (or whatever shell variable set-up you need to perform to add the
* JAR file to your Java CLASSPATH)
*
* Compile this file:
*
* javac JDBC.java
*
* Finally, run the program:
*
* java JDBC <oracle username> <oracle password>
*
* Author: Tanner Koch
* Class: CS460
* Assignment: Project 3
* Instructor: Mccann, Yang
* Prompt the user to choose from a menu of queries, then with a user's choice, execute the query
* in Oracle.
*
* I simply used a text editor to cleanse my files.
*/
import java.io.*;
import java.sql.*; // For access to the SQL interaction methods
import java.util.Scanner;
public class Prog4 {
public static void main(String[] args) {
final String oracleURL = // Magic lectura -> aloe access spell
"jdbc:oracle:thin:@aloe.cs.arizona.edu:1521:oracle";
String username = null, // Oracle DBMS username
password = null; // Oracle DBMS password
if (args.length == 2) { // get username/password from cmd line args
username = args[0];
password = args[1];
} else {
System.out.println("\nUsage: java JDBC <username> <password>\n"
+ " where <username> is your Oracle DBMS" + " username,\n and <password> is your Oracle"
+ " password (not your system password).\n");
System.exit(-1);
}
// load the (Oracle) JDBC driver by initializing its base
// class, 'oracle.jdbc.OracleDriver'.
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException e) {
System.err.println("*** ClassNotFoundException: " + "Error loading Oracle JDBC driver. \n"
+ "\tPerhaps the driver is not on the Classpath?");
System.exit(-1);
}
// make and return a database connection to the user's
// Oracle database
Connection dbconn = null;
try {
dbconn = DriverManager.getConnection(oracleURL, username, password);
} catch (SQLException e) {
System.err.println("*** SQLException: " + "Could not open JDBC connection.");
System.err.println("\tMessage: " + e.getMessage());
System.err.println("\tSQLState: " + e.getSQLState());
System.err.println("\tErrorCode: " + e.getErrorCode());
System.exit(-1);
}
// Send the query to the DBMS, and get and display the results
int response = 0;
int exceeds = 0; // will be used for query4
Scanner s = new Scanner(System.in);
while (response != -1) { // if the user enters -1, quit the program
System.out.println("Choose from the menu.(-1 to quit)");
System.out.println("1.Test querying the mattseall.office table");
System.out.println("2. Test an insertion");
System.out.println("3. Test a deletion");
response = s.nextInt();
if (response == -1) {
break;
}
if (response == 1) { // choice 1
queryOne(dbconn);
}
if (response == 2) { // choice 1
insertOffice(dbconn,"university", "85719", "tucson", "university", 2);
}
if (response == 3) { // choice 1
deleteOffice(dbconn, 3);
}
}
s.close();
}
/*Method: queryOne(Connection)
* The purpose of this method is to execute the first query
* from the menu in the main method. This will find all the
* schools who are a junior high school.
*/
private static void queryOne(Connection dbconn) {
Statement stmt = null;
ResultSet answer = null;
String query = "select * from mattseall.office";
try {
stmt = dbconn.createStatement();
answer = stmt.executeQuery(query);
if (answer != null) {
System.out.println("\nThe results of the query are:\n");
System.out.println();
// Use next() to advance cursor through the result
// tuples and print their attribute values
String office;
int officeNum;
int count = 0;
while (answer.next()) {
officeNum = answer.getInt("office_id");
System.out.print("" + officeNum + "\t");
office = answer.getString("street");
office = office.toLowerCase();
System.out.print("" + office + "\t");
office = answer.getString("postal");
office = office.toLowerCase();
System.out.print("" + office + "\t");
office = answer.getString("city");
office = office.toLowerCase();
System.out.print("" + office + "\t");
office = answer.getString("branch");
office = office.toLowerCase();
System.out.print("" + office + "\t");
officeNum = answer.getInt("manager_id");
System.out.print("" + officeNum + "\t");
System.out.println();
count++;
}
System.out.println("The count is: " + count);
}
System.out.println();
// Shut down the connection to the DBMS.
stmt.close();
dbconn.close();
} catch (SQLException e) {
System.err.println("*** SQLException: "
+ "Could not fetch query results.");
System.err.println("\tMessage: " + e.getMessage());
System.err.println("\tSQLState: " + e.getSQLState());
System.err.println("\tErrorCode: " + e.getErrorCode());
System.exit(-1);
}
}
private static void insertOffice(Connection dbconn,String branch_id, String street, String postal, String city,
int manager_id) {
Statement stmt = null;
String query = "insert into mattseall.branch (branch_id, street, postal, city, manager_id)"
+ " VALUES ('" + branch_id + "', '" + street + "', '" + postal + "', '" + city + "', "+ manager_id + ")";
try {
stmt = dbconn.createStatement();
stmt.executeQuery(query);
// Shut down the connection to the DBMS.
stmt.close();
dbconn.close();
} catch (SQLException e) {
System.err.println("*** SQLException: " + "Could not fetch query results.");
System.err.println("\tMessage: " + e.getMessage());
System.err.println("\tSQLState: " + e.getSQLState());
System.err.println("\tErrorCode: " + e.getErrorCode());
System.exit(-1);
}
}
private static void deleteOffice(Connection dbconn, int office_id) {
Statement stmt = null;
String query = "delete from mattseall.office"
+ " where branch_id=" + office_id;
try {
stmt = dbconn.createStatement();
stmt.executeQuery(query);
// Shut down the connection to the DBMS.
stmt.close();
dbconn.close();
} catch (SQLException e) {
System.err.println("*** SQLException: " + "Could not fetch query results.");
System.err.println("\tMessage: " + e.getMessage());
System.err.println("\tSQLState: " + e.getSQLState());
System.err.println("\tErrorCode: " + e.getErrorCode());
System.exit(-1);
}
}
}