Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. package exercises;
  2.  
  3. import java.io.*;
  4. import java.sql.*;
  5.  
  6. /*
  7. * @author Jason Goldenberg
  8. * Created March 29th, 2017
  9. * Purpose: connect to database using Oracle
  10. */
  11.  
  12. public class jdbcEx1 {
  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 = "Password";
  19. public jdbcEx1() { }
  20.  
  21. public static void doQ1(Connection conn) throws SQLException
  22. {
  23. try {
  24. conn = DriverManager.getConnection(dbURL, user, password);
  25. System.out.println("Connection opened! For driver ==> Oracle 11XE");
  26. stmt = conn.createStatement();
  27. ResultSet qw = stmt.executeQuery("select productid, productname, unitprice" + " from VQ1");
  28. System.out.println("Lab 10 ~ Jason Goldenberg, 20XX");
  29. System.out.printf("%10s %10s %10s\n", "ID", "Product", "Price");
  30. System.out.printf("%10s %9s %10s\n", "===", "===", "===");
  31. while (qw.next()) {
  32. System.out.println(qw.getString(1) + "\t" + qw.getString(2) + "\t\tqqqqqqqqq\t\t$" + qw.getString(3));
  33. }
  34.  
  35.  
  36. qw.close();
  37.  
  38.  
  39. } catch (SQLException m) {
  40. System.err.println(m.getMessage());
  41. } finally {
  42. if (!conn.isClosed()) {
  43. conn.close();
  44. System.out.println("Connection closed");
  45. }
  46. }
  47. }
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56. public static void doQ2(Connection conn) throws SQLException
  57. {
  58.  
  59. try {
  60. conn = DriverManager.getConnection(dbURL, user, password);
  61.  
  62. conn.clearWarnings();
  63.  
  64.  
  65. System.out.println("Connection opened! For driver ==> Oracle 11XE");
  66. stmt = conn.createStatement();
  67. ResultSet variable = stmt.executeQuery("alter session set nls_date_format = 'DD-MM-YYYY'");
  68. ResultSet rs = stmt.executeQuery("select * from VQ2");
  69. System.out.println("");
  70. System.out.println("Lab 10 ~ Jason Goldenberg, 20XX");
  71. System.out.printf("%10s %15s %25s %27s\n", "SHIPPED DATE", "ORDER DATE", "ORDERID", "COMPANY NAME");
  72. System.out.printf("%10s %15s %25s %27s\n", "=====", "======", "========", "========");
  73. while (rs.next()) {
  74. System.out.println(" " + rs.getString("ORDERID") + "\t\t" + rs.getString(2) + "\t" + rs.getString(3) + "\t" + rs.getString("COMPANYNAME"));
  75. }
  76.  
  77.  
  78. rs.close();
  79.  
  80.  
  81. variable.close();
  82. } catch (SQLException m) {
  83. System.err.println(m.getMessage());
  84.  
  85. } finally {
  86.  
  87. if (!conn.isClosed()) {
  88. conn.close();
  89. System.out.println("Connection Closed! Oracle");
  90. }
  91. }
  92. }
  93.  
  94.  
  95.  
  96. public static void main (String args[]) throws SQLException, IOException {
  97. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  98. String option = "";
  99.  
  100.  
  101. while (!option.equalsIgnoreCase("X")) {
  102. System.out.print("Please choose 'Q1' or 'Q2?' or 'X' to end: ");
  103. option = br.readLine();
  104. System.out.println("");
  105.  
  106. if (option.equalsIgnoreCase("1")){
  107. doQ1(conn);
  108. }
  109. if (option.equalsIgnoreCase("2")){
  110. doQ2(conn);
  111.  
  112. System.out.println("");
  113. }
  114. }
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement