web添加学生信息(首发web)

程序思路,先在JSP上画好页面,然后再创建一Servlet文件用于判断在网页上操作是否正确,还需要与数据库相连接,用DBUtile文件连接数据库,用Dao层来实现数据的增加,用Service来服务于Dao层

其代码如下:

jsp页面代码:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page language="java" import="java.util.*"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>注册</title>

</head>
<%
    Object message=request.getAttribute("message");
    if(!(message==null||message.equals("")))
    {
        %>
        <script type="text/javascript">alert(‘<%=message%>‘)</script>
        <%
    }
%>

<body style="background:blue" align="center">
    <form action="LoginServlet?method=register" method="post">
        <p style="padding-left:400px;">登录账号:<input type="text" name="username" ></p>
        <p style="padding-left:400px;">密  码:&nbsp;&nbsp;&nbsp;<input type="password"  name="password"></p>
        <p style="padding-left:400px;">性  别:&nbsp;&nbsp;&nbsp;<select name=sex>
            <option value="男">男</option>
            <option value="女">女</option>
        </select></p>
        <p style="padding-left:400px;">姓  名:&nbsp;&nbsp;&nbsp;<input type="text" name="name" ></p>
        <p style="padding-left:400px;">学  号:&nbsp;&nbsp;&nbsp;<input type="text" name="num"></p>
        <p style="padding-left:400px;">电子邮件&nbsp;&nbsp;<input type="text" name="email"></p>
        <p style="padding-left:400px;">所在学院&nbsp;&nbsp;<input type="text" name="xueyuan"></p>
        <p style="padding-left:400px;">所在系&nbsp;&nbsp;<input type="text" name="xi"></p>
        <p style="padding-left:400px;">所在班级&nbsp;&nbsp;<input type="text" name="banji"></p>
        <p style="padding-left:400px;">入学年份:&nbsp;&nbsp;&nbsp;<select name=ruxue>
            <option value="2008">2008</option>
            <option value="2009">2009</option>
            <option value="2010">2010</option>
            <option value="2011">2011</option>
            <option value="2012">2012</option>
            <option value="2013">2013</option>
            <option value="2014">2014</option>
            <option value="2015">2015</option>
            <option value="2016">2016</option>
            <option value="2017">2017</option>
            <option value="2018">2018</option>
            <option value="2019">2019</option>
        </select></p>
        <p style="padding-left:400px;">生源地:<input type="text" name="dizhi">
        <p style="padding-left:400px;">备注:<input type="text" name="beizhu">
        <p style="padding-left:400px;"><input type="submit" value="注册"></p>
    </form>
</body>
</html>

Servletceng代码:

package zzz;

