自己在学习的第一个小项目


  1 package cn.kgc.dao;
  2 
  3 import cn.kgc.util.DBUtil;
  4 
  5 import java.sql.Connection;
  6 import java.sql.PreparedStatement;
  7 import java.sql.ResultSet;
  8 import java.sql.SQLException;
  9 
 10 
 11 public class BaseDao {
 12     protected Connection conn =null;
 13     protected PreparedStatement pstmt = null;
 14 
 15     public void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs) {
 16         // 若结果集对象不为空,则关闭
 17         if (rs != null) {
 18             try {
 19                 rs.close();
 20             } catch (Exception e) {
 21                 e.printStackTrace();
 22             }
 23         }
 24         // 若Statement对象不为空,则关闭
 25         if (pstmt != null) {
 26             try {
 27                 pstmt.close();
 28             } catch (Exception e) {
 29                 e.printStackTrace();
 30             }
 31         }
 32         // 若数据库连接对象不为空,则关闭
 33         if (conn != null) {
 34             try {
 35                 conn.close();
 36             } catch (Exception e) {
 37                 e.printStackTrace();
 38             }
 39         }
 40     }
 41     /**
 42      * 增、删、改操作
 43      * @param param sql语句
 44      * @param preparedSql 参数数组
 45      * @return 执行结果
 46      */
 47     public int exceuteUpdate(String preparedSql, Object... param) {
 48         PreparedStatement pstmt = null;
 49         int num = 0;
 50         try {
 51             conn = DBUtil.getConnection();
 52             pstmt = conn.prepareStatement(preparedSql);
 53             if (param != null) {
 54                 for (int i = 0; i < param.length; i++) {
 55                     // 为预编译sql设置参数
 56                     pstmt.setObject(i + 1, param[i]);
 57                 }
 58             }
 59             num = pstmt.executeUpdate();
 60             //lxl
 61             //System.out.println(pstmt.toString().substring(pstmt.toString().lastIndexOf(":")+2));
 62         } catch (SQLException e) {
 63             e.printStackTrace();
 64         } finally {
 65             closeAll(conn, pstmt, null);
 66         }
 67         return num;
 68     }
 69     
 70     public ResultSet executeQuery(String sql, Object... params) {
 71         ResultSet rs = null;
 72         try {
 73             conn = DBUtil.getConnection();
 74             pstmt = conn.prepareStatement(sql);
 75             for (int i = 0; i < params.length; i++) {
 76                 pstmt.setObject(i + 1, params[i]);
 77             }
 78             rs = pstmt.executeQuery();
 79             //lxl
 80             //System.out.println(pstmt.toString().substring(pstmt.toString().lastIndexOf(":")+2));
 81 
 82         } catch (SQLException e) {
 83             e.printStackTrace();
 84         }
 85         return rs;
 86     }
 87 
 88     public static void main(String[] args) throws Exception {
 89 //        DBUtil dbUtil = new DBUtil();
 90 //        Connection conn =dbUtil.getConnection();
 91 //        if (conn!=null) {
 92 //            System.out.println("连接数据库成功!");
 93 //        }
 94 
 95         BaseDao baseDao = new BaseDao();
 96         ResultSet rs =baseDao.executeQuery("select * from t_admin where adminId = ?",1);
 97         while(rs.next()){
 98             System.out.println(rs.getString("userName"));
 99 
100         }
101     }
102 }
baseDao
package cn.kgc.dao;

import java.util.List;

import cn.kgc.entity.studentLogin;

public interface StudentDao {
    public List<studentLogin> studentList();
}
StudentDao
package cn.kgc.dao;

import cn.kgc.entity.studentLogin;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class studentDaoImpl extends BaseDao implements StudentDao {

    @Override
    public List<studentLogin> studentList() {
        String sql = "SELECT t1.*,t2.`dormBuildName`,t3.`dormName` FROM t_student t1,`t_dormbuild` t2,`t_dorm` t3 where t1.`dormId` = t3.`dormId` AND t3.`dormBuildId` = t2.`dormBuildId`";
        ResultSet resultSet = this.executeQuery(sql);
        List<studentLogin> studentLogins = new ArrayList<studentLogin>();
        try {
            while (resultSet.next()) {
                studentLogin studentLogin = new studentLogin();
                studentLogin.setStudentId(resultSet.getString("studentId"));
                studentLogin.setStuName(resultSet.getString("stuName"));
                studentLogin.setPassword(resultSet.getString("password"));
                studentLogin.setDormId(resultSet.getString("dormId"));
                studentLogin.setSex(resultSet.getString("sex"));
                studentLogin.setTel(resultSet.getString("tel"));
                studentLogin.setDormBuildName(resultSet.getString("dormBuildName"));
                studentLogin.setDormName(resultSet.getString("dormName"));
                studentLogins.add(studentLogin);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return studentLogins;
    }

    public static void main(String[] args) {
        StudentDao studentDao = new studentDaoImpl();
        List<studentLogin> studentLogins = new ArrayList<>();
        studentLogins = studentDao.studentList();
        for (studentLogin studentLogin : studentLogins) {
            System.out.println(studentLogin.getDormName());
        }
    }
}
studentDaoImpl
package cn.kgc.entity;

public class studentLogin {
   String studentId;
   String stuName;
   String password;
   String dormId;
   String sex;
   String tel;
   String dormBuildName;
   String dormName;

    public studentLogin() {
    }

    public studentLogin(String studentId, String stuName, String password, String dormId, String sex, String tel, String dormBuildName, String dormName) {
        this.studentId = studentId;
        this.stuName = stuName;
        this.password = password;
        this.dormId = dormId;
        this.sex = sex;
        this.tel = tel;
        this.dormBuildName = dormBuildName;
        this.dormName = dormName;
    }

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDormId() {
        return dormId;
    }

    public void setDormId(String dormId) {
        this.dormId = dormId;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getDormBuildName() {
        return dormBuildName;
    }

    public void setDormBuildName(String dormBuildName) {
        this.dormBuildName = dormBuildName;
    }

    public String getDormName() {
        return dormName;
    }

    public void setDormName(String dormName) {
        this.dormName = dormName;
    }
}
studentLogin
package cn.kgc.service;

import cn.kgc.entity.studentLogin;

import java.util.List;

public interface StudentService {
    public List<studentLogin> studentList();
}
StudentService
package cn.kgc.service;

import cn.kgc.dao.StudentDao;
import cn.kgc.dao.studentDaoImpl;
import cn.kgc.entity.studentLogin;

import java.util.List;

public class StudentServiceImpl implements StudentService {
    StudentDao studentDao =new studentDaoImpl();
    @Override
    public List<studentLogin> studentList() {
        return studentDao.studentList();
    }
}
StudentServiceImpl
package cn.kgc.servlet;

import cn.kgc.dao.StudentDao;
import cn.kgc.dao.studentDaoImpl;
import cn.kgc.entity.studentLogin;

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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/student")
public class studentServlet extends HttpServlet {
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StudentDao studentDao = new studentDaoImpl();
        List<studentLogin> studentLogins = studentDao.studentList();
        request.setAttribute("studentList",studentLogins);
        request.setAttribute("mainPage","admin/student.jsp");
        request.getRequestDispatcher("mainAdmin.jsp").forward(request,response);
    }
}
studentServlet
posted @ 2020-01-07 16:42  战神_风  阅读(126)  评论(0)    收藏  举报