eclipse+MySQL实现

一、前端部分,通过编写jsp实现添加页面:

add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>信息添加</title> <script type="text/javascript"> function check() {
var name1 = document.getElementById("name");
var name2 = document.getElementById("name2");
var mima1 = document.getElementById("password");
var mima2 = document.getElementById("mima2");
if (name1.value == "") {
name2.innerHTML = "请输入用户名".fontcolor("red");
name1.focus();
return false;
} else {
name2.innerHTML = "".fontcolor("green");
}
if (mima1.value == "") {
mima2.innerHTML = "请输入密码".fontcolor("red");
mima1.focus();
return false;
} else {
mima2.innerHTML = "".fontcolor("green");
}
}
function check1() {
var name1 = document.getElementById("name");
var name2 = document.getElementById("name2")
var ref = /^[a-z0-9A-Z_]{4,10}$/
if (name1.value.length > 10 || name1.value.length < 4) {
name2.innerHTML = "用户名应为4到10位".fontcolor("red");
name1.focus();
return false;
} else {
name2.innerHTML = "".fontcolor("green");
}
if (!ref.test(name1.value)) {
name2.innerHTML = "由数字字母下划线组成".fontcolor("red");
name1.focus();
return false;
} else {
name2.innerHTML = "".fontcolor("green");
}
}
function check2() {
var mima1 = document.getElementById("password");
var mima2 = document.getElementById("mima2");
var ref = /^[a-z0-9A-Z_]{4,10}$/
if (mima1.value.length > 10 || mima1.value.length < 4) {
mima2.innerHTML = "密码应为4到10位".fontcolor("red");
mima1.focus();
return false;
} else {
mima2.innerHTML = "".fontcolor("green");
}
if (!ref.test(mima1.value)) {
mima2.innerHTML = "由数字字母下划线组成".fontcolor("red");
mima1.focus();
return false;
} else {
name2.innerHTML = "".fontcolor("green");
}
}
</script> </head>
<body>
<%
Object message = request.getAttribute("message");
if(message!=null && !"".equals(message)){ %>
<script type="text/javascript">
alert("<%=request.getAttribute("message")%>");
</script>
<%} %>
<div align="center">
<h1 style="color: black;">信息录入</h1>
<form action="adduser" method="post" onsubmit="return check()">
<div>
姓名:<input type="text" id="name" name="name" onchange="check1()" /><span id="name2"></span>
</div>
<div>
密码:<input type="password" id="password" name="password"onchange="check2()" /><span id="mima2"></span>
</div>
<div>
性别: <input type="radio" name="sex" value="男">男
<input type="radio" name="sex" value="女">女
</div>
<div>
邮箱:<input type="email" name="email" />
</div>
<div>
学校:<select name="school">
<option value="铁大">铁大</option>
<option value="北大">北大</option>
<option value="清华 ">清华</option>
</select>
</div>
<div>
<input type="submit" value="添加" />
<input type="reset" value="重置" />
</div>
</form>
</div>
</body>
</html>

绿色部分用来校验表单,若对用户输入数据无要求可以删去

效果图:

二、后台部分,即Java代码

1、创建相关包,如图所示:

User.java

package bean;

public class User {
private int id;
private String name;
private String password;
private String sex;
private String email;
private String school;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public User() {
super();
// TODO 自动生成的构造函数存根
}
public User(String name, String password, String sex, String email, String school) {
super();
this.name = name;
this.password = password;
this.sex = sex;
this.email = email;
this.school = school;
}
}

UserDao.java

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import bean.User;
import util.DBUtil; public class UserDao { public boolean adduser(User b)
{
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
con=DBUtil.getConn();
String sql="insert into user(name,password,sex,email,school) values(?,?,?,?,?)";
pstmt=con.prepareStatement(sql);
pstmt.setString(1, b.getName());
pstmt.setString(2, b.getPassword());
pstmt.setString(3, b.getSex());
pstmt.setString(4, b.getEmail());
pstmt.setString(5, b.getSchool());
pstmt.executeUpdate();
return true;
}
catch (SQLException e) {
System.out.println("添加失败");
e.printStackTrace();
}
finally {
DBUtil.close(rs, pstmt, con);
}
return false;
} }

UserService.java

package service;

import bean.User;
import dao.UserDao; public class UserService { UserDao userdao = new UserDao(); public boolean adduser(User b) {
return userdao.adduser(b);
} }