import java.io.IOException;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    public static boolean isMatchString(String str,String pattern)
    {
        boolean isMatch = Pattern.matches(pattern, str);
        return isMatch;
    }

    private static final long serialVersionUID = 1L;
    zhuceService fuwu=new zhuceService();
       /**
     * @see HttpServlet#HttpServlet()
     */

    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
        ZhuceDao haha=new ZhuceDao();
        // TODO 自动生成的方法存根
        arg0.setCharacterEncoding("utf-8");
        arg1.setCharacterEncoding("utf-8");
        //super.service(arg0, arg1);

        zhucexinxi sta=new zhucexinxi();
        String function=arg0.getParameter("method");

        if(function.equals("register"))
        {
            String str;
            str=arg0.getParameter("username");
            if(str.length()==0||str==null||str.equals(""))
            {
                arg0.setAttribute("message","账号不能为空");
                arg0.getRequestDispatcher("Login.jsp").forward(arg0,arg1);
                return;
            }

            if(str.charAt(0)>=‘a‘&&str.charAt(0)<=‘z‘||str.charAt(0)>=‘A‘&&str.charAt(0)<=‘Z‘);
            else
            {
                arg0.setAttribute("message","账号第一位需要是字母");
                arg0.getRequestDispatcher("Login.jsp").forward(arg0,arg1);
                return;

            }

            if(str.length()<6||str.length()>12)
            {
                arg0.setAttribute("message", "账号需是6-12位");
                arg0.getRequestDispatcher("Login.jsp").forward(arg0, arg1);
                return;
            }
            for(int a=0;a<str.length();a++)
            {
                if(!(str.charAt(a)>=‘0‘&&str.charAt(a)<=‘9‘||str.charAt(a)>=‘a‘&&str.charAt(a)<=‘z‘||str.charAt(a)>=‘A‘&&str.charAt(a)<=‘Z‘||str.charAt(a)==‘_‘))
                {
                    arg0.setAttribute("message", "账号只能由字母数字下划线组成");
                    arg0.getRequestDispatcher("Login.jsp").forward(arg0, arg1);
                    return;
                }
            }
            sta.setYonghuming(str);
            str=arg0.getParameter("password");
            if(str.length()<8||str==null||str.equals(""))
            {
                arg0.setAttribute("message", "用户密码需要是8位以上数组字母组成");
                arg0.getRequestDispatcher("Login.jsp").forward(arg0,arg1);
                return;
            }
            int flag1=0;
            int flag2=0;
            for(int b=0;b<str.length();b++)
            {
                if(str.charAt(b)>=‘0‘&&str.charAt(b)<=‘9‘)
                {
                    flag1=1;
                }
                if(str.charAt(b)>=‘a‘&&str.charAt(b)<=‘z‘||str.charAt(b)>=‘A‘&&str.charAt(b)<=‘Z‘)
                {
                    flag2=1;
                }
            }

            if(flag1!=1||flag2!=1)
            {
                arg0.setAttribute("message", "安全程度低,需要由8位以上的数字和字母组成");
                arg0.getRequestDispatcher("Login.jsp").forward(arg0, arg1);
                return;
            }
            sta.setPassword(str);
            str=arg0.getParameter("sex");
            sta.setSex(str);
            str=arg0.getParameter("name");
            if(str.length()==0||str==null)
            {
                arg0.setAttribute("message", "姓名不能为空");
                arg0.getRequestDispatcher("Login.jsp").forward(arg0, arg1);
                return;
            }
            sta.setMingzi(str);
            str=arg0.getParameter("num");
            if(str.length()!=8)
            {
                arg0.setAttribute("message", "学号是由8位数字组成");
                arg0.getRequestDispatcher("Login.jsp").forward(arg0, arg1);
                return;
            }
            int flag3=1;
            if(str.charAt(0)!=‘2‘)flag3=0;
            if(str.charAt(1)!=‘0‘)flag3=0;
            if(str.charAt(2)!=‘1‘)flag3=0;
            if(str.charAt(3)!=‘8‘)flag3=0;
            if(flag3==0)
            {
                arg0.setAttribute("message", "学号输入错误");
                arg0.getRequestDispatcher("Login.jsp").forward(arg0, arg1);
                return;
            }
            sta.setNum(str);

            str=arg0.getParameter("email");
            boolean nani=LoginServlet.isMatchString(str,"([a-zA-Z0-9])[email protected]([a-zA-Z0-9])+\\.([a-zA-Z0-9])+");
            if(!nani)
            {
                arg0.setAttribute("message", "请输入正确格式的邮箱号码");
                arg0.getRequestDispatcher("Login.jsp").forward(arg0, arg1);
                return;
            }

            sta.setEmail(str);
            str=arg0.getParameter("xueyuan");
            sta.setXueyuan(str);
            str=arg0.getParameter("xi");
            sta.setXi(str);
            str=arg0.getParameter("banji");
            sta.setBanji(str);

            str=arg0.getParameter("ruxue");
            sta.setRuxue(str);
            str=arg0.getParameter("dizhi");
            sta.setDizhi(str);

            str=arg0.getParameter("beizhu");
            sta.setBeizhu(str);

            //判断是否添加数据库成功
            if(fuwu.add("tianjiaxuesheng", sta))
            {
                arg0.setAttribute("message", "注册成功");
                arg0.getRequestDispatcher("Login.jsp").forward(arg0, arg1);
            }
            else
            {
                arg0.setAttribute("message", "注册失败");
                arg0.getRequestDispatcher("Login.jsp").forward(arg0, arg1);
            }

        }
    }

    /**
     * @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)
     */
    /*protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }*/

}

暂存信息层:

package zzz;

public class zhucexinxi {
    String yonghuming;
    String password;
    String sex;
    String mingzi;
    String num;
    String email;
    String xueyuan;
    String xi;
    String banji;
    String ruxue;
    String dizhi;
    String beizhu;
    public String getXueyuan() {
        return xueyuan;
    }
    public void setXueyuan(String xueyuan) {
        this.xueyuan = xueyuan;
    }
    public String getYonghuming() {
        return yonghuming;
    }
    public void setYonghuming(String yonghuming) {
        this.yonghuming = yonghuming;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getMingzi() {
        return mingzi;
    }
    public void setMingzi(String mingzi) {
        this.mingzi = mingzi;
    }
    public String getNum() {
        return num;
    }
    public void setNum(String num) {
        this.num = num;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getXi() {
        return xi;
    }
    public void setXi(String xi) {
        this.xi = xi;
    }
    public String getBanji() {
        return banji;
    }
    public void setBanji(String banji) {
        this.banji = banji;
    }
    public String getRuxue() {
        return ruxue;
    }
    public void setRuxue(String ruxue) {
        this.ruxue = ruxue;
    }
    public String getDizhi() {
        return dizhi;
    }
    public void setDizhi(String dizhi) {
        this.dizhi = dizhi;
    }
    public String getBeizhu() {
        return beizhu;
    }
    public void setBeizhu(String beizhu) {
        this.beizhu = beizhu;
    }

}

数据连接层:

package zzz;

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

/**
 * 数据库连接工具
 * @author Hu
 *
 */
public class DBUtil {

    public static String db_url = "jdbc:mysql://localhost:3306/yonghucaozuo?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8";
    public static String db_user = "root";
    public static String db_pass = "password";

    public static Connection getConn () {
        Connection conn = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动
            conn = DriverManager.getConnection(db_url, db_user, db_pass);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return conn;
    }

    /**
     * 关闭连接
     * @param state
     * @param 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();
            }
        }
    }

    public static void main(String[] args) throws SQLException {
        Connection conn = getConn();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql ="select * from course";
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        if(rs.next()){
            System.out.println("空");
        }else{
            System.out.println("不空");
        }
    }
}

Dao层:

package zzz;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ZhuceDao {

    public boolean add(String table,zhucexinxi en)
    {
        boolean c=false;
        String sql="insert into "+table+"(username,password,sex,name,num,email,xueyuan,xi,banji,ruxue,dizhi,beizhu) values(‘"+en.getYonghuming()+"‘,‘"+en.getPassword()+"‘,‘"+en.getSex()+"‘,‘"+en.getMingzi()+"‘,‘"+en.getNum()+"‘,‘"+en.getEmail()+"‘,‘"+en.getXueyuan()+"‘,‘"+en.getXi()+"‘,‘"+en.getBanji()+"‘,‘"+en.getRuxue()+"‘,‘"+en.getDizhi()+"‘,‘"+en.getBeizhu()+"‘);";
        Connection conn=DBUtil.getConn();
        Statement state=null;
        try {
            state=conn.createStatement();
            int num=state.executeUpdate(sql);
            if(num!=0)
                c=true;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return c;

    }

}

服务层:

package zzz;

public class zhuceService {
     ZhuceDao  str=new ZhuceDao();
     public boolean add(String table,zhucexinxi en)
     {
         return str.add(table, en);
     }

}

原文地址:https://www.cnblogs.com/yizhixiaozhu/p/11715712.html

时间: 2024-10-09 14:29:42

web添加学生信息(首发web)的相关文章

从C#中通过Windows窗体添加信息到数据库 (添加学生信息)

如上图所示界面,当我们点击保存按钮时将会将表格中的数据保存到数据库中去,与数据库进行一个交互 第一步我们就是要获取到表格中的数据 string pwd = textpwd.Text; //获得第一次输入密码 string agePqd = txtsurepwd.Text; //获得第二次输入的密码 string name = textname.Text; //获得用户输入的名字 int grendID = Change(); //获得用户输入年级 string phone = textphone

添加学生信息(Javaweb)

add.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%--设置页面的脚本支持语言为java—导入util包中的类—申明编码方式为UTF-8--%> <% request.setCharacterEncoding("UTF-8"); //设置响应的编码为UTF-8 response.setChar

添加学生信息并显示

思路分析 第一,创建表单 <fieldset> <legend>学生信息添加</legend> <form id="mainForm"> 姓名:<input type="text" name="name"> <br> 年龄:<input type="text" name="age"> <br> 性别:<in

java7连接数据库 网页 添加学生信息测试

石家庄铁道大学2019年秋季   2018 级课堂测试试卷(六)(10分) 课程名称: JAVA语言程序设计  任课教师: 王建民        考试时间: 150 分钟 一. 考试要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1分) 3性别:要求用单选框或下拉框实现,选项只有“男”或“女”:(1分) 4学号:要求八位数字组成,前四位为“2018”开头,输入自己学号:(1

向班级集合中添加学生信息

实现效果: 知识运用: System.Collections命名空间下的ArrayList集合类的Add()方法 使用了 DataGridView 控件 实现代码: private void Form1_Load(object sender, EventArgs e) { ArrayList list_StudentInfo = new ArrayList(); string students=""; string[] arr_student, arr_studentinfo; lis

javaweb之添加学生信息

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title>

学习练习 读取学生信息表并添加数据

1 <%@page import="java.sql.*"%> 2 <%@page import="java.sql.DriverManager"%> 3 <%@ page language="java" contentType="text/html; charset=UTF-8" 4 pageEncoding="UTF-8"%> 5 <!DOCTYPE html

Web技术相关信息列表

http://baike.baidu.com/view/15906.htm#rs_15906_5829 Web技术相关信息列表 WEB技术 HTML XHTML ? HTML 5 ? CSS ? TCP/IP XML XML ? XSL ? XSLT ? XSL-FO ? XPath ? XPointer ? XLink ? DTD ? XML Schema ? DOM ? XForms? SOAP ? WSDL ? RDF ? RSS ? WAP ? Web Services Web脚本 Ja

实现学生信息添加界面(连接数据库)

  1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1分) 3性别:要求用单选框或下拉框实现,选项只有“男”或“女”:(1分) 4学号:要求八位数字组成,前四位为“2018”开头,输入自己学号:(1分) 5姓名:输入自己的姓名: 5电子邮箱:要求判断正确格式[email protected]:(1分) 6点击“添加”按钮,将学生个人信息存储到数据库中.(3分) 7可以演示连接上数据