Advertisement
Guest User

Untitled

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