Advertisement
Guest User

Untitled

a guest
Apr 12th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.sql.*;
  5. import java.util.*;
  6.  
  7. public class Practicum
  8. {
  9.  
  10. //for keyboard input
  11. static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  12. static String custID = "";
  13. static String dbURL ="jdbc:oracle:thin:@127.0.0.1:1521:XE";
  14. static String user ="projects";
  15. static String password ="projects";
  16. static Connection conn =null;
  17. static Statement stmt = null;
  18.  
  19. public static void main(String[] args) throws SQLException
  20. {
  21. /*
  22. * Register the driver with the Class manager
  23. */
  24. try
  25. {
  26. Class.forName("oracle.jdbc.driver.OracleDriver");
  27. }
  28. catch (ClassNotFoundException e)
  29. {
  30. System.err.println(e.getMessage());
  31. }
  32.  
  33. /* Open the connection */
  34. try
  35. {
  36. conn = DriverManager.getConnection(dbURL, user, password);
  37. conn.clearWarnings();
  38. }
  39. catch(SQLException e)
  40. {
  41. System.err.println(e.getMessage());
  42. }
  43.  
  44. try
  45. {
  46. while(!custID.equalsIgnoreCase("X"))
  47. {
  48. System.out.print("Enter Customer ID: ");
  49. custID = br.readLine();
  50. System.out.println("\n");
  51. doQ2(conn);
  52. doQ1(conn);
  53.  
  54. }
  55. }
  56. catch(Exception e)
  57. {
  58. System.err.println(e.getMessage());
  59. }
  60. }
  61.  
  62. public static void doQ1(Connection conn)
  63. {
  64. try
  65. {
  66. conn = DriverManager.getConnection(dbURL, user, password);
  67. conn.clearWarnings();
  68. stmt = conn.createStatement();
  69. ResultSet rs = stmt.executeQuery("SELECT o.orderid, o.orderdate, NVL2(o.shippeddate, 'Shipped', 'Not Shipped') AS \"Status\", TO_CHAR(SUM((od.unitprice - (od.unitprice * od.discount)) * od.quantity), '$9,999.99') AS \"Order Total\" FROM Orders o INNER JOIN OrderDetails od ON o.orderid = od.orderid WHERE o.customerid = '" +custID +"' GROUP BY o.orderid, o.orderdate, o.shippeddate, o.customerid ORDER BY 4 DESC");
  70. ResultSetMetaData meta = rs.getMetaData();
  71. System.out.printf("%1s %17s %15s %23s \n", meta.getColumnName(1), meta.getColumnName(2), meta.getColumnName(3), meta.getColumnName(4));
  72. while (rs.next())
  73. {
  74. System.out.printf("%1s %20s %15s %20s \n", rs.getString(1),rs.getDate(2),rs.getString(3), rs.getString(4));
  75. }
  76. }
  77. catch(SQLException e)
  78. {
  79. System.err.println(e.getMessage());
  80. }
  81. }
  82.  
  83. public static void doQ2(Connection conn)
  84. {
  85. try
  86. {
  87. conn = DriverManager.getConnection(dbURL, user, password);
  88. conn.clearWarnings();
  89. stmt = conn.createStatement();
  90. ResultSet rs = stmt.executeQuery("SELECT CompanyName, (SELECT sysdate FROM dual) AS \"Date\" FROM Customers WHERE customerID='ERNSH'");
  91. while (rs.next())
  92. {
  93. System.out.println("Statistics for "+rs.getString(1)
  94. + " "+rs.getDate(2) +" by Tiberiu Egyed");
  95. }
  96. }
  97. catch(SQLException e)
  98. {
  99. System.err.println(e.getMessage());
  100. }
  101. }
  102. }
  103. //end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement