Guest User

2/2

a guest
Dec 8th, 2016
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 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. public static void dbConnection() throws ClassNotFoundException, SQLException {
  22. Class.forName("oracle.jdbc.driver.OracleDriver");
  23. System.out.println("Successful!");
  24.  
  25. Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "system", "elephant");
  26. System.out.println("2....connection established");
  27.  
  28. Statement stmt = conn.createStatement();
  29.  
  30. String createTableSQLs = "Drop table FeedingData";
  31. stmt.executeUpdate(createTableSQLs);
  32.  
  33. String createTableSQL = "CREATE TABLE FeedingData(animalName VARCHAR2(30), amountFed number)";
  34. stmt.executeUpdate(createTableSQL);
  35.  
  36. PreparedStatement preparedUpdateStmt = conn.prepareStatement("INSERT INTO FeedingData(animalName, amountFed) VALUES (?, ?)");
  37.  
  38. int rhinoSum = 0;
  39. int cowSum = 0;
  40. int horseSum = 0;
  41. int zebraSum = 0;
  42. int deerSum = 0;
  43.  
  44. for (int i = 0; i < rhino.size(); i++) {
  45. rhinoSum += rhino.get(i);
  46. }
  47.  
  48.  
  49. for (int i = 0; i < FeedingZooAnimals.cow.size(); i++) {
  50. cowSum += FeedingZooAnimals.cow.get(i);
  51. }
  52.  
  53.  
  54. for (int i = 0; i < FeedingZooAnimals.horse.size(); i++) {
  55. horseSum += FeedingZooAnimals.horse.get(i);
  56. }
  57.  
  58.  
  59. for (int i = 0; i < FeedingZooAnimals.zebra.size(); i++) {
  60. zebraSum += FeedingZooAnimals.zebra.get(i);
  61. }
  62.  
  63.  
  64. for (int i = 0; i < FeedingZooAnimals.deer.size(); i++) {
  65. deerSum += FeedingZooAnimals.deer.get(i);
  66. }
  67.  
  68. preparedUpdateStmt.setString(1, "Rhino");
  69. preparedUpdateStmt.setInt(2, rhinoSum);
  70. preparedUpdateStmt.executeUpdate();
  71.  
  72. preparedUpdateStmt.setString(1, "Cow");
  73. preparedUpdateStmt.setInt(2, cowSum);
  74. preparedUpdateStmt.executeUpdate();
  75.  
  76. preparedUpdateStmt.setString(1, "Horse");
  77. preparedUpdateStmt.setInt(2, horseSum);
  78. preparedUpdateStmt.executeUpdate();
  79.  
  80. preparedUpdateStmt.setString(1, "Zebra");
  81. preparedUpdateStmt.setInt(2, zebraSum);
  82. preparedUpdateStmt.executeUpdate();
  83.  
  84. preparedUpdateStmt.setString(1, "Deer");
  85. preparedUpdateStmt.setInt(2, deerSum);
  86. preparedUpdateStmt.executeUpdate();
  87.  
  88.  
  89.  
  90.  
  91. System.out.println("\nAnimalName AmountFed");
  92. ResultSet rs = stmt.executeQuery("SELECT * FROM FeedingData");
  93. while (rs.next()) {
  94. String animalName = rs.getString("animalName");
  95. int amountFed = rs.getInt("amountFed");
  96. System.out.println(animalName + "\t\t " + amountFed);
  97. }
  98. // TODO code application logic here
  99. }
  100. }
Add Comment
Please, Sign In to add comment