Advertisement
Guest User

Untitled

a guest
Nov 9th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8.  
  9.  
  10.  
  11. public class Main {
  12.  
  13. public static HashMap<Integer, int[]> connectToAndQueryDatabase() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
  14.  
  15. HashMap map = new HashMap<Integer, int[]>();
  16.  
  17. Class.forName("com.mysql.jdbc.Driver").newInstance();
  18.  
  19. String connectionCommand = "jdbc:mysql://"+"localhost"+"/"+"tank" + "?user=root";
  20.  
  21. Connection con = DriverManager.getConnection(connectionCommand);
  22.  
  23. Statement stmt = con.createStatement();
  24. ResultSet rs = stmt.executeQuery("SELECT id, level, max FROM tbl_tank");
  25.  
  26.  
  27. while (rs.next()) {
  28. int x = rs.getInt("id");
  29. int s = rs.getInt("level");
  30. int f = rs.getInt("max");
  31.  
  32. int[] array = new int[2];
  33. array[0] = s;
  34. array[1] = f;
  35. map.put(x, array);
  36. }
  37.  
  38. return map;
  39. }
  40.  
  41. public static void main(String[] args) {
  42. Controller c = new Controller();
  43.  
  44. try {
  45. for (Map.Entry<Integer, int[]> entry : Main.connectToAndQueryDatabase().entrySet())
  46. {
  47. c.addTank(entry.getValue()[0], entry.getValue()[1]);
  48. }
  49. } catch (SQLException e) {
  50. // TODO Auto-generated catch block
  51. e.printStackTrace();
  52. } catch (InstantiationException e) {
  53. // TODO Auto-generated catch block
  54. e.printStackTrace();
  55. } catch (IllegalAccessException e) {
  56. // TODO Auto-generated catch block
  57. e.printStackTrace();
  58. } catch (ClassNotFoundException e) {
  59. // TODO Auto-generated catch block
  60. e.printStackTrace();
  61. }
  62.  
  63. System.out.println(c.getSumLevel());
  64. }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement