Guest User

Untitled

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