Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package daos;
- import model.Route;
- import model.RouteStage;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- public class SQLDAO extends DAO {
- static private String userName = "root";
- static private String password = "";
- static private String url = "jdbc:mysql://localhost:3306/routes";
- static private Connection connection = null;
- static private Statement stmt = null;
- static private ResultSet rs = null;
- public SQLDAO() {
- try{
- connection = DriverManager.getConnection(url,userName,password);
- System.out.println("Connected");
- stmt = connection.createStatement();
- }catch(SQLException e){
- System.out.println(e.getMessage());
- }
- }
- @Override
- public void addRoute(Route route) {
- }
- @Override
- public void addStage(RouteStage stage) {
- }
- @Override
- public Route getRoute(int routeId) {
- return null;
- }
- @Override
- public List<Route> getRoutes() {
- List<Route> routes = new ArrayList<>();
- try{
- String query = "CALL GetRoutes()";
- rs = stmt.executeQuery(query);
- while (rs.next()){
- System.out.println("First while");
- int id = rs.getInt("ROUTEID");
- String routeTitle = rs.getString("ROUTETITLE");
- String departurePoint = rs.getString("DEPARTUREPOINT");
- Route route = new Route(id,routeTitle,departurePoint);
- List<RouteStage> stages = new ArrayList<>();
- String query2 = "CALL GetRouteStages("+ id +")";
- Statement statement2 = connection.createStatement();
- ResultSet rs2 = statement2.executeQuery(query2);
- while (rs2.next()){
- System.out.println("Second while");
- int stageNumber = rs2.getInt("STAGENUMBER");
- String endPoint = rs2.getString("ENDPOINT");
- float stageLength = rs2.getFloat("STAGELENGTH");
- RouteStage stage = new RouteStage(stageNumber,endPoint,stageLength,id);
- stages.add(stage);
- }
- route.setRouteStages(stages);
- routes.add(route);
- }
- }catch(SQLException e){
- System.out.println("Faild to run the query");
- }
- return routes;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement