Advertisement
Guest User

Untitled

a guest
Mar 31st, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. package exercise;
  2. import java.sql.*;
  3. import java.util.*;
  4.  
  5. /**
  6. * Program Name: databaseLab10.java
  7. * Purpose: databaseLab10.java - connect to a database using Oracle Thin Driver
  8. * Coder: Mike Pasara Sec03 0741815
  9. * Date: Mar 31, 2016
  10. */
  11.  
  12. public class databaseLab10
  13. {
  14. static Connection conn = null;
  15. static Statement stmt = null;
  16. static String dbURL = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
  17. static String user = "projects";
  18. static String password = "projects";
  19. static String option = "1";
  20. static Scanner input = new Scanner(System.in);
  21. static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  22.  
  23.  
  24. //creates a new instance of databaseLab
  25. public databaseLab10()
  26. {
  27.  
  28. }
  29.  
  30. public static void main (String args[]) throws SQLException
  31. {
  32. //Register the driver with the Class manager
  33.  
  34. try
  35. {
  36. Class.forName("oracle.jdbc.driver.OracleDriver");
  37. }
  38. catch (ClassNotFoundException e)
  39. {
  40. System.err.println(e.getMessage());
  41. }
  42.  
  43. //Open and close the connection
  44. try
  45. {
  46. conn = DriverManager.getConnection(dbURL, user, password);
  47. conn.clearWarnings();
  48. System.out.println("Connection opened! for driver ==> Oracle 11E");
  49. stmt = conn.createStatement();
  50. ResultSet rs = stmt.executeQuery("select * from customers");
  51.  
  52. //Dump out the data 2 ways - 1) by ordinal position, and 2) by name
  53. while (rs.next())
  54. {
  55. System.out.println(rs.getString(2) + " " + rs.getString("CUSTLASTNAME"));
  56. }
  57. rs.close();
  58. }
  59. catch (SQLException e)
  60. {
  61. System.err.println(e.getMessage());
  62. }
  63.  
  64. finally
  65. {
  66. if (!conn.isClosed())
  67. {
  68. conn.close();
  69. System.out.println("Connection closed! Oracle");
  70. }// end if
  71.  
  72. }//end finally
  73.  
  74. String option = null;
  75.  
  76. while ( !option.equalsIgnoreCase("X") )
  77. {
  78. System.out.print("Mike - Want to do Q1 (1) or Q2 (2) or (X) exit: ");
  79. option = input.nextLine();
  80. System.out.println("");
  81.  
  82. if( option.equalsIgnoreCase("1") )
  83. {
  84. doQ1(conn);
  85. }
  86. else if ( option.equalsIgnoreCase("2") )
  87. {
  88. doQ2(conn);
  89. }
  90. else if ( option.equalsIgnoreCase("X") )
  91. {
  92. if( !conn.isClosed() )
  93. {
  94. conn.close();
  95. System.out.println("Connection closed!");
  96. }
  97. }
  98. else
  99. System.out.println("Invalid Input Please Try again! [ 1, 2, or X] ");
  100. }
  101.  
  102. }//end main
  103.  
  104. public static void doQ1(Connection c){
  105. try
  106. {
  107. q1 = c.createStatement();
  108. ResultSet rs = q1.executeQuery("SELECT * FROM VQ1");
  109.  
  110. System.out.println("Id Product Name UnitPrice");
  111. System.out.println("-- ------------ ---------");
  112. while(rs.next()){
  113. System.out.printf("%-7s %-35s%10s\n",rs.getString(1), rs.getString(2), rs.getString(3));
  114. }//end while
  115. rs.close();
  116.  
  117. } catch (SQLException ex){
  118. System.err.println(ex.getMessage());
  119. }// end try catch
  120.  
  121. }//end method
  122.  
  123. /**
  124. * Method Name: doQ2
  125. * purpose: executes a sql statement
  126. * @param: Connection c; the connection
  127. * @return: nothing
  128. */
  129.  
  130. public static void doQ2(Connection c){
  131. try
  132. {
  133. q2 = c.createStatement();
  134. ResultSet rs = q2.executeQuery("SELECT * FROM VQ2");
  135.  
  136. System.out.println("Id Order Date Shipped Date Company Name");
  137. System.out.println("-- ---------- ------------ ------------");
  138. while(rs.next()){
  139.  
  140. System.out.printf("%-9s %-25s %9s %23s\n",rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
  141.  
  142. }//end while
  143.  
  144. rs.close();
  145.  
  146. } catch (SQLException ex){
  147. System.err.println(ex.getMessage());
  148. }//end try catch
  149. }//end method
  150. }// end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement