Java教程

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);
    }

}

 

这篇关于2022/4/1的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!