Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package feedingzoo;
  7.  
  8. import static feedingzoo.FeedingZooAnimals.rhino;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.sql.Statement;
  15.  
  16. /**
  17. *
  18. * @author Amrit
  19. */
  20. public class Database1 {
  21.  
  22. public static void dbConnection() throws ClassNotFoundException, SQLException {
  23. Class.forName("oracle.jdbc.driver.OracleDriver");
  24. System.out.println("Successful!");
  25.  
  26. Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "system", "elephant");
  27. System.out.println("2....connection established");
  28.  
  29. Statement stmt = conn.createStatement();
  30.  
  31. String createTableSQLs = "Drop table FeedingData";
  32. stmt.executeUpdate(createTableSQLs);
  33.  
  34. String createTableSQL = "CREATE TABLE FeedingData(animalName VARCHAR2(30), amountFed number)";
  35. stmt.executeUpdate(createTableSQL);
  36.  
  37. PreparedStatement preparedUpdateStmt = conn.prepareStatement("INSERT INTO FeedingData(animalName, amountFed) VALUES (?, ?)");
  38.  
  39. int rhinoSum = 0;
  40. int cowSum = 0;
  41. int horseSum = 0;
  42. int zebraSum = 0;
  43. int deerSum = 0;
  44.  
  45. for (int i = 0; i < rhino.size(); i++) {
  46. rhinoSum += rhino.get(i);
  47. }
  48.  
  49. for (int i = 0; i < FeedingZooAnimals.cow.size(); i++) {
  50. cowSum += FeedingZooAnimals.cow.get(i);
  51. }
  52.  
  53. for (int i = 0; i < FeedingZooAnimals.horse.size(); i++) {
  54. horseSum += FeedingZooAnimals.horse.get(i);
  55. }
  56.  
  57. for (int i = 0; i < FeedingZooAnimals.zebra.size(); i++) {
  58. zebraSum += FeedingZooAnimals.zebra.get(i);
  59. }
  60.  
  61. for (int i = 0; i < FeedingZooAnimals.deer.size(); i++) {
  62. deerSum += FeedingZooAnimals.deer.get(i);
  63. }
  64.  
  65. preparedUpdateStmt.setString(1, "Rhino");
  66. preparedUpdateStmt.setInt(2, rhinoSum);
  67. preparedUpdateStmt.executeUpdate();
  68.  
  69. preparedUpdateStmt.setString(1, "Cow");
  70. preparedUpdateStmt.setInt(2, cowSum);
  71. preparedUpdateStmt.executeUpdate();
  72.  
  73. preparedUpdateStmt.setString(1, "Horse");
  74. preparedUpdateStmt.setInt(2, horseSum);
  75. preparedUpdateStmt.executeUpdate();
  76.  
  77. preparedUpdateStmt.setString(1, "Zebra");
  78. preparedUpdateStmt.setInt(2, zebraSum);
  79. preparedUpdateStmt.executeUpdate();
  80.  
  81. preparedUpdateStmt.setString(1, "Deer");
  82. preparedUpdateStmt.setInt(2, deerSum);
  83. preparedUpdateStmt.executeUpdate();
  84.  
  85. System.out.println("\nAnimalName AmountFed");
  86. ResultSet rs = stmt.executeQuery("SELECT * FROM FeedingData");
  87. while (rs.next()) {
  88. String animalName = rs.getString("animalName");
  89. int amountFed = rs.getInt("amountFed");
  90. System.out.println(animalName + "\t\t " + amountFed);
  91. }
  92.  
  93. rs = stmt.executeQuery("select * from feedingData where amountfed = (select max(amountfed) from feedingData)");
  94. System.out.println("\nThe Animal with the most eaten....");
  95. while (rs.next()) {
  96. String animalName = rs.getString("animalName");
  97. int amountFed = rs.getInt("amountFed");
  98. System.out.println(animalName + "\t\t " + amountFed);
  99. }
  100.  
  101. rs = stmt.executeQuery("select sum(amountFed) from feedingData");
  102. System.out.println("\nTotal Amount Eaten By All");
  103. while (rs.next()) {
  104. int amountFed = rs.getInt("sum(amountFed)");
  105. System.out.println(amountFed);
  106. }
  107. // TODO code application logic here
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement