Advertisement
Styczen007

Untitled

Nov 2nd, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. import javax.servlet.ServletException;
  2. import javax.servlet.http.*;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.sql.*;
  6. import java.util.*;
  7.  
  8.  
  9.  
  10. class DBHandler {
  11.     private String database = "";
  12.     private Connection conn;
  13.     private Graph graph;
  14.  
  15.  
  16.     public DBHandler(String database) {
  17.         this.database = database;
  18.     }
  19.  
  20.     public void setConn(Connection conn) {
  21.         this.conn = conn;
  22.     }
  23.  
  24.     public Connection getConn() {
  25.         return conn;
  26.     }
  27.  
  28.     public void getConnection() {
  29.         try {
  30.             Class.forName("com.mysql.jdbc.Driver");
  31.             conn = DriverManager.getConnection(database);
  32.  
  33.         } catch (SQLException e) {
  34.             e.printStackTrace();
  35.  
  36.         } catch (ClassNotFoundException e) {
  37.             e.printStackTrace();
  38.         }
  39.  
  40.         setConn(conn);
  41.  
  42.     }
  43.  
  44.     public Map retriveData() {
  45.         Map citiesMap = new HashMap();
  46.  
  47.         String sql = "SELECT id,i,j FROM HData";
  48.         Statement statement;
  49.         try {
  50.             statement = getConn().createStatement();
  51.             ResultSet resultSet = statement.executeQuery(sql);
  52.             while (resultSet.next()) {
  53.                 int id = resultSet.getInt("id");
  54.                 int i = resultSet.getInt("i");
  55.                 int j = resultSet.getInt("j");
  56.                 List listHighway = new ArrayList();
  57.                 listHighway.add(i);
  58.                 listHighway.add(j);
  59.                 citiesMap.put(id, listHighway);
  60.  
  61.             }
  62.         } catch (SQLException e) {
  63.             e.printStackTrace();
  64.         }
  65.  
  66.         return citiesMap;
  67.     }
  68.  
  69.     public void closeConnection() {
  70.  
  71.     }
  72. }
  73.  
  74. public class Highway extends HttpServlet {
  75.     Connection conn;
  76.     DBHandler dbHandler;
  77.     Graph graph;
  78.  
  79.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  80.         PrintWriter out = response.getWriter();
  81.         String database = "jdbc:mysql://localhost:3306/dbo?user=root&password=root";
  82.         dbHandler = new DBHandler(database);
  83.         dbHandler.getConnection();
  84.         graph = new Graph(dbHandler.retriveData());
  85.     }
  86. }
  87.  
  88. class Graph {
  89.     Map map;
  90.     Stac
  91.     public Graph(Map map) {
  92.         this.map = map;
  93.     }
  94.  
  95.     public Map getMap() {
  96.         return map;
  97.     }
  98.  
  99.     public int computeHowManyHighways() {
  100.         return getMap().size();
  101.     }
  102.  
  103.     public int computeHowManyCities() {
  104.  
  105.         List list = (List) getMap().get(1);
  106.         int max = (int) Collections.max(list);
  107.  
  108.  
  109.         for (int i = 1; i < getMap().size() + 1; i++) {
  110.             if ((int) Collections.max((List) getMap().get(i)) > max) {
  111.                 max = (int) Collections.max((List) getMap().get(i));
  112.             }
  113.         }
  114.  
  115.         return max;
  116.  
  117.     }
  118.  
  119.     public void calculateIfHighwaysCanBeBuild(){
  120.         for (int i=0;i<getMap().size()+1;i++){
  121.             List list = (List) getMap().get(1);
  122.         }
  123.     }
  124.  
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement