Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.HashMap;
- import java.util.Map;
- public class Main {
- public static HashMap<Integer, int[]> connectToAndQueryDatabase() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
- HashMap map = new HashMap<Integer, int[]>();
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- String connectionCommand = "jdbc:mysql://"+"localhost"+"/"+"tank" + "?user=root";
- Connection con = DriverManager.getConnection(connectionCommand);
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT id, level, max FROM tbl_tank");
- while (rs.next()) {
- int x = rs.getInt("id");
- int s = rs.getInt("level");
- int f = rs.getInt("max");
- int[] array = new int[2];
- array[0] = s;
- array[1] = f;
- map.put(x, array);
- }
- return map;
- }
- public static void main(String[] args) {
- Controller c = new Controller();
- try {
- for (Map.Entry<Integer, int[]> entry : Main.connectToAndQueryDatabase().entrySet())
- {
- c.addTank(entry.getValue()[0], entry.getValue()[1]);
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InstantiationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println(c.getSumLevel());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement