Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Connection URL or username or password errors!
- org.postgresql.util.PSQLException: ResultSet not positione properly, perhaps you need to call next.
- at org.postgresql.jdbc2.AbstractJdbc2ResultSet.checkResultSet(AbstractJdbc2ResultSet.java:2926)
- at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getString(AbstractJdbc2ResultSet.java:2001)
- at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getString(AbstractJdbc2ResultSet.java:2613)
- at HW2_report3.main(HW2_report3.java:59)
- public static void main(String[] args)
- {
- String usr ="zihengma";
- String pwd ="Maziheng1993";
- String url ="jdbc:postgresql://localhost:5432/postgres";
- try
- {
- Class.forName("org.postgresql.Driver");
- System.out.println("Success loading Driver!");
- }
- catch(Exception e)
- {
- System.out.println("Fail loading Driver!");
- e.printStackTrace();
- }
- try
- {
- // connect once, and create the first hashmap.
- Connection conn = DriverManager.getConnection(url, usr, pwd);
- System.out.println("Success connecting server!");
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM Sales");
- HashMap<String, ArrayList<Integer>> hashMap = new HashMap<String, ArrayList<Integer>>();
- while (rs.next()){
- String string = rs.getString("cust") + " " + rs.getString("prod");
- if (hashMap.containsKey(string)) {
- hashMap.get(string).set(0,hashMap.get(string).get(0) + Integer.parseInt(rs.getString("quant"))/3);
- }
- else{
- ArrayList<Integer> arrayList = new ArrayList<Integer>();
- arrayList.add(Integer.parseInt(rs.getString("quant"))/3);
- hashMap.put(string, arrayList);
- }
- }
- // connect second time, and create the second hashmap1
- //which can use the data in the first hashmap.
- Connection conn1 = DriverManager.getConnection(url, usr, pwd);
- System.out.println("Success connecting server!");
- Statement stmt1 = conn1.createStatement();
- ResultSet rs1 = stmt1.executeQuery("SELECT * FROM Sales");
- HashMap<String, ArrayList<Integer>> hashMap1 = new HashMap<String, ArrayList<Integer>>();
- while (rs1.next()){
- ArrayList<Integer> arrayList1 = new ArrayList<Integer>();
- String string1 = rs.getString("cust") + " " + rs.getString("prod");
- if (hashMap1.containsKey(string1)) {
- hashMap1.get(string1).set(0,hashMap1.get(string1).get(0) + Integer.parseInt(rs1.getString("quant")));
- if(arrayList1.get(0) == hashMap.get(string1).get(0)){
- arrayList1.set(1,Integer.parseInt(rs1.getString("month")));
- }
- }
- else{
- arrayList1.add(Integer.parseInt(rs1.getString("quant")));
- arrayList1.add(0);
- if(arrayList1.get(0) == hashMap.get(string1).get(0)){
- arrayList1.set(1,Integer.parseInt(rs1.getString("month")));
- }
- hashMap1.put(string1, arrayList1);
- }
- }
- System.out.printf("%-11s", "CUSTOMER");
- System.out.printf("%-11s", "PRODUCT");
- System.out.printf("%-11s", "1/3TOTAL");
- System.out.println();
- System.out.printf("%-11s", "========");
- System.out.printf("%-11s", "========");
- System.out.printf("%-11s", "========");
- System.out.println();
- Set<String> set = hashMap.keySet();
- for (String s: set) {
- String[] strings = s.split(" ");
- System.out.printf("%-11s", strings[0]);
- System.out.printf("%-11s", strings[1]);
- System.out.printf("%-11s", hashMap.get(s).get(0));
- System.out.println();
- }
- System.out.printf("%-11s", "CUSTOMER");
- System.out.printf("%-11s", "PRODUCT");
- System.out.printf("%-11s", "1/3Month");
- System.out.println();
- System.out.printf("%-11s", "========");
- System.out.printf("%-11s", "========");
- System.out.printf("%-11s", "========");
- System.out.println();
- Set<String> set1 = hashMap.keySet();
- for (String s: set1) {
- String[] strings = s.split(" ");
- System.out.printf("%-11s", strings[0]);
- System.out.printf("%-11s", strings[1]);
- System.out.printf("%-11s", hashMap1.get(s).get(1));
- System.out.println();
- }
- }catch(SQLException e)
- {
- System.out.println("Connection URL or username or password errors!");
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement