Advertisement
Guest User

Untitled

a guest
Mar 30th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. /****************************
  2. * PROJECT NAME: JdbcEx1.java
  3. * PURPOSE:
  4. * CODER: David Birnie
  5. * DATE: Mar 29, 2016
  6. ****************************
  7. */
  8. package exercises;
  9. import java.util.*;
  10. import java.sql.*;
  11. import java.text.DecimalFormat;
  12.  
  13.  
  14. public class JdbcEx2
  15. {
  16. static Connection conn = null;
  17. static String dbURL = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
  18. static String user = "projects";
  19. static String password = "projects";
  20. static Statement stmt = null;
  21.  
  22. public JdbcEx2()
  23. {
  24. }
  25.  
  26. public static void main (String args[]) throws SQLException
  27. {
  28. Scanner input = new Scanner(System.in);
  29. DecimalFormat moneyFormat = new DecimalFormat("$###,###,##0.00 ");
  30. try
  31. {
  32. Class.forName("oracle.jdbc.driver.OracleDriver");
  33. }
  34. catch (ClassNotFoundException e)
  35. {
  36. System.err.println(e.getMessage());
  37. }
  38.  
  39. /*Open and Close
  40. * Connection
  41. */
  42.  
  43. try
  44. {
  45. conn = DriverManager.getConnection(dbURL,user,password);
  46. conn.clearWarnings();
  47. System.out.println("Connection Opened! For driver ==> Oracle 11XE");
  48. stmt = conn.createStatement();
  49. String userInput;
  50. for(;;)
  51. {
  52. System.out.print("\n\n" + "Please enter 1 to print VQ1, 2 to print VQ2 or X to exit the program: ");
  53. userInput = input.nextLine();
  54.  
  55. if(userInput.equals("1"))
  56. {
  57. ResultSet rs = stmt.executeQuery("select * from VQ1");
  58. System.out.println("\n*********David Birnie's Lab**********\n");
  59. System.out.printf("%10s\t%34s\t%s\n", "PRODUCT ID", "PRODUCTNAME","UNITPRICE");
  60. System.out.printf("%10s\t%34s\t%s\n","----------","------------------------", "----------");
  61. while (rs.next())
  62. {
  63. System.out.printf("%10s\t%34s\t%s\n", rs.getString("productid"),
  64. rs.getString("productname"), moneyFormat.format(Double.parseDouble(rs.getString("unitprice"))));
  65. }
  66. rs.close();
  67. }
  68.  
  69. else if (userInput.equals("2"))
  70. {
  71. ResultSet rs2 = stmt.executeQuery("select * from VQ2");
  72. System.out.println("\n*********David Birnie's Lab**********\n");
  73. System.out.printf("%10s\t%34s\t%s\n", "ORDERID", "CUSTOMERID", "COMPANY NAME");
  74. System.out.printf("%10s\t%34s\t%s\n","----------","------------------------", "----------");
  75. while (rs2.next())
  76. {
  77. System.out.printf("%10s\t%34s\t%s\n", rs2.getString("orderid"), rs2.getDate("orderdate"), rs2.getString("companyname"));
  78.  
  79. }
  80. rs2.close();
  81. }
  82. else if (userInput.equals("X") || (userInput.equals("x")))
  83. {
  84. System.out.println("'X' Entered, Terminating program... \n\n");
  85. break;
  86. }
  87. else
  88. {
  89. System.out.println("Incorrect input, please try again!");
  90. }
  91. }
  92. }
  93. catch(SQLException e)
  94. {
  95. System.err.println(e.getMessage());
  96. }
  97. finally
  98. {
  99. if (!conn.isClosed())
  100. {
  101. conn.close();
  102. System.out.println("Connection Closed! Oracle");
  103. }
  104. }
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement