Advertisement
Guest User

Untitled

a guest
Mar 31st, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. package exercise;
  2.  
  3. /**
  4. * @author Mike Pasara Sec 03 0741815
  5. */
  6.  
  7. import java.sql.*;
  8. import java.util.*;
  9.  
  10. public class databaseLab10
  11. {
  12. static Connection conn = null;
  13. static String bdURL = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
  14. static String user = "projects";
  15. static String password = "projects";
  16. static Statement q1 = null;
  17. static Statement q2 = null;
  18.  
  19. public databaseLab10() {};
  20.  
  21. public static void main(String[] args) throws SQLException
  22. {
  23. try{
  24. Class.forName("oracle.jdbc.driver.OracleDriver");
  25. } catch (ClassNotFoundException ex){
  26. System.err.println(ex.getMessage());
  27. }//end try catch
  28.  
  29. try{
  30.  
  31. conn = DriverManager.getConnection(bdURL,user,password);
  32. conn.clearWarnings();
  33. System.out.println("Connection opened for Driver ==> Oracle 11EX");
  34.  
  35. Scanner keyboard = new Scanner(System.in);
  36. String choice = "";
  37.  
  38. while(!choice.equalsIgnoreCase("x")){
  39. System.out.print("Mike - Which Question would you like to see? Q1 (1) or Q2 (2), press x to exit: ");
  40. choice = keyboard.next();
  41.  
  42. if(choice.equalsIgnoreCase("1"))
  43. doQ1(conn);
  44.  
  45. else if(choice.equalsIgnoreCase("2"))
  46. doQ2(conn);
  47.  
  48. else
  49. System.out.println("~Invalid Input~");
  50.  
  51. System.out.println("\n");
  52. }//end while
  53.  
  54. keyboard.close();
  55. } catch(SQLException ex){
  56. System.err.println(ex.getMessage());
  57. } finally{
  58. if(!conn.isClosed()){
  59. conn.close();
  60.  
  61. System.out.println("Connection closed");
  62. }//end if
  63. }//end try catch finally
  64. }// end main"
  65.  
  66. public static void doQ1(Connection c){
  67. try
  68. {
  69. q1 = c.createStatement();
  70. ResultSet rs = q1.executeQuery("SELECT * FROM VQ1");
  71.  
  72. System.out.println("Id Product Name UnitPrice");
  73. System.out.println("-- ------------ ---------");
  74. while(rs.next()){
  75. System.out.printf("%-7s %-35s%10s\n",rs.getString(1), rs.getString(2), rs.getString(3));
  76. }//end while
  77. rs.close();
  78.  
  79. } catch (SQLException ex){
  80. System.err.println(ex.getMessage());
  81. }// end try catch
  82.  
  83. }//end method
  84.  
  85. /**
  86. * Method Name: doQ2
  87. * purpose: executes a sql statement
  88. * @param: Connection c; the connection
  89. * @return: nothing
  90. */
  91.  
  92. public static void doQ2(Connection c){
  93. try
  94. {
  95. q2 = c.createStatement();
  96. ResultSet rs = q2.executeQuery("SELECT * FROM VQ2");
  97.  
  98. System.out.println("Id Order Date Shipped Date Company Name");
  99. System.out.println("-- ---------- ------------ ------------");
  100. while(rs.next()){
  101.  
  102. System.out.printf("%-9s %-25s %9s %23s\n",rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
  103.  
  104. }//end while
  105.  
  106. rs.close();
  107.  
  108. } catch (SQLException ex){
  109. System.err.println(ex.getMessage());
  110. }//end try catch
  111. }//end method
  112.  
  113. }
  114. //end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement