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