地铁系统代码

按条件查询代码

package Dao;

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

import DBUtil.DBUtil;
import Javabean.*;

public class Dao {

    
    
     public List<Sub> select(String xianlu) {
         List<Sub> list=new ArrayList<Sub>(); 
            Connection con;
            try {
                String sql="select * from line where lineid=?";
                con = DBUtil.getConn();
                PreparedStatement pa=con.prepareStatement(sql);
                pa.setString(1,xianlu );
                ResultSet rs=pa.executeQuery();
                while(rs.next()) {
                     Sub L = new Sub();
                     int id = rs.getInt("id");
                     String lineid = rs.getString("lineid");
                     String stationname = rs.getString("stationname");
                     String ordnum = rs.getString("ordnum");
                     String tinformation = rs.getString("tinformation");
                    L=new Sub(id,lineid, stationname, ordnum ,tinformation);
                    list.add(L);
                    
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return list;
        }
     
     public List<Sub> select1(String zhandian) {
         List<Sub> list=new ArrayList<Sub>(); 
            Connection con;
            try {
                String sql="select * from line where stationname=?";
                con = DBUtil.getConn();
                PreparedStatement pa=con.prepareStatement(sql);
                pa.setString(1,zhandian );
                ResultSet rs=pa.executeQuery();
                while(rs.next()) {
                     Sub L = new Sub();
                     int id = rs.getInt("id");
                     String lineid = rs.getString("lineid");
                     String stationname = rs.getString("stationname");
                     String ordnum = rs.getString("ordnum");
                     String tinformation = rs.getString("tinformation");
                    L=new Sub(id,lineid, stationname, ordnum ,tinformation);
                    list.add(L);
                    
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return list;
        }
    
}

DBUtil

package DBUtil;

import java.sql.*;

/**
 * 数据库连接工具
 * @author Hu
 *
 */
public class DBUtil {
    
    public static String url =  "jdbc:mysql://localhost:3306/subway?serverTimezone=UTC";
    public static String user = "root";
    public static String password = "123456";
    
    public static Connection getConn () {
        Connection conn = null;
        
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动
            conn = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return conn;
    }
    
    /**
     * 关闭连接
     * @param state
     * @param conn
     */
    public static void close (PreparedStatement preparedState, Connection conn) {
        if (preparedState != null) {
            try {
                preparedState.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void close (ResultSet rs, PreparedStatement preparedState, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (preparedState != null) {
            try {
                preparedState.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭连接
     * @param state
     * @param conn
     */
    public static void close (Statement 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, Statement state, Connection conn) {
        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) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void main(String[] args) throws SQLException {
        Connection conn = getConn();
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        String sql ="select * from subway";
        preparedStatement = conn.prepareStatement(sql);
        rs = preparedStatement.executeQuery();
        if(rs.next()){
            System.out.println("数据库为空");
        }
        else{
            System.out.println("数据库不为空");
        }
    }
}

ditieservlet

package ditieServlet;

import java.io.IOException;
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 Dao.Dao;
import Javabean.*;


/**
 * Servlet implementation class ditieServlet
 */
@WebServlet("/ditieServlet")
public class ditieServlet extends HttpServlet {
    /**
     * 特有id号
     */
    private static final long serialVersionUID = 1L;
    Dao dao = new Dao();
    /**
     * 方法选择
     * @return 
     * @throws IOException 
     * @throws ServletException 
     */
    protected void service(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException
    {
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        if("show".equals(method)) {
            show(req,resp);
      
        }else if("show1".equals(method)) {
           show1(req,resp);
        }
    }
    
    private void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        String lineid=request.getParameter("lineid");
        
        List<Sub> list=dao.select(request.getParameter("xianlu"));
        
        request.setAttribute("list",list);
        request.getRequestDispatcher("show.jsp").forward(request, response);
    }
    
    private void show1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        String stationname=request.getParameter("stationname");
        
        List<Sub> list=dao.select1(request.getParameter("zhandian"));
        
        request.setAttribute("list",list);
        request.getRequestDispatcher("show1.jsp").forward(request, response);
    }
   
   

}
package Javabean;

public class Sub {

    private int id;
    private String lineid;
    private String stationname;
    private String tinformation;
    private String ordnum;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public void setLineid(String lineid){
        this.lineid = lineid;
    }
    public String getLineid() {
        return lineid;
    }    
    public void setStationname(String stationname){
        this.stationname = stationname;
    }
    public String getStationname() {
        return stationname;
    }    
    public void setTinformation(String tinformation){
        this.tinformation = tinformation;
    }
    public String getTinformation() {
        return tinformation;
    }    
    public void setOrdnum(String ordnum){
        this.ordnum = ordnum;
    }
    public String getOrdnum() {
        return ordnum;
    }
    
    public Sub() {}
    public Sub(int id,String lineid,String stationname,String ordnum,String tinformation) {
        this.id=id;
        this.lineid=lineid;
        this.stationname=stationname;
        this.ordnum=ordnum;
        this.tinformation=tinformation;
    }
    public Sub(String lineid,String stationname,String ordnum,String tinformation) {
        
        this.lineid=lineid;
        this.stationname=stationname;
        this.ordnum=ordnum;
        this.tinformation=tinformation;
    }
}

 

posted @ 2022-04-08 22:57  慢漫曼蔓  阅读(102)  评论(0)    收藏  举报