javaWeb项目jdbc+servlet+jsp+mysql人员管理系统

一、

所需:IDEA

Tmocat (没有可以官网下载)

javaWeb项目jdbc+servlet+jsp+mysql人员管理系统_第1张图片

 Mysql

javaWeb项目jdbc+servlet+jsp+mysql人员管理系统_第2张图片

JDBC

package com.wuyunlong.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

//操作数据库的公共类
public class BaseDao {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    //静态代码块,类加载的时候就初始化了
    static {
        Properties params=new Properties();

        InputStream is=BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            params.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        driver=params.getProperty("driver");
        url=params.getProperty("url");
        username=params.getProperty("username");
        password=params.getProperty("password");
    }

    //获取数据库的链接

    public static Connection getConnection(){
        Connection connection = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    //编写查询公共方法
    public static ResultSet execute(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet,String sql,Object[] params) throws SQLException {
        //预编译的sql,在后面直接执行就可以了
        preparedStatement = connection.prepareStatement(sql);

        for (int i = 0; i < params.length; i++) {
            //setObject,占位符从1开始,但是我们的数组是从0开始!
            preparedStatement.setObject(i+1,params[i]);
        }

        resultSet = preparedStatement.executeQuery();
        return resultSet;
    }


    //编写增删改公共方法
    public static int execute(Connection connection,PreparedStatement preparedStatement,String sql,Object[] params) throws SQLException {
        preparedStatement = connection.prepareStatement(sql);

        for (int i = 0; i < params.length; i++) {
            //setObject,占位符从1开始,但是我们的数组是从0开始!
            preparedStatement.setObject(i+1,params[i]);
        }

        int updateRows = preparedStatement.executeUpdate();
        return updateRows;
    }


    //释放资源
    public static boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){
        boolean flag = true;

        if (resultSet!=null){
            try {
                resultSet.close();
                //GC回收
                resultSet = null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }

        if (preparedStatement!=null){
            try {
                preparedStatement.close();
                //GC回收
                preparedStatement = null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }

        if (connection!=null){
            try {
                connection.close();
                //GC回收
                connection = null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }
        return flag;
    }


}

 配置jdbc.properties

javaWeb项目jdbc+servlet+jsp+mysql人员管理系统_第3张图片

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mdb1?useUnicode=true&characterEncoding=utf-8
username=root
password=admin

更改自己对应的:数据库 账户和密码

创建servlet(Web层) service(业务层) dao(持久层)bean(实体类) util(三方工具类存放JDBC)

javaWeb项目jdbc+servlet+jsp+mysql人员管理系统_第4张图片

创建bean层userBean表实体类

package com.wuyunlong.pojo;

public class User {
  private int uid;
  private String usercode;
  private String userpassword;
  private String age;
  private String address;
  private String gender;

  public User() {
  }

  public User(int uid, String usercode, String userpassword, String age, String address, String gender) {
    this.uid = uid;
    this.usercode = usercode;
    this.userpassword = userpassword;
    this.age = age;
    this.address = address;
    this.gender = gender;
  }

  public int getUid() {
    return uid;
  }

  public void setUid(int uid) {
    this.uid = uid;
  }

  public String getUsercode() {
    return usercode;
  }

  public void setUsercode(String usercode) {
    this.usercode = usercode;
  }

  public String getUserpassword() {
    return userpassword;
  }

  public void setUserpassword(String userpassword) {
    this.userpassword = userpassword;
  }

  public String getAge() {
    return age;
  }

  public void setAge(String age) {
    this.age = age;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }

  public String getGender() {
    return gender;
  }

  public void setGender(String gender) {
    this.gender = gender;
  }
}

 

servlet:登录请求

package com.wuyunlong.servlet;

import com.wuyunlong.pojo.User;
import com.wuyunlong.service.UserService;
import com.wuyunlong.service.UserServiceImpl;
import com.wuyunlong.util.Constants;
import org.junit.Test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userCode = req.getParameter("userCode");
        String userPassword = req.getParameter("userPassword");


        UserService userService = new UserServiceImpl();
        User user = userService.login(userCode, userPassword);//查询出登录的人
        List all= userService.findll();


        req.setAttribute("findAll",all);
        if(user!=null){  //查有此人可以登录

//将用户信息放到session中
            req.getSession().setAttribute(Constants.USER_SESSINO,user);
            //跳转页面

            this.findall(req,resp);

            //resp.sendRedirect("main.jsp");


        }else {
            req.setAttribute("error","用户名或者密码错误,请先注册");
            req.getRequestDispatcher("login.jsp").forward(req,resp);

        }




    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    protected void findall(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService us = new UserServiceImpl();
        List all = us.findll();

        req.setAttribute("all",all);
        //req.setAttribute("findAll",all);
        req.getRequestDispatcher("main.jsp").forward(req,resp);
    }


}

service

接口:UserService

 public User login(String userCode,String password );

实现类:UserServiceImpl

    private UserDao userDao;

    public UserServiceImpl() {

            userDao=new UserDaoImpl();
        }

            //登录
        public User login(String userCode, String userPassword) {
            Connection connection=null;
            User user=null;

            try {
                connection = BaseDao.getConnection();
                //通过业务层调用对应的具体的数据库操作
                user=   userDao.getLoginUser(connection,userCode,userPassword);
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                BaseDao.closeResource(connection,null,null);
            }

            return user;
        }

Dao

接口:UserServlet

public User getLoginUser(Connection connection, String userCode,String userPassword) throws SQLException;

实现类:UserServletImpl

//登陆查询
    public User getLoginUser(Connection connection, String userCode,String userPassword) throws SQLException {
        {


            PreparedStatement pstm = null;
            ResultSet rs = null;
            User user = null;

            if (connection != null) {
                String sql = "select * from user where usercode=? ";
                Object[] params = {userCode};

                rs = BaseDao.execute(connection, pstm, rs, sql, params);
                if (rs.next()) {
                    //通过usercode查询 返回整个数组
                    user = new User();
                    user.setUid(rs.getInt("uid"));
                  user.setUsercode(rs.getString("usercode"));
                  user.setUserpassword(rs.getString("userpassword"));
                  user.setAge(rs.getString("age"));
                  user.setAddress(rs.getString("address"));
                  user.setGender(rs.getString("gender"));

                  if (!user.getUserpassword().equals(userPassword)){
                        user=null;}

                }



                BaseDao.closeResource(null, pstm, rs);
            }
            return user;


        }
    }

注册Servlet

javaWeb项目jdbc+servlet+jsp+mysql人员管理系统_第5张图片

登陆页面:login.jsp

 javaWeb项目jdbc+servlet+jsp+mysql人员管理系统_第6张图片

登陆成功(主页面):main.jsp

编号 账户 密码 年龄 地址 性别 操作
${s.count} ${u.usercode} ${u.userpassword} ${u.age} ${u.address} ${u.gender} 修改 删除

 只上传了登录功能

废话不多说

javaWeb项目jdbc+servlet+jsp+mysql人员管理系统_第7张图片

 

你可能感兴趣的:(JDBC,Mysql,java,tomcat)