Advertisement
Guest User

Untitled

a guest
Apr 19th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. Connection URL or username or password errors!
  2. org.postgresql.util.PSQLException: ResultSet not positione properly, perhaps you need to call next.
  3. at org.postgresql.jdbc2.AbstractJdbc2ResultSet.checkResultSet(AbstractJdbc2ResultSet.java:2926)
  4. at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getString(AbstractJdbc2ResultSet.java:2001)
  5. at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getString(AbstractJdbc2ResultSet.java:2613)
  6. at HW2_report3.main(HW2_report3.java:59)
  7.  
  8. public static void main(String[] args)
  9. {
  10. String usr ="zihengma";
  11. String pwd ="Maziheng1993";
  12. String url ="jdbc:postgresql://localhost:5432/postgres";
  13.  
  14. try
  15. {
  16. Class.forName("org.postgresql.Driver");
  17. System.out.println("Success loading Driver!");
  18. }
  19.  
  20. catch(Exception e)
  21. {
  22. System.out.println("Fail loading Driver!");
  23. e.printStackTrace();
  24. }
  25.  
  26. try
  27. {
  28. // connect once, and create the first hashmap.
  29.  
  30. Connection conn = DriverManager.getConnection(url, usr, pwd);
  31. System.out.println("Success connecting server!");
  32.  
  33. Statement stmt = conn.createStatement();
  34. ResultSet rs = stmt.executeQuery("SELECT * FROM Sales");
  35. HashMap<String, ArrayList<Integer>> hashMap = new HashMap<String, ArrayList<Integer>>();
  36.  
  37. while (rs.next()){
  38. String string = rs.getString("cust") + " " + rs.getString("prod");
  39. if (hashMap.containsKey(string)) {
  40. hashMap.get(string).set(0,hashMap.get(string).get(0) + Integer.parseInt(rs.getString("quant"))/3);
  41. }
  42. else{
  43. ArrayList<Integer> arrayList = new ArrayList<Integer>();
  44. arrayList.add(Integer.parseInt(rs.getString("quant"))/3);
  45. hashMap.put(string, arrayList);
  46. }
  47. }
  48.  
  49. // connect second time, and create the second hashmap1
  50. //which can use the data in the first hashmap.
  51. Connection conn1 = DriverManager.getConnection(url, usr, pwd);
  52. System.out.println("Success connecting server!");
  53. Statement stmt1 = conn1.createStatement();
  54. ResultSet rs1 = stmt1.executeQuery("SELECT * FROM Sales");
  55.  
  56. HashMap<String, ArrayList<Integer>> hashMap1 = new HashMap<String, ArrayList<Integer>>();
  57. while (rs1.next()){
  58. ArrayList<Integer> arrayList1 = new ArrayList<Integer>();
  59. String string1 = rs.getString("cust") + " " + rs.getString("prod");
  60. if (hashMap1.containsKey(string1)) {
  61. hashMap1.get(string1).set(0,hashMap1.get(string1).get(0) + Integer.parseInt(rs1.getString("quant")));
  62. if(arrayList1.get(0) == hashMap.get(string1).get(0)){
  63. arrayList1.set(1,Integer.parseInt(rs1.getString("month")));
  64. }
  65. }
  66. else{
  67.  
  68. arrayList1.add(Integer.parseInt(rs1.getString("quant")));
  69. arrayList1.add(0);
  70. if(arrayList1.get(0) == hashMap.get(string1).get(0)){
  71. arrayList1.set(1,Integer.parseInt(rs1.getString("month")));
  72. }
  73. hashMap1.put(string1, arrayList1);
  74. }
  75. }
  76.  
  77. System.out.printf("%-11s", "CUSTOMER");
  78. System.out.printf("%-11s", "PRODUCT");
  79. System.out.printf("%-11s", "1/3TOTAL");
  80. System.out.println();
  81. System.out.printf("%-11s", "========");
  82. System.out.printf("%-11s", "========");
  83. System.out.printf("%-11s", "========");
  84. System.out.println();
  85. Set<String> set = hashMap.keySet();
  86. for (String s: set) {
  87. String[] strings = s.split(" ");
  88. System.out.printf("%-11s", strings[0]);
  89. System.out.printf("%-11s", strings[1]);
  90. System.out.printf("%-11s", hashMap.get(s).get(0));
  91. System.out.println();
  92. }
  93.  
  94. System.out.printf("%-11s", "CUSTOMER");
  95. System.out.printf("%-11s", "PRODUCT");
  96. System.out.printf("%-11s", "1/3Month");
  97. System.out.println();
  98. System.out.printf("%-11s", "========");
  99. System.out.printf("%-11s", "========");
  100. System.out.printf("%-11s", "========");
  101. System.out.println();
  102. Set<String> set1 = hashMap.keySet();
  103. for (String s: set1) {
  104. String[] strings = s.split(" ");
  105. System.out.printf("%-11s", strings[0]);
  106. System.out.printf("%-11s", strings[1]);
  107. System.out.printf("%-11s", hashMap1.get(s).get(1));
  108. System.out.println();
  109. }
  110.  
  111.  
  112. }catch(SQLException e)
  113. {
  114. System.out.println("Connection URL or username or password errors!");
  115. e.printStackTrace();
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement