Advertisement
MartinYanchev-99

Java - MySQL Connection

Dec 8th, 2020
1,054
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. package daos;
  2.  
  3. import model.Route;
  4. import model.RouteStage;
  5.  
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13.  
  14. public class SQLDAO extends DAO {
  15.     static private String userName = "root";
  16.     static private String password = "";
  17.     static private String url = "jdbc:mysql://localhost:3306/routes";
  18.     static private Connection connection = null;
  19.     static private Statement stmt = null;
  20.     static private ResultSet rs = null;
  21.  
  22.     public SQLDAO() {
  23.         try{
  24.  
  25.         connection = DriverManager.getConnection(url,userName,password);
  26.         System.out.println("Connected");
  27.         stmt = connection.createStatement();
  28.  
  29.          }catch(SQLException e){
  30.  
  31.             System.out.println(e.getMessage());
  32.             }
  33.  
  34.     }
  35.  
  36.     @Override
  37.     public void addRoute(Route route) {
  38.  
  39.     }
  40.  
  41.     @Override
  42.     public void addStage(RouteStage stage) {
  43.  
  44.     }
  45.  
  46.     @Override
  47.     public Route getRoute(int routeId) {
  48.         return null;
  49.     }
  50.  
  51.     @Override
  52.     public List<Route> getRoutes() {
  53.         List<Route> routes = new ArrayList<>();
  54.         try{
  55.             String query = "CALL GetRoutes()";
  56.             rs = stmt.executeQuery(query);
  57.             while (rs.next()){
  58.                 System.out.println("First while");
  59.                 int id = rs.getInt("ROUTEID");
  60.                 String routeTitle = rs.getString("ROUTETITLE");
  61.                 String departurePoint = rs.getString("DEPARTUREPOINT");
  62.                 Route route = new Route(id,routeTitle,departurePoint);
  63.  
  64.                 List<RouteStage> stages = new ArrayList<>();
  65.  
  66.                 String query2 = "CALL GetRouteStages("+ id +")";
  67.  
  68.                 Statement statement2 = connection.createStatement();
  69.                 ResultSet rs2 = statement2.executeQuery(query2);
  70.                 while (rs2.next()){
  71.                     System.out.println("Second while");
  72.  
  73.                     int stageNumber = rs2.getInt("STAGENUMBER");
  74.                     String endPoint = rs2.getString("ENDPOINT");
  75.                     float stageLength = rs2.getFloat("STAGELENGTH");
  76.                     RouteStage stage = new RouteStage(stageNumber,endPoint,stageLength,id);
  77.                     stages.add(stage);
  78.                 }
  79.                 route.setRouteStages(stages);
  80.                 routes.add(route);
  81.  
  82.             }
  83.         }catch(SQLException e){
  84.             System.out.println("Faild to run the query");
  85.         }
  86.     return routes;
  87.     }
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement