Guest User

Untitled

a guest
Dec 14th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. public class App {
  2.  
  3. private static String dbName;
  4. private static String userName;
  5. private static String password;
  6.  
  7. public static void connect() throws IOException {
  8. BufferedReader br = new BufferedReader(new FileReader("login.txt"));
  9.  
  10. dbName = br.readLine();
  11. userName = br.readLine();
  12. password = br.readLine();
  13.  
  14. Connection c = null;
  15. try {
  16. Class.forName("org.postgresql.Driver");
  17. c = DriverManager.getConnection(dbName, userName, password);
  18. } catch (Exception e) {
  19. e.printStackTrace();
  20. System.err.println(e.getClass().getName() + ": " + e.getMessage());
  21. System.exit(0);
  22. }
  23. System.out.println("Opened database successfully");
  24. }
  25.  
  26. public static void query() {
  27.  
  28. Connection c = connect(); // call function - mistake
  29. Statement stmt = c.createStatement();
  30. stmt = c.createStatement();
  31. String query = "Select count(distinct country) sum from customers";
  32. ResultSet rs = stmt.executeQuery(query);
  33. query = "SELECT C.country, SUM(O.netamount) netamount" + "from customers as C join orders as O "
  34. + "on C.customerid = O.customerid " + "where O.orderdate < '20040701' and O.orderdate > '20031231' "
  35. + "group by C.country " + "order by SUM(O.netamount) DESC";
  36. rs = stmt.executeQuery(query);
  37. while (rs.next()) {
  38. String country = rs.getString("country");
  39. String netamount = rs.getString("netamount");
  40. System.out.println(country + " " + netamount);
  41. System.out.println();
  42. }
  43. rs.close();
  44. stmt.close();
  45. c.close();
  46. }
  47.  
  48. public static void main(String[] args) {
  49.  
  50. query(); // call function
  51. }
Add Comment
Please, Sign In to add comment