Advertisement
Guest User

Untitled

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