Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.servlet.ServletException;
- import javax.servlet.http.*;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.*;
- import java.util.*;
- class DBHandler {
- private String database = "";
- private Connection conn;
- private Graph graph;
- public DBHandler(String database) {
- this.database = database;
- }
- public void setConn(Connection conn) {
- this.conn = conn;
- }
- public Connection getConn() {
- return conn;
- }
- public void getConnection() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(database);
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- setConn(conn);
- }
- public Map retriveData() {
- Map citiesMap = new HashMap();
- String sql = "SELECT id,i,j FROM HData";
- Statement statement;
- try {
- statement = getConn().createStatement();
- ResultSet resultSet = statement.executeQuery(sql);
- while (resultSet.next()) {
- int id = resultSet.getInt("id");
- int i = resultSet.getInt("i");
- int j = resultSet.getInt("j");
- List listHighway = new ArrayList();
- listHighway.add(i);
- listHighway.add(j);
- citiesMap.put(id, listHighway);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return citiesMap;
- }
- public void closeConnection() {
- }
- }
- public class Highway extends HttpServlet {
- Connection conn;
- DBHandler dbHandler;
- Graph graph;
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- PrintWriter out = response.getWriter();
- String database = "jdbc:mysql://localhost:3306/dbo?user=root&password=root";
- dbHandler = new DBHandler(database);
- dbHandler.getConnection();
- graph = new Graph(dbHandler.retriveData());
- }
- }
- class Graph {
- Map map;
- Stac
- public Graph(Map map) {
- this.map = map;
- }
- public Map getMap() {
- return map;
- }
- public int computeHowManyHighways() {
- return getMap().size();
- }
- public int computeHowManyCities() {
- List list = (List) getMap().get(1);
- int max = (int) Collections.max(list);
- for (int i = 1; i < getMap().size() + 1; i++) {
- if ((int) Collections.max((List) getMap().get(i)) > max) {
- max = (int) Collections.max((List) getMap().get(i));
- }
- }
- return max;
- }
- public void calculateIfHighwaysCanBeBuild(){
- for (int i=0;i<getMap().size()+1;i++){
- List list = (List) getMap().get(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement