数据库创建的话,我使用数据库管理工具Navicat for MySQL
数据库需要两张表,一张是学生信息表,一张是工时信息表
我的数据库名字为project,里面有两张表
一张是stumessage
主键是学生学号,也就是num
一张是stuworktime
-------------------------------------
创建文件:
创建一个带java文件的包:
Java Resources-src-右键src-new-Package
在已有的包内创建文件(不管用途是什么都是创建一个Class,也就是java文件)
右键所选中的包-new-Class
写文件名,然后Finish
-------------------------------------------------------
下面是学生信息录入的数据库连接
需要的文件:
JSP页面:AddMessage.jsp
Bean类:AddMessageBean.java
Dao文件:AddMessageDao.java
Servlet文件:AddMessageServlet.java
Servlet配置:web.xml
-------------------------------------------
先要写一个Bean类存放提交的表单的信息
AddMessageBean.java
package bean;
public class AddMessageBean {
/**
* 属性和数据库中的字段相对应
*/
private String name; //对应JSP文件里面的每一栏
private String num;
private String sex;
private String college;
private String year;
private String major;
private String poor;
public AddMessageBean() { //构造函数
}
public String getNum() { //将num的值返回
return num;
}
public void setNum(String num) { //设置num的值
this.num = num;
}
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 getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getPoor() {
return poor;
}
public void setPoor(String poor) {
this.poor = poor;
}
}
AddMessageServlet.java
package servlet;
import java.io.*;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bean.AddMessageBean; //导入Bean文件
import dao.AddMessageDao; //导入Dao文件
public class AddMessageServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp); //如果是Get方式,则使用doPost方法
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) //与form表单的method提交方法有关,我的method为post方法
throws ServletException, IOException {
//设置编码,防止请求乱码
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
PrintWriter out = resp.getWriter();//我不知道是干什么的,貌似也没用吧
//获取参数
String Name=req.getParameter("name");//将提交的表单里的name="name"的文本框的值给Name
String Year=req.getParameter("year");
String Sex=req.getParameter("sex");
String ID=req.getParameter("id");
String Insti=req.getParameter("institute");
String Pro=req.getParameter("professional");
String Poor=req.getParameter("level");
AddMessageBean bean =new AddMessageBean(); //创建一个Bean类,将值都赋给Bean类里面对应的值
bean.setName(Name); //将Name用setName函数,将bean的name值设为Name的值
bean.setYear(Year);
if(Sex == "male")
bean.setSex("男");
else
bean.setSex("女");
bean.setNum(ID);
bean.setCollege(Insti);
bean.setMajor(Pro);
if(Poor == "poor")
bean.setPoor("贫困");
else
bean.setPoor("特困");
AddMessageDao dao =new AddMessageDao(); //新建一个Dao类
dao.addClassInfo(bean); //调用addClassInfo方法,将bean作为参数传入
req.getRequestDispatcher("/AddMessage.jsp").forward(req, resp); //完成之后要返回的页面,我返回原来页面
}
}
AddMessageDao.java
package dao;
import java.sql.Connection;
import java.io.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.annotation.WebServlet;
import bean.AddMessageBean;
public class AddMessageDao {
static {
// 加载数据库驱动 ,也就是jdbc
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void addClassInfo(AddMessageBean Bean) {
Connection conn = null;
Statement stmt = null;
try {
// 获取数据库连接,三个参数为("数据库名字","用户名","密码")
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","root");
// 写一条SQL的插入语句,按照表的顺序,依次调用对应的get方法,得到值
String sql = "insert into stumessage() values ('"+Bean.getNum()+"','"+ Bean.getName() +"','"+ Bean.getSex() +"','"
+ Bean.getCollege() +"','"+ Bean.getYear()+"','"+ Bean.getMajor() +"','"+ Bean.getPoor()+"')";
// 创建SQL执行对象
stmt = conn.createStatement();
// 执行sql语句
stmt.executeUpdate(sql);
} catch (SQLException e) { //抓取异常
e.printStackTrace();
} finally { //最后要关掉数据库和statement对象
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}}
Servlet配置,如果不配置,前端和后端就连不起来
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>test</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>AddMessage</servlet-name>
<servlet-class>servlet.AddMessageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddMessage</servlet-name>
<url-pattern>/AddMessage</url-pattern>
</servlet-mapping>
</web-app>
配置需要一个<servlet>和一个<servlet-mapping>
<servlet>中servlet-name与<servlet-mapping>中的servlet-name需要一致
servlet-class:需要执行的servlet的位置,也就是servlet包的AddMessageServlet
url-pattern:地址映射。JSP文件中的form的action的值就是这个的值,必须要有/
----------------------------------------------------------
完成后就可以录入学生信息了。
表中就看到了这条数据库信息
-------------------------------------------------------------------------------------------------------------------------
同理,工时录入操作一样,我就直接贴代码了。
需要的文件:
JSP页面:AddWorkTime.jsp
Bean类:AddWorkTime.java
Dao文件:AddWorkTimeDao.java
Servlet文件:AddWorkTimeServlet.java
Servlet配置:web.xml 直接在上一个的基础上添加一个<servlet>,<servlet-mapping>写对应的值就可以了
---------------------------------------------------------------------------------------
AddWorkTime.java
package bean;
public class AddWorkTime {
private String id;
private String activity;
private String worktime;
private String workdate;
public AddWorkTime() {
}
public String getID() {
return id;
}
public void setID(String id) {
this.id = id;
}
public String getAct() {
return activity;
}
public void setAct(String activity) {
this.activity = activity;
}
public String getTime() {
return worktime;
}
public void setTime(String time) {
this.worktime = time;
}
public String getDate() {
return workdate;
}
public void setDate(String date) {
this.workdate = date;
}
}
AddWorkTimeDao.java
package dao;
import java.sql.*;
import java.util.*;
import java.io.*;
import javax.servlet.annotation.WebServlet;
import bean.AddWorkTime;
public class AddWorkTimeDao {
static {
// 加载数据库驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void Addworktime(AddWorkTime bean) {
Connection conn = null;
Statement stmt = null;
try {
// 获取数据库连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","root");
// 整理一条SQL语句
String sql = "insert into stuworktime(number,workname,worktime,datetime) values ('"+bean.getID()+"','"+ bean.getAct() +"','"
+ bean.getTime() +"','"+bean.getDate()+"')";
// 创建SQL执行对象
stmt=conn.createStatement();
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
AddWorkTimeServlet.java
package servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import bean.AddWorkTime;
import dao.AddWorkTimeDao;
public class AddWorkTimeServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置编码,防止请求乱码
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
PrintWriter out = resp.getWriter();
//获取参数
String ID=req.getParameter("id");
String activity=req.getParameter("activity");
String time=req.getParameter("worktime");
String date=req.getParameter("workdate");
// out.println("get success");
//创建AddWorkTimeBean对象保存信息
AddWorkTime bean =new AddWorkTime();
bean.setAct(activity);
bean.setTime(time);
bean.setID(ID);
bean.setDate(date);
//创建数据库操作对象
AddWorkTimeDao dao =new AddWorkTimeDao();
//新增班级信息到数据库
dao.Addworktime(bean);
//转发请求
req.getRequestDispatcher("/AddWorkTime.jsp").forward(req, resp);
}
}
web.xml更新
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>test</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>AddMessage</servlet-name>
<servlet-class>servlet.AddMessageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddMessage</servlet-name>
<url-pattern>/AddMessage</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>AddWorkTime</servlet-name>
<servlet-class>servlet.AddWorkTimeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddWorkTime</servlet-name>
<url-pattern>/AddWorkTime</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>TimeManage</servlet-name>
<servlet-class>servlet.TimeManageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TimeManage</servlet-name>
<url-pattern>/TimeManage</url-pattern>
</servlet-mapping>
</web-app>
然后工时录入也就完成了