Advertisement
Guest User

Untitled

a guest
Oct 14th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.91 KB | None | 0 0
  1. package com.sqldbsamples;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.Statement;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.DriverManager;
  8. import java.util.Scanner;
  9.  
  10. public class App {
  11.  
  12. public static void main(String[] args) {
  13.  
  14. // Connect to database
  15. String hostName = "vo6901-sql-server.database.windows.net";
  16. String dbName = "cs-dsa-4513-sql-db";
  17. String user = "vo6901";
  18. String password = "";
  19. String url = String.format("jdbc:sqlserver://%s:1433;database=%s;user=%s;password=%s;encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;", hostName, dbName, user, password);
  20. Connection connection = null;
  21.  
  22. try {
  23. connection = DriverManager.getConnection(url);
  24. String schema = connection.getSchema();
  25. System.out.println("Successful connection - Schema: " + schema);
  26.  
  27. System.out.println("Query data example:");
  28. System.out.println("=========================================");
  29. Scanner scan = new Scanner(System.in);
  30. Scanner scanString = new Scanner(System.in);
  31. String userInput;
  32.  
  33. int pidInsert;
  34. String pnameInsert;
  35. int aidInsert;
  36. int maxScoreInsert;
  37.  
  38. // Create and execute a SELECT SQL statement.
  39. String selectSql = "SELECT * FROM Problem";
  40. String authorSql = "SELECT * FROM Author";
  41.  
  42. String pidSql = "SELECT pid FROM Problem";
  43. String pnameSql = "SELECT pname FROM Problem";
  44. String aidSql = "SELECT aid FROM Author";
  45.  
  46. String insertSql;
  47.  
  48. System.out.println("OPTIONS:");
  49. System.out.println("(insert) Insert the pid, pname, Max-score, and aid into the table Problem (aid would be the existing ids from Author table). Assume that the value of Max_score is not known and will be estimated using the information present in the tableas follows. The Max_score should be set to the rounded average of maximumof Max_score + 10% ofproblems by that author. If there are no problems by that author yet in that table, then Max_score should be set to the rounded average of all the problems present in the table.");
  50. System.out.println("(aid) The compensation of authors should be increased as follows. Taking aid as input from user, if that author has the highest number of problems, he should get 20% raise in his compensation; If it is the 2nd highest, he should get 15% raise in his compensation; if it is the 3rd highest, he should get 10% raisein his compensation; otherwise, the default 5% raise should be applied.");
  51. System.out.println("(display) Display the complete information of the Problem table and author table.");
  52. System.out.println("(quit) Quit");
  53.  
  54. while(true)
  55. {
  56. System.out.println("Please select one of the four options by typing in their keyword:");
  57. userInput = scanString.nextLine();
  58.  
  59. /*INSERT*****************************************************************/
  60.  
  61. if (userInput.equals("insert"))
  62. {
  63. System.out.println("Currently used pids:");
  64. try (Statement statement = connection.createStatement();
  65. ResultSet resultSet = statement.executeQuery(pidSql))
  66. {
  67. while (resultSet.next())
  68. {
  69. System.out.println(resultSet.getString(1));
  70. }
  71. }
  72.  
  73. System.out.println("Enter pid to insert: ");
  74. pidInsert = scan.nextInt();
  75.  
  76. System.out.println("Currently used pnames:");
  77. try (Statement statement = connection.createStatement();
  78. ResultSet resultSet = statement.executeQuery(pnameSql))
  79. {
  80. while (resultSet.next())
  81. {
  82. System.out.println(resultSet.getString(1));
  83. }
  84. }
  85.  
  86. System.out.println("Enter pname to insert: ");
  87. pnameInsert = scanString.nextLine();
  88.  
  89. System.out.println("Currently used aids:");
  90. try (Statement statement = connection.createStatement();
  91. ResultSet resultSet = statement.executeQuery(aidSql))
  92. {
  93. while (resultSet.next())
  94. {
  95. System.out.println(resultSet.getString(1));
  96. }
  97. }
  98. System.out.println("Enter aid to insert: ");
  99. aidInsert = scan.nextInt();
  100.  
  101. System.out.println(pidInsert + " " + pnameInsert + " " + aidInsert);
  102.  
  103. maxScoreInsert = 100;
  104.  
  105. //insertSql = "GO CREATE PROCEDURE sp_insert @pid INT @pname VARCHAR(64) @MAX_SCORE INT @aid INT AS BEGIN set nocount on INSERT INTO Problem (pid, pname, max_score, aid) VALUES (" + pidInsert + ", " + pnameInsert + ", " + maxScoreInsert + ", " + aidInsert + "); set nocount off END GO EXEC sp_insert @pid = " + pidInsert + ", @pname = " + pnameInsert + ", @MAX_SCORE = " + maxScoreInsert + ", @aid = " + aidInsert + ";";
  106. insertSql = "INSERT INTO Problem (pid, pname, max_score, aid) VALUES (" + pidInsert + ", '" + pnameInsert + "', " + maxScoreInsert + ", " + aidInsert + ");";
  107. try (Statement statement = connection.createStatement();
  108. ResultSet resultSet = statement.executeQuery(insertSql))
  109. {
  110. System.out.println("Successful insertion.");
  111. }
  112.  
  113. }
  114. /*AID*****************************************************************/
  115. else if (userInput.equals("aid"))
  116. {
  117. System.out.println("BOOP");
  118. }
  119. /*DISPLAY*****************************************************************/
  120. else if (userInput.equals("display"))
  121. {
  122. try (Statement statement = connection.createStatement();
  123. ResultSet resultSet = statement.executeQuery(selectSql))
  124. {
  125. System.out.println("PID PNAME MAX_SCORE AID");
  126. while (resultSet.next())
  127. {
  128. System.out.println(resultSet.getString(1) + " "
  129. + resultSet.getString(2) + " "
  130. + resultSet.getString(3)+ " "
  131. + resultSet.getString(4));
  132. }
  133. }
  134.  
  135. try (Statement statement = connection.createStatement();
  136. ResultSet authorSet = statement.executeQuery(authorSql))
  137. {
  138. System.out.println("AID ANAME COMPENSATION");
  139.  
  140. while (authorSet.next())
  141. {
  142. System.out.println(authorSet.getString(1) + " "
  143. + authorSet.getString(2) + " "
  144. + authorSet.getString(3));
  145. }
  146. }
  147. }
  148. /*QUIT*****************************************************************/
  149. else if (userInput.equals("quit"))
  150. {
  151. connection.close();
  152. break;
  153. }
  154. else
  155. {
  156. System.out.println("Invalid input.");
  157. }
  158. }
  159. }
  160. catch (Exception e) {
  161. e.printStackTrace();
  162. }
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement