zoukankan      html  css  js  c++  java
  • javaweb基本增删改查实现

    package dao;
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import entry.user;
    import util.DBUtil;
    
    public class userDao {
        //
            public boolean add(user user) {
                String sql = "insert into user2(id,name,password,sex,no,mail,address,class1,yuan,sheng,bei) "
                        + "values('" + user.getId() + "','"  + user.getName() + "','"  + user.getPassword() + "','"  + user.getSex() + "','"  + user.getNo() + "','"  + user.getMail() + "','"  + user.getAddress() + user.getClass1() + "','"  + user.getYuan() +  "','"  + user.getSheng()+ "','"  + user.getBei()+"','"+"')";
                //
                Connection conn = DBUtil.getConn();
                Statement state = null;
                boolean f = false;
                int a = 0;
                
                try {
                    state = conn.createStatement();
                    state.executeUpdate(sql);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    //
                    DBUtil.close(state, conn);
                }
                
                if (a > 0) {
                    f = true;
                }
                return f;
            }
            
            
            
            public boolean id(String id) {
                boolean f = false;
                String sql = "select id from user2 where name = '" + id + "'";
                //
                Connection conn = DBUtil.getConn();
                Statement state = null;
                ResultSet rs = null;       
                try {
                    state = conn.createStatement();
                    rs = state.executeQuery(sql);
                    while (rs.next()) {
                        f = true;
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    DBUtil.close(rs, state, conn);
                }
                return f;
            }
    }
    package entry;
    
    public class user {
    private String id;
    private String password;
    private String name;
    private String sex;
    private String address;
    private String no;
    private String mail;
    private String class1;
    private String yuan;
    private String sheng;
    private String bei;
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getClass1() {
        return class1;
    }
    public void setClass1(String class1) {
        this.class1 = class1;
    }
    public String getYuan() {
        return yuan;
    }
    public void setYuan(String yuan) {
        this.yuan = yuan;
    }
    public String getSheng() {
        return sheng;
    }
    public void setSheng(String sheng) {
        this.sheng = sheng;
    }
    public String getBei() {
        return bei;
    }
    public void setBei(String bei) {
        this.bei = bei;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getNo() {
        return no;
    }
    public void setNo(String no) {
        this.no = no;
    }
    public String getMail() {
        return mail;
    }
    public void setMail(String mail) {
        this.mail = mail;
    }
    public user(){}
    public user(String name, String password) {
        this.name = name;
        this.password = password;
    };
    public user(String id, String password, String name, String sex, String address, String no, String mail,String class1,String yuan,String sheng,String bei) {
        super();
        this.id = id;
        this.password = password;
        this.name = name;
        this.sex = sex;
        this.address = address;
        this.no = no;
        this.mail = mail;
        this.class1 = class1;
        this.yuan = yuan;
        this.sheng = sheng;
        this.bei = bei;
    }
    }
    
    package service;
    
    import dao.userDao;
    import entry.user;
    
    public class userservice {
    userDao cDao = new userDao();
        
        /*添加*/
        public boolean add(user user) {
            boolean f = false;
            if(!cDao.id(user.getId())) {
                cDao.add(user);
                f = true;
            }
            return f;
        }
        
        
        
    }
    package 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 entry.user;
    import service.userservice;
    
    /**
     * Servlet implementation class AddServlet
     */
    @WebServlet("/AddServlet")
    public class AddServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public AddServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
        
            
            userservice service = new userservice();    
            protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                req.setCharacterEncoding("utf-8");
                String method = req.getParameter("method");
                if("add".equals(method))
                 add(req,resp);
            
            }
            
            
            protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                 request.setCharacterEncoding("utf-8");
                    //鑾峰彇鏁版嵁
                    String id = request.getParameter("id");
                    String password = request.getParameter("password");
                    String name = request.getParameter("name");
                    String sex = request.getParameter("sex");
                    String address = request.getParameter("address");
                    String no = request.getParameter("no");
                    String mail = request.getParameter("mail");
                    String class1 = request.getParameter("class1");
                    String yuan = request.getParameter("yuan");
                    String sheng = request.getParameter("sheng");
                    String bei = request.getParameter("bei");
                    user user = new user(id,password,name,sex,address,no,mail,class1,yuan,sheng,bei);
                     System.out.println(id);
                    //娣诲姞鍚庢秷鎭樉绀�
                    if(service.add(user)) {
                        request.setAttribute("message", "添加成功");
                        request.getRequestDispatcher("NewFile.jsp").forward(request,response);
                    } else {
                        request.setAttribute("message", "添加失败");
                        request.getRequestDispatcher("NewFile.jsp").forward(request,response);
                    }
            }
    
            protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
                doGet(request, response);
            }
    
        
    
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            response.getWriter().append("Served at: ").append(request.getContextPath());
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
    
    
    }
    package util;
     
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
     
    /*
     * 鏁版嵁搴撹繛鎺ュ伐鍏�
     */
    public class DBUtil {
         
        public static String db_url = "jdbc:mysql://localhost:3306/user?useSSL=false";
        public static String db_user = "root";
        public static String db_pass = "root";
         
        public static Connection getConn () {
            Connection conn = null;
             
            try {
                Class.forName("com.mysql.jdbc.Driver");//鍔犺浇椹卞姩
                conn = DriverManager.getConnection(db_url, db_user, db_pass);
            } catch (Exception e) {
                e.printStackTrace();
            }
             
            return conn;
        }
         
        /*10鍏抽棴杩炴帴*/
        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 pstmt = null;
            ResultSet rs = null;
            String sql ="select * from user";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            if(rs.next()){
                System.out.println("绌�");
            }else{
                System.out.println("涓嶇┖");
            }
        }
    }
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <title>Insert title here</title>
    <meta charset="UTF-8">
        <title>注册页面</title>
    </head>
    <body>
        <%
             Object message = request.getAttribute("message");
             if(message!=null && !"".equals(message)){
         
        %>
             <script type="text/javascript">
                  alert("<%=request.getAttribute("message")%>");
             </script>
        <%} %>
        <div align="center">
            <h6 style="color: black;">当前位置:添加学生信息</h6>
         
            <form name = "form1" action="${pageContext.request.contextPath}/AddServlet?method=add" method="post" onsubmit="return check_submit()">
        <table>
                <tr>
                    <td>登陆账号</td>
                    <td colspan="2"> <input type="text" id="id" name="id"  maxlength="12" onblur="blur_id()" onfocus="focus_id()"/></td>
                    <td width="300"><div id="result"></td>
                </tr>
                <tr>
                    <td>登陆密码</td>
                    <td colspan="2"> <input type="password" id="password" name="password" onblur="blur_pass()" onfocus="focus_pass()" /></td>
                    <td width="300"><div id="result1"></td>
                </tr>
                <tr>
                    <td>姓名</td>
                    <td colspan="2"><input type="text" id="name" name="name" /></td>
                </tr>
                <tr>
                    <td>性别:</td>
                    <td colspan="2">
                  <select name="sex"id="sex" >
                  <option value="男" ></option>
                  <option value="女" ></option>
                  
                </select></td>
                </tr><tr>
                    <td>入学年份:</td>
                    <td colspan="2">
                  <select name="address"id="address" >
                  <option value="1998" >1998</option>
                  <option value="1999" >1999</option>
                  <option value="2000" >2000</option>
                </select></td>
                </tr><tr>
                    <td>学号:2018</td>
                    <td colspan="2"><input  maxlength="4" type="tel" id="no" name="no" /></td>
                     <td width="300"><div id="result_name"></td>
                </tr><tr>
                    <td>邮箱</td>
                    <td colspan="2"><input type="email" id="mail" name="mail" ></td>
                </tr>
                <tr>
                    <td>所在班级</td>
                    <td colspan="2"><input type="text" id="class1" name="class1" /></td>
                     
                </tr>
                <tr>
                    <td>所在院校</td>
                    <td colspan="2"><input type="text" id="yuan" name="yuan" /></td>
                     
                </tr>
                <tr>
                    <td>生源地</td>
                    <td colspan="2"><input type="text" id="sheng" name="sheng" /></td>
                     
                </tr>
                <tr>
                    <td>备注</td>
                    <td colspan="2"><input type="text" id="bei" name="bei" /></td>
                     
                </tr>
                <tr>
                <td colspan="3"> <button type="submit" class="b">&nbsp;&nbsp;&nbsp;</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <button type="reset" class="b">&nbsp;&nbsp;&nbsp;</button></td>
                
                </tr>
        </table>
        </form>
        </div>
        <script type="text/javascript">
    /*
        表单验证
    */
    var flag = false;   // flag 如果为true(即用户名合法)就允许表单提交, 如果为false(即用户名不合法)阻止提交
    function focus_pass()
    {
        var nameObj = document.getElementById("result1");
        nameObj.innerHTML = "由六位字符和数字组成";
        nameObj.style.color="#999";
        }
    function blur_pass()
    {
        var nameObj = document.getElementById("result1");
        // 判断用户名是否合法
        var str2 = check_user_pass(document.form1.password.value);
        nameObj.style.color="red";
        if ("密码合法" ==  str2)
        {
            flag = true;
            nameObj.innerHTML = str2;
        }
        else
        {
            nameObj.innerHTML = str2;
        }
    }
    
    function check_user_pass(str)
    {  var str2 = "密码合法";
    if ("" == str)
    {
        str2 = "密码为空";
        return str2;
    }
    else if (str.length!=6)
    {
        str2 = "用户名应是六位组成";
        return str2;
    }
    else if (!check_word(str))
    {
        str2 = "未含有英文字符";
        return str2;
    }
    
    return str2;
        
        
        }
    
    
    
    
    
    function focus_id()
    {
        var nameObj = document.getElementById("result");
        nameObj.innerHTML = "由六到十二英文字符和数字组成";
        nameObj.style.color="#999";
        }
    function blur_id()
    {
        var nameObj = document.getElementById("result");
        // 判断用户名是否合法
        var str2 = check_user_id(document.form1.id.value);
        nameObj.style.color="red";
        if ("用户名合法" ==  str2)
        {
            flag = true;
            nameObj.innerHTML = str2;
        }
        else
        {
            nameObj.innerHTML = str2;
        }
    }
    
    function check_user_id(str)
    {
        var str2 = "用户名合法";
        if ("" == str)
        {
            str2 = "用户名号为空";
            return str2;
        }
        else if ((str.length<=4)||(str.length>=12))
        {
            str2 = "用户名应是六到十二位组成";
            return str2;
        }
        else if (!check_word(str))
        {
            str2 = "未含有英文字符";
            return str2;
        }
        else if(!check_firstword(str))
        {
            str2 = "必须以英文字母开头";
            return str2;
        }
        return str2;
    }
    
    
    function check_firstword(str)
    {   var arr = ["a", "b", "c", "d", "e", "f", "g", "h","i","j", "k", "l", "m", "n", "o", "p", "q","r", "s", "t", "u", "v", "w", "x", "y","z","A", "B", "C", "D", "E", "F", "G", "H","I","J", "K", "L", "M", "N", "O", "P", "Q","R","S", "T", "U", "V", "W", "X", "Y", "Z"];
    for (var i = 0; i < arr.length; i++)
    {
            if (arr[i] == str.charAt(0))
            {
                return true;
            }
    }   
    return false;
        }
    
    
    function check_word(str)
    {   var arr = ["a", "b", "c", "d", "e", "f", "g", "h","i","j", "k", "l", "m", "n", "o", "p", "q","r", "s", "t", "u", "v", "w", "x", "y","z","A", "B", "C", "D", "E", "F", "G", "H","I","J", "K", "L", "M", "N", "O", "P", "Q","R","S", "T", "U", "V", "W", "X", "Y", "Z"];
    for (var i = 0; i < arr.length; i++)
    {
        for (var j = 0; j < str.length; j++)
        {
            if (arr[i] == str.charAt(j))
            {
                return true;
            }
        }
    }   
    return false;
        }
    
    // 验证用户名是否含有特殊字符
    function check_other_char(str)
    {
        var arr = ["&", "\\", "/", "*", ">", "<", "@", "!"];
        for (var i = 0; i < arr.length; i++)
        {
            for (var j = 0; j < str.length; j++)
            {
                if (arr[i] == str.charAt(j))
                {
                    return true;
                }
            }
        }   
        return false;
    }
    // 根据验证结果确认是否提交
    function check_submit()
    {
        if (flag == false)
        {
            return false;
        }
        return true;
    }
    </script>
    </head>
    </body>
  • 相关阅读:
    hdu 4578 线段树 ****
    hdu 4576 概率dp **
    hdu 4622 **
    vue中保存和获取cookie,读写cookie以及设置有效时间等,使用js-cookie
    go语言 strconv.ParseInt 的例子
    【golang】unsafe.Sizeof浅析
    Golang 漫谈之channel妙法
    总结了才知道,原来channel有这么多用法!
    字符集之在UTF-8中,一个汉字为什么需要三个字节?
    什么是Bitmap
  • 原文地址:https://www.cnblogs.com/520520520zl/p/12151614.html
Copyright © 2011-2022 走看看