Advertisement
Guest User

Untitled

a guest
Mar 30th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.sql.*;
  5.  
  6. public class LabWeek12
  7.  
  8. {
  9. static Connection conn =null;
  10. static String dbURL ="jdbc:oracle:thin:@127.0.0.1:1521:XE";
  11. static String user ="projects";
  12. static String password ="projects";
  13. static Statement stmt = null;
  14. /* Creates a new instance of JdbcEx1 */
  15.  
  16. public LabWeek12(){ }
  17.  
  18. public static void main (String args[]) throws SQLException, IOException
  19. {
  20. /*
  21. * Register the driver with the Class manager
  22. */
  23. try
  24. {
  25. Class.forName("oracle.jdbc.driver.OracleDriver");
  26. }
  27. catch (ClassNotFoundException e)
  28. {
  29. System.err.println(e.getMessage());
  30. }
  31.  
  32. /* Open the connection */
  33. try
  34. {
  35. conn = DriverManager.getConnection(dbURL, user, password);
  36. conn.clearWarnings();
  37. System.out.println("Connection opened! for driver ==> Oracle 11XE)");
  38. }
  39. catch(SQLException e)
  40. {
  41. System.err.println(e.getMessage());
  42. }
  43.  
  44. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  45. String option="1";
  46.  
  47. while(!option.equalsIgnoreCase("X"))
  48. {
  49. System.out.print("Want to do Q1 (1) or Q2 (2) or (X) exit: ");
  50. option = br.readLine();
  51. System.out.println("");
  52. if (option.equalsIgnoreCase("1"))
  53. {
  54. doQ1(conn);
  55. }
  56. else if(option.equalsIgnoreCase("2"))
  57. {
  58. doQ2(conn);
  59. }
  60. else if(option.equalsIgnoreCase("X"))
  61. {
  62. conn.close();
  63. System.out.println("Connection closed!");
  64. }
  65. }
  66.  
  67.  
  68. }
  69.  
  70.  
  71. public static void doQ1(Connection conn)
  72. {
  73. try
  74. {
  75. conn = DriverManager.getConnection(dbURL, user, password);
  76. conn.clearWarnings();
  77. stmt = conn.createStatement();
  78. ResultSet rs = stmt.executeQuery("SELECT * FROM VQ1");
  79.  
  80. while (rs.next())
  81. {
  82. System.out.println(rs.getString("PRODUCTID")
  83. + "\t"+ rs.getString("ProductName") + " "
  84. +rs.getString("UNITPRICE"));
  85. }
  86. }
  87. catch(SQLException e)
  88. {
  89. System.err.println(e.getMessage());
  90. }
  91. }
  92.  
  93. public static void doQ2(Connection conn)
  94. {
  95. {
  96. try
  97. {
  98. conn = DriverManager.getConnection(dbURL, user, password);
  99. conn.clearWarnings();
  100. stmt = conn.createStatement();
  101. ResultSet rs = stmt.executeQuery("SELECT * FROM VQ2");
  102.  
  103. while (rs.next())
  104. {
  105. System.out.println(rs.getString("OrderID")
  106. + "\t"+ rs.getDate("OrderDate") + " "
  107. +rs.getDate("ShippedDate")+ " "+rs.getString("CompanyName"));
  108. }
  109. }
  110. catch(SQLException e)
  111. {
  112. System.err.println(e.getMessage());
  113. }
  114. }
  115. }
  116. }
  117. //end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement