Advertisement
Guest User

Untitled

a guest
Apr 1st, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. package tarek;
  2.  
  3. import java.sql.*;
  4. import java.text.NumberFormat;
  5.  
  6. public class DBTestApp {
  7.  
  8. public static void main(String args[]) {
  9.  
  10. // Load the database driver
  11. // NOTE: This block is necessary for Oracle 10g (JDBC 3.0) or earlier,
  12. // but not for Oracle 11g (JDBC 4.0) or later
  13. try {
  14. Class.forName("oracle.jdbc.OracleDriver");
  15. } catch (ClassNotFoundException e) {
  16. System.out.println(e);
  17. }
  18.  
  19. // define common JDBC objects
  20. Connection connection = null;
  21. Statement statement = null;
  22. ResultSet rs = null;
  23. try {
  24. // Connect to the database
  25. String dbUrl = "jdbc:oracle:thin:@techlaw.law.miami.edu:1521:BTE423FALL15";
  26. String username = "tarek";
  27. String password = "mypassword";
  28. connection = DriverManager.getConnection(
  29. dbUrl, username, password);
  30.  
  31. // Execute a SELECT statement
  32. statement = connection.createStatement();
  33. String query
  34. = "SELECT vendor_name, invoice_number, invoice_total "
  35. + "FROM vendors INNER JOIN invoices "
  36. + " ON vendors.vendor_id = invoices.vendor_id "
  37. + "WHERE invoice_total >= 500 "
  38. + "ORDER BY vendor_name, invoice_total DESC";
  39. rs = statement.executeQuery(query);
  40.  
  41. // Display the results of a SELECT statement
  42. System.out.println("Invoices with totals over 500:\n");
  43. while (rs.next()) {
  44. String vendorName = rs.getString("vendor_name");
  45. String invoiceNumber = rs.getString("invoice_number");
  46. double invoiceTotal = rs.getDouble("invoice_total");
  47.  
  48. NumberFormat currency = NumberFormat.getCurrencyInstance();
  49. String invoiceTotalString = currency.format(invoiceTotal);
  50.  
  51. System.out.println(
  52. "Vendor: " + vendorName + "\n"
  53. + "Invoice No: " + invoiceNumber + "\n"
  54. + "Total: " + invoiceTotalString + "\n");
  55. }
  56. } catch (SQLException e) {
  57. System.out.println(e);
  58. } finally {
  59. try {
  60. if (rs != null) {
  61. rs.close();
  62. }
  63. if (statement != null) {
  64. statement.close();
  65. }
  66. if (connection != null) {
  67. connection.close();
  68. }
  69. } catch (SQLException e) {
  70. System.out.println(e);
  71. }
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement