学生信息的添加 Java web简单项目初试(修改)

时间:2022-08-06 10:42:29

错误原因:

1.Java web 的Servlet类没有配置好,并且缺少一个 Dao类(Date Access Object通常用于操作数据库的).

2.代码的某些名称错误,导致数据库数据存储错误。

添加的Dao类如下:

 package com.Dao;

 import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.DBUtil.UserDBUtil; import jdk.nashorn.internal.ir.RuntimeNode.Request; public class Dao {
public boolean add(String username,String password,String name,String studentID,String sex,String phone,String email,String xueyuan,String xi,String classes,String year,String shengyuandi,String beizhu) {
String sql = "insert into use02 values ('" + username + "','" + password +"','" + name +"','" +studentID +"','" + sex +"','" +phone +"','" + email +"','" +xueyuan +"','"+xi +"','"+classes +"','"+year+"','"+shengyuandi +"','"+beizhu +"')";
// 创建数据库链接
Connection conn = UserDBUtil.getConnection();
Statement state = null;
boolean f = false;
int a = 0;
try {
state = conn.createStatement();
a = state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭z 连接
//DB.close(state, conn);
} if (a > 0) {
f = true;
}
return f;
}
}

修改后的Servlet代码如下:

 import java.io.IOException;

 import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.Dao.*;
import com.DBUtil.*; /**
* Servlet implementation class UserServlet
*/
@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public AddServlet() {
super();
// TODO Auto-generated constructor stub
}
Dao dao = new Dao(); 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);
System.out.println("!!!");
}
}
private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
// TODO Auto-generated method stub
String username = req.getParameter("username");
String password = req.getParameter("password");
String name = req.getParameter("name");
String studentID = req.getParameter("studentID");
String sex = req.getParameter("sex");
String phone = req.getParameter("phone");
String email = req.getParameter("email");
String xueyuan = req.getParameter("xueyuan");
String xi = req.getParameter("xi");
String classes = req.getParameter("classes");
String year = req.getParameter("year");
String shengyuandi = req.getParameter("shengyuandi");
String beizhu = req.getParameter("beizhu");
if(dao.add(username,password,name,studentID,sex,phone,email,xueyuan,xi,classes,year,shengyuandi,beizhu)) {
req.setAttribute("message", "注册成功!");
req.getRequestDispatcher("index1.jsp").forward(req, resp);
return;
}else {
req.setAttribute("message", "账号重复,请重新输入!");
req.getRequestDispatcher("index1.jsp").forward(req, resp);
return;
}
//req.getRequestDispatcher("index1.jsp").forward(req, resp);
} }