adduser.java

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 bean.User;
import service.UserService; /**
* Servlet implementation class adduser
*/
@WebServlet("/adduser")
public class adduser extends HttpServlet {
private static final long serialVersionUID = 1L;
UserService service=new UserService(); /**
* @see HttpServlet#HttpServlet()
*/
public adduser() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
String password=request.getParameter("password");
String sex=request.getParameter("sex");
String email=request.getParameter("email");
String school=request.getParameter("school");
User bean=new User(name,password,sex,email,school);
if(service.adduser(bean))
{
request.setAttribute("message", "添加成功");
request.getRequestDispatcher("add.jsp").forward(request, response);
}
else
{
request.setAttribute("message", "添加失败");
request.getRequestDispatcher("add.jsp").forward(request, response);
} }
/**
* @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);
}
}

DBUtil.java

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 { private static final String db_url="jdbc:mysql://localhost:3306/mmm?useUnicode=true&characterEncoding=GB18030&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true";
public static String db_user = "root";
public static String db_pass = "123456";//数据库密码 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;
}
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();
}
}
}
}

三、数据库中相关表的创建

添加学生信息(Javaweb)的更多相关文章

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

    如上图所示界面,当我们点击保存按钮时将会将表格中的数据保存到数据库中去,与数据库进行一个交互 第一步我们就是要获取到表格中的数据 string pwd = textpwd.Text; //获得第一次输 ...

  2. MVC学生管理系统-阶段II(添加学生信息)

    项目源码 :https://download.csdn.net/download/weixin_44718300/11091042 前期准备,主体框架,学生列表显示    请看上一篇文章 本文是对阶段 ...

  3. javaweb之添加学生信息

    1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1分) 3性别:要求用单选框或下拉框 ...

  4. javaweb添加学生信息

    连接数据库已经进行判断 要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1分 ...

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

    石家庄铁道大学2019年秋季   2018 级课堂测试试卷(六)(10分) 课程名称: JAVA语言程序设计  任课教师: 王建民        考试时间: 150 分钟 一. 考试要求: 1登录账号 ...

  6. 添加学生信息(java wb)

    要求: 1.登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头: 2.登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成: 3.性别:要求用单选框或下拉框实 ...

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

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

  8. 向数据库添加学生信息。存放在REQUEST对象里

    代码前几天已经发过了,但是程序一直还没运行出来,今天重新建立了一个数据库,才可以,下面补充上数据截图

  9. javeweb_学生信息添加系统

    在text.jsp中画出界面,以及设置提交选项的限制 <%@ page language="java" contentType="text/html; charse ...

随机推荐

  1. 机器学习——EM

    整理自: https://blog.csdn.net/woaidapaopao/article/details/77806273?locationnum=9&fps=1 EM算法是用于含有隐变 ...

  2. ZR 8.31

    ZR8.31 题目链接:http://www.zhengruioi.com/contest/388 版权原因,不放题面 A 首先,排序肯定要根据工作经验排序,因为这样便于选择 之后,如果两个人工作经验 ...

  3. 【hdu 1849】Rabbit and Grass

    Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  4. vue依赖

    ajax:vue-resource moke数据:body-parser

  5. Hibernate各种查询方式及查询策略(转)

    转自:https://www.cnblogs.com/xujingyang/p/6734203.html 在了解Hibernate的基本知识后,重点让我们看下相关查询方式和查询优化策略吧! 话不多说, ...

  6. Vue通讯

    vue组件通讯 #props传递数据 父->子 //父组件传递数据 <template> <Children :data="msg"></Chi ...

  7. java_学生成绩管理系统

    //信1805-2 20183670 王云鹏 package student; import java.util.Scanner; public class ScoreManagement { sta ...

  8. ansible批量部署nginx

    1.1 将nginx的安装包,和安装脚本copy到客户端,并安装脚本 vim /ansible/roles/nginx/tasks/main.yml - name: copy nginx_tar_gz ...

  9. jenkins安装自动部署

    1.1简介: 开源项目,一个可扩展的持续集成引擎.提供了一种易于使用的持续集成系统,能实施监控集成中存在的错误,提供详细的日志文件和提醒功能,还能用图表的形式形象地展示项目构建的趋势和稳定性.还做到持 ...

  10. 洛谷$P2153\ [SDOI2009]$ 晨跑 网络流

    正解:网络流 解题报告: 传送门$QwQ$ 题目好长昂,,,大概概括下$QwQ$.就说有$n$个节点$m$条边,然后要求每次走的路径都不一样,问最多能走多少次,然后在次数最多的前提下问路径最短是多少$ ...