以JavaWeb实现员工信息的登记

萝らか妹 提交于 2020-01-06 14:45:52

1.

 

 

2.代码

Bean.java

package com.bean;

public class Bean {

    private String name;
    private String sex;
    private String nation;//民族
    private String time;
    private int age;
    private String kind;//政治面貌
    private String serve;//服务类别
    
    public Bean() {}
    public Bean(String name, String sex, String nation, String time, int age, String kind, String serve) {
        super();
        this.name = name;
        this.sex = sex;
        this.nation = nation;
        this.time = time;
        this.age = age;
        this.kind = kind;
        this.serve = serve;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getNation() {
        return nation;
    }
    public void setNation(String nation) {
        this.nation = nation;
    }
    public String getTime() {
        return time;
    }
    public void setTime(String time) {
        this.time = time;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getKind() {
        return kind;
    }
    public void setKind(String kind) {
        this.kind = kind;
    }
    public String getServe() {
        return serve;
    }
    public void setServe(String serve) {
        this.serve = serve;
    }
    
}

Dao.java

package com.dao;

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

import com.db.DB;
import com.db.DB;
import com.bean.Bean;

public class Dao {
    //增加
        @SuppressWarnings("static-access")
        public boolean add(Bean bean) {
            String tablename = "test";
            DB db=new DB();
            Connection con = db.getCon();
            
            try {
                String sql="insert into "+tablename+"(name,sex,nation,time,age,kind,serve) values ('"+bean.getName()+"','"+bean.getSex()+"','"+bean.getNation()+"','"+bean.getTime()+"','"+bean.getAge()+"','"+bean.getKind()+"','"+bean.getServe()+"')";
                Statement stm = con.createStatement();
                System.out.println(sql);
                stm.execute(sql);
                db.close(stm, con);
            }catch(Exception e) {
                e.printStackTrace();
                System.out.println("add false");
                return false;
            }
            System.out.println("add true");
            return true;
        }
    //根据名称进行查询
    @SuppressWarnings("static-access")
    public static Bean selectName(String name) {
        String tablename = "test";
        System.out.println("select bean where name = "+name);
        Bean bean=null;
        DB db=new DB();
        Connection con = db.getCon();
        try {
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery("select * from "+tablename+" where name='" + name + "'");
            /*
             * ResultSet rs =stm.executeQuery("select value3 from "+tablename+" where value2="+i);
             */
            if(rs.next())
            {
                bean = new Bean();
                System.out.println("select the bean from mysql");
                bean.setName(rs.getString("name"));
                bean.setSex(rs.getString("sex"));
                bean.setNation(rs.getString("nation"));
                bean.setTime(rs.getString("time"));
                bean.setAge(Integer.parseInt(rs.getString("age")));
                bean.setKind(rs.getString("kind"));
                bean.setServe(rs.getString("serve"));
                //bean.setValue2(Integer.parseInt(rs.getString("teacher")));
                //bean.setValue3(Boolean.parseBoolean(rs.getString("address")));
                System.out.println("name of the bean is "+rs.getString("name"));
            }
            db.close(rs,stm, con);
        }catch(Exception e) {
            e.printStackTrace();
        }
        return bean;
    }
/*    //根据性别进行查询
        @SuppressWarnings("static-access")
        public static Bean selectSex(String sex) {
            String tablename = "test";
            System.out.println("select bean where name = "+sex);
            Bean bean=null;
            DB db=new DB();
            Connection con = db.getCon();
            try {
                Statement stm = con.createStatement();
                ResultSet rs = stm.executeQuery("select * from "+tablename+" where name='" + sex + "'");
                /*
                 * ResultSet rs =stm.executeQuery("select value3 from "+tablename+" where value2="+i);
                 */
            /*    if(rs.next())
                {
                    bean = new Bean();
                    System.out.println("select the bean from mysql");
                    bean.setName(rs.getString("name"));
                    bean.setSex(rs.getString("sex"));
                    bean.setNation(rs.getString("nation"));
                    bean.setTime(rs.getString("time"));
                    bean.setAge(Integer.parseInt(rs.getString("age")));
                    bean.setKind(rs.getString("kind"));
                    bean.setServe(rs.getString("serve"));
                    //bean.setValue2(Integer.parseInt(rs.getString("teacher")));
                    //bean.setValue3(Boolean.parseBoolean(rs.getString("address")));
                    System.out.println("name of the bean is "+rs.getString("name"));
                }
                db.close(rs,stm, con);
            }catch(Exception e) {
                e.printStackTrace();
            }
            return bean;
        }
    */
    //删除
    @SuppressWarnings("static-access")
    public static boolean delete(String name)
    {
        //String tablename ="test";
        DB db=new DB();
        Connection con = db.getCon();
        //Connection con=null;
        PreparedStatement stm=null;
        try {
            con=DB.getCon();
            String sql="delete from test where name='"+name+"'";
            //String sql="delete from test where name='"+bean.getName()+"'";
            System.out.println(sql);
            System.out.println(name);
            stm=con.prepareStatement(sql);
            stm.executeUpdate();
            return true;
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally {
            db.close(stm, con);
        }
        return false;
    }
   //修改
    @SuppressWarnings("static-access")
    public boolean update(Bean bean) {
        Connection con=null;
        PreparedStatement stm=null;
        String tablename="test";
        DB db=new DB();
        try {
            con=DB.getCon();
            String sql="update "+tablename+" set sex=?,nation=?,time=?,age=?,kind=?,serve=? where name=?";
            //String sql="update "+tablename+" set teacher=?,classroom=? where name=?";
            //String sql="update "+tablename+"set teacher=?,classroom=? where name=?";
            stm=con.prepareStatement(sql);
            stm.setString(1,bean.getSex());
            stm.setString(2,bean.getNation());
            stm.setString(3,bean.getTime());
            stm.setInt(4,bean.getAge());
            stm.setString(5,bean.getKind());
            stm.setString(6,bean.getServe());
            stm.setString(7,bean.getName());
            
            stm.executeUpdate();
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            return false;
        }
        finally {
            db.close(stm, con);
            return true;
        }
    }
  //根据名字进行查找
     public  boolean findName(String name){
            boolean flag=false;
            Connection con=null;
            PreparedStatement psts=null;
            ResultSet rs=null;
            String tablename="test";
            DB db=new DB();
            try {
                con=db.getCon();
                String sql="select * from"+tablename+" where name=?";
                psts=con.prepareStatement(sql);
                psts.setString(1, name);
                rs=psts.executeQuery();
                while(rs.next()){
                    flag=true;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            
            return flag;
     }
/*    //根据性别进行查找
         public  boolean findSex(String sex){
                boolean flag=false;
                Connection con=null;
                PreparedStatement psts=null;
                ResultSet rs=null;
                String tablename="test";
                DB db=new DB();
                try {
                    con=db.getCon();
                    String sql="select * from"+tablename+" where name=?";
                    psts=con.prepareStatement(sql);
                    psts.setString(1, sex);
                    rs=psts.executeQuery();
                    while(rs.next()){
                        flag=true;
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                
                return flag;
         }*/
    //
    @SuppressWarnings("static-access")
     public ArrayList<Bean> selectList(){
        Connection con=null;
        PreparedStatement stm=null;
        ResultSet rs=null;
        ArrayList<Bean> listbean=new ArrayList<Bean>();
        String tablename="test";
        DB db=new DB();
        try {
            con=db.getCon();
            String sql="select * from "+tablename;
            stm=con.prepareStatement(sql);
            rs=stm.executeQuery();
            while(rs.next()){
            
                String name=rs.getString("name");
                String sex=rs.getString("sex");
                String nation=rs.getString("nation");
                String time=rs.getString("time");
                int age=rs.getInt("age");
                String kind=rs.getString("kind");
                String serve=rs.getString("serve");
//                String teacher=rs.getString("teacher");
//                String classroom=rs.getString("classroom");
                //Bean bean=new Bean(name,teacher,classroom);
                Bean bean=new Bean(name,sex,nation,time,age,kind,serve);
                listbean.add(bean);
                
            }
        } 
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            db.close(stm, con);
        }
        return listbean;
    }

}

DB。java

package com.db;

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

import org.junit.Test;



public class DB {
    private static String mysqlname = "localhost_3306";//库名
    private static Connection con;
    private static Statement stm;
    private static ResultSet rs;
    private static String coursename = "com.mysql.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GB18030&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true";
    
    public static Connection getCon() {
        try {
            Class.forName(coursename);
            System.out.println("注册驱动成功");
        }catch(ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            con = DriverManager.getConnection(url,"root","1234");
            System.out.println("建立连接成功");
        }catch(Exception e){
            e.printStackTrace();
            con = null;
        }
        return con;
    }
    
    public static void close(Statement stm,Connection connection) {
        if(stm!=null) {
            try {
                stm.close();
            }catch(SQLException e) {
                e.printStackTrace();
            }
        }
        if(connection!=null) {
            try {
                connection.close();
            }catch(SQLException e) {
                e.printStackTrace();
            }
        }
    }
        
    
    public static void close(ResultSet rs,Statement stm,Connection connection) {
        if(rs!=null) {
            try {
                rs.close();
            }catch(SQLException e) {
            e.printStackTrace();
            }
        }
        if(stm!=null) {
            try {
                stm.close();
            }catch(SQLException e) {
                e.printStackTrace();
            }
        }
        if(connection!=null) {
            try {
                connection.close();
            }catch(SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args)
    {
        getCon();
    }
}

Servlet.java

package com.servlet;

import java.io.IOException;
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.bean.Bean;
import com.dao.Dao;

/**
 * Servlet implementation class addServlet
 */
@WebServlet("/addServlet")
public class addServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("UTF-8");
        
        Bean bean = new Bean();
        bean.setName(request.getParameter("name"));
        bean.setSex(request.getParameter("sex"));
        bean.setNation(request.getParameter("nation"));
        bean.setTime(request.getParameter("time"));
        bean.setAge(Integer.parseInt(request.getParameter("age")));
        bean.setKind(request.getParameter("kind"));
        bean.setServe(request.getParameter("serve"));
        Dao dao = new Dao();
        if(dao.add(bean)) {
            
            request.setAttribute("message", "添加成功");
            request.getRequestDispatcher("add.jsp").forward(request,response);
            //response.sendRedirect("index.jsp");
        }
        else {
            request.setAttribute("message","添加失败");
            request.getRequestDispatcher("add.jsp").forward(request,response);
            //response.sendRedirect("add.jsp");
        }
    }

    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        doGet(request, response);
    }

}

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!