bean类

 public class User {
private String username;
private String password;
private String name;
private String studentID;
private String sex;
private String phone;
private String email;
private String xueyuan;
private String xi;
private String classes;
private String year;
private String shengyuandi;
private String beizhu;
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 getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStudentID() {
return studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getXueyuan() {
return xueyuan;
}
public void setXueyuan(String xueyuan) {
this.xueyuan = xueyuan;
}
public String getXi() {
return xi;
}
public void setXi(String xi) {
this.xi = xi;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getShengyuandi() {
return shengyuandi;
}
public void setShengyuandi(String shengyuandi) {
this.shengyuandi = shengyuandi;
}
public String getBeizhu() {
return beizhu;
}
public void setBeizhu(String beizhu) {
this.beizhu = beizhu;
}
public User(String username, String password, String name, String studentID, String sex, String phone, String email,
String xueyuan, String xi, String classes, String year, String shengyuandi, String beizhu) {
super();
this.username = username;
this.password = password;
this.name = name;
this.studentID = studentID;
this.sex = sex;
this.phone = phone;
this.email = email;
this.xueyuan = xueyuan;
this.xi = xi;
this.classes = classes;
this.year = year;
this.shengyuandi = shengyuandi;
this.beizhu = beizhu;
} }

数据库连接程序:

 import java.sql.Connection;

 import java.sql.DriverManager;
//import java.sql.ResultSet;
import java.sql.SQLException;
//import java.sql.Statement; public class UserDBUtil {
static Connection con;
public static Connection getConnection() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("数据库驱动加载成功");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} try {
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B88&serverTimezone=GMT%2B8&useSSL=false","root","101032");
System.out.println("数据库链接成功!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return con;
} public static void main(String[] args) {
UserDBUtil c=new UserDBUtil();
c.getConnection(); }

jsp 页面源代码实现:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户注册</title>
</head>
<body> <script type="text/javascript" src="./js/jquery-3.3.1.min.js"></script> <div class="container">
<section>
<form action="AddServlet?method=add" method="post" onsubmit="return check(this)">
<span class="username">登陆账号</span>
<input type="text" name="username" placeholder=" username" id="username">
<br/> <span class="password">登陆密码</span>
<input type="password" name="password" placeholder=" password" id="password">
<br/> <span class="name">姓名</span>
<input type="text" name="name" placeholder=" name" id="name">
<br/> <span class="studentID">学号</span>
<input type="text" name="studentID" placeholder=" studentID" id="studentID" >
<br/> <span class="sex">性别</span>
<input type="radio" name="sex" id="sexnan" value="M">男
&nbsp;
<input type="radio" name="sex" id="sexnv" value="F">女
<br/> <!-- <span class="diqu">地区</span> -->
<!-- <select name="diqu"> -->
<!-- <option value ="北京市">北京市</option> -->
<!-- <option value ="天津市">天津市</option> -->
<!-- <option value="河北省">河北省</option> -->
<!-- </select> -->
<!-- <br/> --> <span class="phone">手机号</span>
<input type="text" name="phone" placeholder=" phone" id="phone">
<br/> <span class="email">邮箱</span>
<input type="text" name="email" placeholder=" email" id="email">
<br/> <span class="xueyuan">所在学院:</span>
<input type="text" name="xueyuan" placeholder=" xueyuan" id="xueyuan">
<br/> <span class="xi">所在系:</span>
<input type="text" name="xi" placeholder=" suozaixi" id="xi">
<br/> <span class="classes">所在班级:</span>
<input type="text" name="classes" placeholder=" classes" id="classes">
<br/> <span class="year">入学年份(届):</span>
<select name="year">
<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>
<br/> <span class="shengyuandi">生源地:</span>
<input type="text" name="shengyuandi" placeholder=" shengyuandi" id="shengyuandi">
<br/> <span class="beizhu">备注:</span>
<input type="text" name="beizhu" placeholder=" beizhu" id="beizhu">
<br/> <input type="submit" value="保存" class="butn">
<!-- <input type="reset" value="重置" class="butn"> -->
<br/>
</form>
</section>
</div> <script>
//正则表达式
function check(form){ if(form.username.value.length<6 || form.username.value.length>12 || new RegExp("[^0-9a-zA-Z]").test(form.username.value)){
alert("登陆账号必须由6-12位英文字符或数字组成!");
form.user.focus();
return false;
}
if(new RegExp("[^a-zA-Z]").test(form.username.value.substring(0,1))){
alert("登陆账号必须以英文字母开头!");
form.user.focus();
return false;
}
if(form.password.value.length <= 8 || new RegExp("[^0-9a-zA-Z]").test(form.password.value)){
alert("密码必须由八位以上字母、数字组成!");
form.phone.focus();
return false;
}
if(form.studentID.value.length != 8 || new RegExp("[^0-9]").test(form.studentID.value)){
alert("请输入八位学号!");
form.phone.focus();
return false;
} if(form.phone.value.length != 11 || new RegExp("[^0-9]").test(form.phone.value)){
alert("手机号请输入11位数字!");
form.phone.focus();
return false;
} //以数字字母开头,中间可以是多个数字字母或下划线;然后是“@”;然后是数字字母;然后是“.”;最后是2-4个字母结尾
var regex = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
if(!regex.test(form.email.value)){
alert("邮箱格式错误!");
form.email.focus();
return false;
}
return true;
}
</script> </body>
</html>

数据库存储界面:

学生信息的添加 Java web简单项目初试(修改)

对于Java web数据库连接的理解:

Java类:

1.servlet 类 处理从前端接受的信息,即jsp 与数据库连接的接口

2.DBUtil 类  加载数据库驱动, 建立对数据库的连接。

3.Dao 类  实现对数据库的增删改查

4.Bean 类 存储信息的基础类

5.在lib目录下导入需要的jar的包

学生信息的添加 Java web简单项目初试(修改)

6. jsp 实现界面。

本人目前处于初学阶段,代码大部分都是对模板进行修改,没有完全理解,仍旧有许多需要学习的地方。

数据库的JDBC桥接:

面向对象的应用程序接口,制定了统一的访问各种关系型数据库的标准接口。

JDBC任务:

1.与数据库建立连接

2.向数据库发生sql语句

3.处理从数据库返回的结果

以下是之前的错误代码:

https://www.cnblogs.com/cxy0210/p/11715924.html

以上代码如有错误,敬请指正,感激不尽。