Advertisement
Guest User

Untitled

a guest
Mar 7th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. package stock;
  2.  
  3. // Skeleton version of StockData.java that links to a database.
  4. // NOTE: You should not have to make any changes to the other
  5. // Java GUI classes for this to work, if you complete it correctly.
  6. // Indeed these classes shouldn't even need to be recompiled
  7. import java.sql.*; // DB handling package
  8. import java.io.*;
  9. //import org.apache.derby.drda.NetworkServerControl;
  10.  
  11. public class StockData {
  12.  
  13. private static Connection connection;
  14. private static Statement stmt;
  15.  
  16. static {
  17. // standard code to open a connection and statement to an Access database
  18. try {
  19. // NetworkServerControl server = new NetworkServerControl();
  20. // server.start(null);
  21. // Load JDBC driver
  22. //Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
  23. //Establish a connection
  24. //String sourceURL = "jdbc:derby://localhost:1527/"
  25. // + new File("UserDB").getAbsolutePath() + ";";
  26. String sourceURL = "jdbc:derby://localhost:1527/StockData";
  27. connection = DriverManager.getConnection(sourceURL, "username", "password");
  28. stmt = connection.createStatement();
  29. } // The following exceptions must be caught
  30. //catch (ClassNotFoundException cnfe) {
  31. // System.out.println(cnfe);
  32. //}
  33. catch (SQLException sqle) {
  34. System.out.println(sqle);
  35. } catch (Exception e) {
  36. System.out.println(e);
  37. }
  38.  
  39. }
  40.  
  41. // You could make methods getName, getPrice and getQuantity simpler by using an auxiliary
  42. // private String method getField(String key, int fieldNo) to return the appropriate field as a String
  43. public static String getName(String key) {
  44. try {
  45. // Need single quote marks ' around the key field in SQL. This is easy to get wrong!
  46. // For instance if key was "11" the SELECT statement would be:
  47. // SELECT * FROM Stock WHERE stockKey = '11'
  48. ResultSet res = stmt.executeQuery("select * from USERNAME.STOCK WHERE stockKey = '" + key + "'");
  49. if (res.next()) { // there is a result
  50. // the name field is the second one in the ResultSet
  51. // Note that with ResultSet we count the fields starting from 1
  52. return res.getString(5);
  53. } else {
  54. return null;
  55. }
  56. } catch (SQLException e) {
  57. System.out.println(e);
  58. return null;
  59. }
  60. }
  61.  
  62. public static double getPrice(String key) {
  63. try {
  64. // Need single quote marks ' around the key field in SQL. This is easy to get wrong!
  65. // For instance if key was "11" the SELECT statement would be:
  66. // SELECT * FROM Stock WHERE stockKey = '11'
  67. ResultSet res = stmt.executeQuery("select * from USERNAME.STOCK WHERE stockKey = '" + key + "'");
  68. if (res.next()) { // there is a result
  69. // the name field is the second one in the ResultSet
  70. // Note that with ResultSet we count the fields starting from 1
  71. return res.getDouble(3);
  72. } else {
  73. return -1;
  74. }
  75. } catch (SQLException e) {
  76. System.out.println(e);
  77. return -1;
  78. }
  79. }
  80.  
  81. public static int getQuantity(String key) {
  82. try {
  83. // Need single quote marks ' around the key field in SQL. This is easy to get wrong!
  84. // For instance if key was "11" the SELECT statement would be:
  85. // SELECT * FROM Stock WHERE stockKey = '11'
  86. ResultSet res = stmt.executeQuery("select * from USERNAME.STOCK WHERE stockKey = '" + key + "'");
  87. if (res.next()) { // there is a result
  88. // the name field is the second one in the ResultSet
  89. // Note that with ResultSet we count the fields starting from 1
  90. return res.getInt(2);
  91. } else {
  92. return -1;
  93. }
  94. } catch (SQLException e) {
  95. System.out.println(e);
  96. return -1;
  97. }
  98. }
  99.  
  100. // update stock levels
  101. // extra is +ve if adding stock
  102. // extra is -ve if selling stock
  103. public static void update(String key, int extra) {
  104. // SQL UPDATE statement required. For instance if extra is 5 and stockKey is "11" then updateStr is
  105. // UPDATE Stock SET stockQuantity = stockQuantity + 5 WHERE stockKey = '11'
  106. String updateStr = "UPDATE Stock SET stockQuantity = stockQuantity + " + extra + " WHERE stockKey = '" + key + "'";
  107. System.out.println(updateStr);
  108. try {
  109. stmt.executeUpdate(updateStr);
  110. } catch (SQLException e) {
  111. System.out.println(e);
  112. }
  113. }
  114.  
  115. // close the database
  116. public static void close() {
  117. try {
  118. connection.close();
  119. } catch (SQLException e) {
  120. // this shouldn't happen
  121. System.out.println(e);
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement