Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class App {
- private static String dbName;
- private static String userName;
- private static String password;
- public static void connect() throws IOException {
- BufferedReader br = new BufferedReader(new FileReader("login.txt"));
- dbName = br.readLine();
- userName = br.readLine();
- password = br.readLine();
- Connection c = null;
- try {
- Class.forName("org.postgresql.Driver");
- c = DriverManager.getConnection(dbName, userName, password);
- } catch (Exception e) {
- e.printStackTrace();
- System.err.println(e.getClass().getName() + ": " + e.getMessage());
- System.exit(0);
- }
- System.out.println("Opened database successfully");
- }
- public static void query() {
- Connection c = connect(); // call function - mistake
- Statement stmt = c.createStatement();
- stmt = c.createStatement();
- String query = "Select count(distinct country) sum from customers";
- ResultSet rs = stmt.executeQuery(query);
- query = "SELECT C.country, SUM(O.netamount) netamount" + "from customers as C join orders as O "
- + "on C.customerid = O.customerid " + "where O.orderdate < '20040701' and O.orderdate > '20031231' "
- + "group by C.country " + "order by SUM(O.netamount) DESC";
- rs = stmt.executeQuery(query);
- while (rs.next()) {
- String country = rs.getString("country");
- String netamount = rs.getString("netamount");
- System.out.println(country + " " + netamount);
- System.out.println();
- }
- rs.close();
- stmt.close();
- c.close();
- }
- public static void main(String[] args) {
- query(); // call function
- }
Add Comment
Please, Sign In to add comment