其他分享
首页 > 其他分享> > 2022/4/1

2022/4/1

作者:互联网

 

 

 

 地铁查询系统的实现:

现在我只做了站点查询和线路查询两个功能,其他的一些功能并没有完善,两个站点的最短路径是我这两天不知道如何解决的点。

 

 

package com.Dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class service {
        public static List<String> searchstop(int line_id) throws ClassNotFoundException,SQLException, IOException{
         List<String> stop= new ArrayList<>();
         Connection connection = mysql.getconn();
         PreparedStatement pstmt = null;
         ResultSet rs = null;
         try {
            String sql = "select stop_name from subway where line_id = ?";
            pstmt = connection.prepareStatement(sql);
            pstmt.setInt(1, line_id);
            rs = pstmt.executeQuery();
            while(rs.next()) {
                String name = rs.getString(1);
                stop.add(name);
            }
        }catch(SQLException e) {
            e.printStackTrace();
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            mysql.close(rs, pstmt, connection);
        }
         return stop;
    }
    
    public static List<String> searchline(String stop_name) throws ClassNotFoundException,SQLException, IOException{
         List<String> line= new ArrayList<>();
         Connection connection = mysql.getconn();
         PreparedStatement pstmt = null;
         ResultSet rs = null;
         try {
            String sql = "select line_name from subway where stop_name = ?";
            pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, stop_name);
            rs = pstmt.executeQuery();
            while(rs.next()) {
                String name = rs.getString(1);
                line.add(name);
            }
        }catch(SQLException e) {
            e.printStackTrace();
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            mysql.close(rs, pstmt, connection);
        }
         return line;
    }
}
package com.Dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class mysql {
    public static String url="jdbc:mysql://localhost:3306/subway";
    public static String user="root";
    public static String pass="yang";
    
    public static Connection getconn() {
        Connection conn=null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url,user,pass);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
    
    public static void close (PreparedStatement state, Connection conn) {
         if (state != null) {
             try {
                 state.close();
             } catch (SQLException e) {
                 e.printStackTrace();
             }
         }
         
         if (conn != null) {
             try {
                 conn.close();
             } catch (SQLException e) {
                 e.printStackTrace();
             }
         }
     }
     
     public static void close (ResultSet rs, PreparedStatement state,Connection conn) throws IOException, SQLException{
         if (rs != null) {
             try {
                 rs.close();
             } catch (SQLException e) {
                 e.printStackTrace();
             }
         }
         
         if (state != null) {
             try {
                 state.close();
             } catch (SQLException e) {
                 e.printStackTrace();
             }
         }
         
         if (conn != null) {
             conn.close();
         }
     }
}
ackage com.Servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Dao.service;

/**
 * Servlet implementation class selectLine
 */
@WebServlet("/selectLine")
public class selectLine extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String line_id = request.getParameter("line_id");
        response.setContentType("text/html;charset=UTF-8");
        List<String> list = new ArrayList<>();
        try {
            list = service.searchstop(Integer.parseInt(line_id));
            String a = null;
            for(int i=0;i<list.size();i++)
            {
                a = list.get(i);
                response.getWriter().println(a + " ");    
            }
        } catch (NumberFormatException | IOException | SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
package com.Servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Dao.service;

/**
 * Servlet implementation class selectStop
 */
@WebServlet("/selectStop")
public class selectStop extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String stop_name = request.getParameter("stop_name");
        response.setContentType("text/html;charset=UTF-8");
        List<String> list = new ArrayList<>();
        try {
            list = service.searchline(stop_name);
            String a = null;
            for(int i=0;i<list.size();i++)
            {
                a = list.get(i);
                response.getWriter().println(a + " ");    
            }
            
        } catch (NumberFormatException | IOException | SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

 

标签:java,name,rs,SQLException,2022,sql,import
来源: https://www.cnblogs.com/yx111/p/16088828.html