Advertisement
Guest User

Untitled

a guest
Oct 5th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.74 KB | None | 0 0
  1. /**
  2. * Created by Bailey Sostek on 9/27/18.
  3. */
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.sql.*;
  9.  
  10. public class p3 implements Runnable {
  11.  
  12. private static String USERID = "";
  13. private static String PASSWORD = "";
  14.  
  15. private static Thread thisThread;
  16.  
  17. private static String[] launchArgs;
  18.  
  19. private static boolean running = false;
  20.  
  21. @Override
  22. public void run() {
  23. //If this should keep running
  24. this.running = true;
  25.  
  26. //Derrive input username and password
  27. if (launchArgs.length >= 2) {
  28. USERID = launchArgs[0];
  29. PASSWORD = launchArgs[1];
  30. }
  31.  
  32. //Input Stream
  33.  
  34. //Run loop
  35. loop:while(running) {
  36. //Not enough input args
  37. if (launchArgs.length == 0) {
  38. System.out.println("Please execute this command with your username and password passed in as additional arguments.");
  39. System.out.println("java -jar p3 <username> <password>");
  40. stop();
  41. break;
  42. }
  43. //No additional input args
  44. if (launchArgs.length == 2) {
  45. System.out.println("1 – Report Wine Information");
  46. System.out.println("2 – Report Company Rep Information");
  47. System.out.println("3 – Report Wine Label Form Information");
  48. System.out.println("4 – Update Phone Number");
  49. System.out.println("5 – Exit Program");
  50. stop();
  51. break;
  52. }
  53. //No additional input args
  54. if (launchArgs.length == 3) {
  55. int option = 0;
  56. try{
  57. option = Integer.parseInt(launchArgs[2]);
  58. }catch(Exception e){
  59. System.out.println("The argument input was invalid.");
  60. stop();
  61. break;
  62. }
  63.  
  64. Connection connection = logIn();
  65. if(connection == null){
  66. System.out.println("No connection could be estableshed with the provided login information.");
  67. }
  68.  
  69. String input = "";
  70. switch (option){
  71. case 1:
  72. // enter "Report Wine Information" Mode
  73. // print out "Enter Wine ID:"
  74. System.out.println("Enter Wine ID:");
  75. //await user input
  76. input = getInput();
  77. // upon user input, query the wines table and print out info (in specific format)
  78. query1(connection, input);
  79. // then terminate
  80. stop();
  81. break loop;
  82. case 2:
  83. // enter "Report Company Rep Information" Mode
  84. // print out "Enter Company Rep Login Name:"
  85. System.out.println("Enter Company Rep Login Name:");
  86. //await user input
  87. input = getInput();
  88.  
  89. query2(connection, input);
  90. // then terminate
  91. stop();
  92. break loop;
  93. case 3:
  94. // enter "Report Wine Label Form Information" Mode
  95. System.out.println("Enter Wine Label Form ID:");
  96. //await user input
  97. input = getInput();
  98. //Execute Query
  99. query3(connection, input);
  100. // then terminate
  101. stop();
  102. break loop;
  103. case 4:
  104. // enter "Update Phone Number" Mode
  105. System.out.println("Enter Company Rep login Name:");
  106. //await user input
  107. input = getInput();
  108. System.out.println("Enter the Updated Phone Number:");
  109. //await users phone number input
  110. String phoneNumber = getInput();
  111. query4(connection, input, phoneNumber);
  112. // then terminate
  113. stop();
  114. break loop;
  115. case 5:
  116. // exit program
  117. stop();
  118. break loop;
  119. default:
  120. // output "The argument inputted was invalid"
  121. System.out.println("The argument input was invalid");
  122. // then terminate
  123. stop();
  124. break loop;
  125. }
  126. }
  127. }
  128. }
  129.  
  130. public String getInput(){
  131. //Wait for command
  132. try {
  133. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  134. return reader.readLine();
  135. } catch (IOException e) {
  136. e.printStackTrace();
  137. return "";
  138. }
  139. }
  140.  
  141. public static void stop(){
  142. running = false;
  143. }
  144.  
  145. public Connection logIn(){
  146. try {
  147. Class.forName("oracle.jdbc.driver.OracleDriver");
  148.  
  149. } catch (ClassNotFoundException e){
  150. System.out.println("Where is your Oracle JDBC Driver?");
  151. e.printStackTrace();
  152. return null;
  153. }
  154.  
  155. Connection connection = null;
  156.  
  157. try {
  158. connection = DriverManager.getConnection(
  159. "jdbc:oracle:thin:@oracle.wpi.edu:1521:orcl", USERID, PASSWORD);
  160. } catch (SQLException e) {
  161. System.out.println("Connection Failed! Check output console");
  162. e.printStackTrace();
  163. return null;
  164. }
  165.  
  166. return connection;
  167. }
  168.  
  169. public void query1(Connection connection, String input){
  170. // Performing the query
  171. try {
  172. Statement stmt = connection.createStatement();
  173. String str = "SELECT * FROM WINES WHERE WINEID = "+input;
  174. ResultSet rset = stmt.executeQuery(str);
  175.  
  176. int wineID = 0;
  177. String brandName = "";
  178. String classType = "";
  179. float percent = 0.0f;
  180. String appellation = "";
  181. float netContent = 0.0f;
  182. String bottler = "";
  183.  
  184. boolean shouldContinue = rset.next();
  185. if(!shouldContinue){
  186. System.out.println("No Wine Records exist with the WineID:"+input);
  187. }
  188.  
  189. // Process the results
  190. while (shouldContinue) {
  191. wineID = rset.getInt("wineID");
  192. brandName = rset.getString("brand");
  193. classType = rset.getString("classType");
  194. percent = rset.getFloat("alcohol");
  195. appellation = rset.getString("appellation");
  196. netContent = rset.getFloat("netContent");
  197. bottler = rset.getString("bottlerName");
  198. System.out.println("Wines Information");
  199. System.out.println("Wine ID: " + wineID + "\nBrand Name: " + brandName + "\nClass/Type: " + classType + "\nAlchohol: " + percent + "%\nAppellation: " + appellation + "\nNet Content: " + netContent + "\nBottler: " + bottler);
  200. shouldContinue = rset.next();
  201. } // end while
  202.  
  203. rset.close();
  204. stmt.close();
  205. connection.close();
  206. } catch (SQLException e) {
  207. System.out.println("Get Data Failed! Check output console");
  208. e.printStackTrace();
  209. }
  210. }
  211.  
  212. public void query2(Connection connection, String input){
  213. // Performing the query
  214. try {
  215. Statement stmt = connection.createStatement();
  216. String str = "SELECT * FROM ACCOUNTS JOIN REPS ON reps.loginname = accounts.loginname WHERE reps.loginname = '"+input+"'";
  217. ResultSet rset = stmt.executeQuery(str);
  218.  
  219. String logInName = "";
  220. int repID = 0;
  221. String name = "";
  222. String phone = "";
  223. String email = "";
  224. String companyName = "";
  225.  
  226. boolean shouldContinue = rset.next();
  227. if(!shouldContinue){
  228. System.out.println("No Representative with the username:"+input+" exists.");
  229. }
  230.  
  231. // Process the results
  232. while (shouldContinue) {
  233. logInName = rset.getString("logInName");
  234. repID = rset.getInt("repID");
  235. name = rset.getString("name");
  236. phone = rset.getString("phone");
  237. email = rset.getString("email");
  238. companyName = rset.getString("companyName");
  239. System.out.println("Company Representative Information");
  240. System.out.println(
  241. "Login Name: " + logInName + "\n" +
  242. "RepID: " + repID + "\n" +
  243. "Full Name: " + name + "\n" +
  244. "Phone: " + phone + "\n" +
  245. "Email Address: " + email + "\n" +
  246. "Company Name: " + companyName);
  247. shouldContinue = rset.next();
  248. } // end while
  249.  
  250. rset.close();
  251. stmt.close();
  252. connection.close();
  253. } catch (SQLException e) {
  254. System.out.println("Get Data Failed! Check output console");
  255. e.printStackTrace();
  256. }
  257. }
  258.  
  259. public void query3(Connection connection, String input){
  260. // Performing the query
  261. try {
  262. Statement stmt = connection.createStatement();
  263. String str = "SELECT FORMID, STATUS, BRAND, VINTAGE, repName, agentName\n" +
  264. "FROM (SELECT FORMID, STATUS, BRAND, VINTAGE, REPID, CURRENTREVIEWERID FROM FORMS JOIN WINES ON wines.wineid = forms.wineid)winestuff\n" +
  265. "JOIN (SELECT repID, NAME AS repName FROM REPS JOIN ACCOUNTS ON REPS.loginName = ACCOUNTS.loginName)repstuff\n" +
  266. "ON winestuff.repid = repstuff.repid\n" +
  267. "JOIN (SELECT ttbID, NAME AS agentName FROM AGENTS JOIN ACCOUNTS ON AGENTS.loginName = ACCOUNTS.loginName)agentstuff\n" +
  268. "ON winestuff.currentreviewerID = agentstuff.ttbID WHERE FORMID = "+input+"";
  269. ResultSet rset = stmt.executeQuery(str);
  270.  
  271. int formID = 0;
  272. String status = "";
  273. String brand = "";
  274. int vintage = 0;
  275. String repName = "";
  276. String agentName = "";
  277.  
  278. boolean shouldContinue = rset.next();
  279. if(!shouldContinue){
  280. System.out.println("No Forms with the FormID:"+input+" exists.");
  281. }
  282.  
  283. // Process the results
  284. while (shouldContinue) {
  285. formID = rset.getInt("formID");
  286. status = rset.getString("status");
  287. brand = rset.getString("brand");
  288. vintage = rset.getInt("vintage");
  289. repName = rset.getString("repName");
  290. agentName = rset.getString("agentName");
  291.  
  292. System.out.println("Company Representative Information");
  293. System.out.println(
  294. "Form ID: " + formID + "\n" +
  295. "Status: " + status + "\n" +
  296. "Wine Brand: " + brand + "\n" +
  297. "Vintage: " + vintage + "\n" +
  298. "Company Rep Full Name: " + repName + "\n" +
  299. "Agent Full Names: " + agentName);
  300. shouldContinue = rset.next();
  301. } // end while
  302.  
  303. rset.close();
  304. stmt.close();
  305. connection.close();
  306. } catch (SQLException e) {
  307. System.out.println("Get Data Failed! Check output console");
  308. e.printStackTrace();
  309. }
  310. }
  311.  
  312. public void query4(Connection connection, String input, String phone){
  313. // Performing the query
  314. try {
  315. Statement stmt = connection.createStatement();
  316. String str = "UPDATE ACCOUNTS SET PHONE = '"+phone+"' WHERE LOGINNAME = '"+input+"'";
  317. stmt.executeUpdate(str);
  318. stmt.close();
  319. connection.close();
  320. } catch (SQLException e) {
  321. System.out.println("Get Data Failed! Check output console");
  322. e.printStackTrace();
  323. }
  324. }
  325.  
  326. public static void main(String[] args) {
  327. thisThread = new Thread(new p3());
  328. launchArgs = args;
  329. thisThread.start();
  330. }
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement