开学测试

Posted quyangzhangsiyuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了开学测试相关的知识,希望对你有一定的参考价值。

寒假里没咋学习,导致开学测试好多都不会没有完成。

连接数据库:

package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil{
    public static String db_url="jdbc:mysql://localhost:3306/eshop?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT";
    public static String db_user="root";
    public static String db_password="password";
    
    public static Connection getConn() {
        Connection conn=null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn=DriverManager.getConnection(db_url, db_user, db_password);
            System.out.println("连接数据库成功");
        }catch(Exception e) {
            System.out.println("连接数据库失败");
            e.printStackTrace();
        }
        return 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();
            }
        }
    }
}

 

Bean层

package Bean;

public class Bean{
    private int id;
    private String username;
    private String password;
    private String date;
    private String people;
    private String money;
    private String shijian;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public String getMoney() {
        return money;
    }
    public void setMoney(String money) {
        this.money = money;
    }
    public String getShijian() {
        return shijian;
    }
    public void setShijian(String shijian) {
        this.shijian = shijian;
    }
    public String getPeople() {
        return people;
    }
    public void setPeople(String people) {
        this.people = people;
    }

    
    
    
    
}

 

Dao层

package Dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import Bean.Bean;
import util.DBUtil;

public class Dao {

    public Bean checkLogin(String username, String password) {
        Connection conn = DBUtil.getConn();
        Bean bean = null;
        try {
            Statement state = conn.createStatement();
            ResultSet rs = state.executeQuery("select * from Money where username=‘" + username + "‘");
            if (rs.next()) {
                // 如果有结果,是认为是通过验证了
                
                if (rs.getString("password").equals(password)) {
                    bean = new Bean();
                    bean.setId(rs.getInt("id"));
                    bean.setUsername(rs.getString("username"));
                    bean.setPassword(rs.getString("password"));
                }
                DBUtil.close(state, conn);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bean;
    }
}

 

Servlet

package servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

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 javax.xml.ws.ResponseWrapper;

import org.apache.jasper.Constants;

import Bean.Bean;
import dao.Dao;
import util.DBUtil;


@WebServlet("/Servlet")
public class Login extends HttpServlet {

    @ResponseWrapper
    protected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
        req.setCharacterEncoding("utf-8");
        String method=req.getParameter("method");
        if("login".equals(method)) {
            try {
                login(req,resp);
            } catch (SQLException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }else if("end".equals(method)) {
            end(req,resp);
        }else if("zhuce".equals(method)) {
            try {
                zhuce(req,resp);
            } catch (SQLException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
    }
    @ResponseWrapper
    private void zhuce(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException {
        req.setCharacterEncoding("utf-8");
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        String phone=req.getParameter("phone");
        String danwei=req.getParameter("danwei");
        if(username==""||password==""||phone==""||danwei=="") {
            resp.sendRedirect(req.getContextPath() + "/admin/register.html?status=0");
        }else {
            Connection conn = DBUtil.getConn();
            Statement state = conn.createStatement();
            ResultSet rs = state.executeQuery("select * from login where username=‘" + username + "‘");
            if(rs.next()) {
                resp.sendRedirect(req.getContextPath() + "/admin/register.html?status=1");
            }else {
                Statement st = conn.createStatement();
                String sql="insert into login (username,password,phone,danwei) values(‘"+username+"‘,‘"+password+"‘,‘"+phone+"‘,‘"+danwei+"‘)";
                st.executeUpdate(sql);
                resp.sendRedirect(req.getContextPath() + "/admin/login.html");
                DBUtil.close(st, conn);
            }
        }
    }
    @ResponseWrapper
    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException {
        req.setCharacterEncoding("utf-8");
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        Dao dao=new Dao();
        Bean bean=dao.checkLogin(username, password);
        if(bean!=null) {
            req.getSession().setAttribute(Constants.SESSION_PARAMETER_NAME,bean);
            resp.sendRedirect(req.getContextPath() + "/admin/main.html");
        }else{
            Connection conn = DBUtil.getConn();
            Statement state = conn.createStatement();
            ResultSet rs = state.executeQuery("select * from login where username=‘" + username + "‘");
            if(!rs.next()) {
                resp.sendRedirect(req.getContextPath() + "/admin/login.html?status=1");
            }else if(rs.next()) {
                resp.sendRedirect(req.getContextPath() + "/admin/login.html?status=2");
            }
            
            }    
    }
    @ResponseWrapper
    private void end(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        String status = req.getParameter("status");
        if (status != null&&"1".equals(status)) {
            req.getSession().invalidate();
            resp.sendRedirect(req.getContextPath() + "/admin/login.html");
        }
    }
    
    
    
}

 

搞了半天还是没能把模板套上去,真的是脑阔痛

 

以上是关于开学测试的主要内容,如果未能解决你的问题,请参考以下文章

开学测试代码——需求征集系统

java开学第一周测试代码

Java开学测试

Java开学测试感想

java开学第一周测试自我感想

2017/2/24日开学测试整理