Advertisement
Guest User

Untitled

a guest
Sep 13th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6.  
  7.  
  8.  
  9. public class Homework2 {
  10. static final String jdbcURL = "jdbc:mariadb://classdb2.csc.ncsu.edu:3306/dwdeans";
  11. // Put your oracle ID and password here
  12.  
  13. private static Connection connection = null;
  14. private static Statement statement = null;
  15. private static ResultSet result = null;
  16.  
  17. public static void main(String[] args) {
  18.  
  19. initialize();
  20.  
  21. try {
  22. boolean canAfford = checkAbilityToStudy("Todd");
  23. // ************************************************************************
  24.  
  25. // modifyALittleBit1();
  26. modifyALittleBit2();
  27.  
  28. boolean canAfford1 = checkAbilityToStudy("Angela");
  29.  
  30. if (canAfford == canAfford1) {
  31. System.out.println("Success");
  32. } else {
  33. System.out.println("Failure");
  34. }
  35.  
  36. } catch (SQLException e) {
  37. e.printStackTrace();
  38. }
  39. // ***********************************************************************
  40. close();
  41. }
  42.  
  43. private static void initialize() {
  44. try {
  45. connectToDatabase();
  46.  
  47. statement.executeUpdate("CREATE TABLE Students (Name VARCHAR(20), "
  48. + "School VARCHAR(10), Age INTEGER, FundingReceived INTEGER, Income INTEGER, Sex CHAR(1))");
  49.  
  50. statement.executeUpdate("INSERT INTO Students VALUES ('Todd', 'NC State'," + " 18, 16000, 30000, 'M')");
  51. statement.executeUpdate("INSERT INTO Students VALUES ('Max', 'Stanford'," + " 21, 20000, 70000, 'M')");
  52. statement.executeUpdate("INSERT INTO Students VALUES ('Alex', 'UNC'," + " 19, 8000, 40000, 'M')");
  53. statement.executeUpdate("INSERT INTO Students VALUES ('Natasha', 'Harvard'," + " 22, 15000, 75000, 'F')");
  54. statement.executeUpdate("INSERT INTO Students VALUES ('Kelly', 'UCLA'," + " 23, 2000, 50000, 'F')");
  55. statement.executeUpdate("INSERT INTO Students VALUES ('Angela', 'NYU'," + "18, 8000, 45000, 'F')");
  56.  
  57. statement.executeUpdate("CREATE TABLE Schools (Name VARCHAR(10), Location VARCHAR(30), "
  58. + "TuitonFees INTEGER, LivingExpenses INTEGER)");
  59. statement.executeUpdate("INSERT INTO Schools VALUES ('NC State', 'North Carolina', 24000, 20000)");
  60. statement.executeUpdate("INSERT INTO Schools VALUES ('Stanford', 'California', 44000, 35000)");
  61. statement.executeUpdate("INSERT INTO Schools VALUES ('UNC', 'North Carolina', 34000, 20000)");
  62. statement.executeUpdate("INSERT INTO Schools VALUES ('Harvard', 'Massachusetts', 50000, 38000)");
  63. statement.executeUpdate("INSERT INTO Schools VALUES ('UCLA', 'California', 36000, 30000)");
  64. statement.executeUpdate("INSERT INTO Schools VALUES ('NYU', 'New York', 22000, 41000)");
  65. } catch (ClassNotFoundException e) {
  66. e.printStackTrace();
  67. } catch (SQLException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71.  
  72. private static void connectToDatabase() throws ClassNotFoundException, SQLException {
  73. Class.forName("org.mariadb.jdbc.Driver");
  74.  
  75. String user = "dwdeans";
  76. String password = "200049114";
  77.  
  78. connection = DriverManager.getConnection(jdbcURL, user, password);
  79. statement = connection.createStatement();
  80.  
  81. try {
  82. statement.executeUpdate("DROP TABLE Students");
  83. statement.executeUpdate("DROP TABLE Schools");
  84. } catch (SQLException e) {
  85. }
  86. }
  87.  
  88. private static boolean checkAbilityToStudy(String studentName) {
  89. try {
  90. result = statement
  91. .executeQuery("SELECT (FundingReceived+Income) AS TotalIncome, (TuitonFees+LivingExpenses) AS "
  92. + "TotalFees FROM Students, Schools WHERE Students.School = Schools.Name AND Students.Name "
  93. + "LIKE '" + studentName + "%'");
  94.  
  95. if (result.next()) {
  96. return (result.getInt("TotalIncome") > result.getInt("TotalFees"));
  97. }
  98. throw new RuntimeException(studentName + " cannot be found.");
  99. } catch (SQLException e) {
  100. e.printStackTrace();
  101. }
  102. return false;
  103. }
  104.  
  105. private static void modifyALittleBit1() throws SQLException {
  106. statement.executeUpdate("UPDATE Students SET Income = 39000 WHERE Name LIKE 'Angela%'");
  107. statement.executeUpdate("UPDATE Schools SET TuitonFees = 30000 WHERE Name = 'NYU'");
  108. }
  109.  
  110. private static void modifyALittleBit2() throws SQLException {
  111. try{
  112. connection.setAutoCommit(false);
  113. statement.executeUpdate("UPDATE Students SET Income = 55000 WHERE Name LIKE 'Angela%'");
  114. statement.executeUpdate("UPDATE Schools SET TuitonFees = 15000 WHERE Name = 'NYU'");
  115. connection.commit();
  116. connection.setAutoCommit(true);
  117. } catch (SQLException e){
  118. //do nothing
  119. }
  120. }
  121.  
  122. private static void close() {
  123. if (connection != null) {
  124. try {
  125. connection.close();
  126. } catch (SQLException e) {
  127. e.printStackTrace();
  128. }
  129. }
  130. if (statement != null) {
  131. try {
  132. statement.close();
  133. } catch (SQLException e) {
  134. e.printStackTrace();
  135. }
  136. }
  137. if (result != null) {
  138. try {
  139. result.close();
  140. } catch (SQLException e) {
  141. e.printStackTrace();
  142. }
  143. }
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement