数据库添加 Javaweb

HTML页面,用于收集信息

<html>
<head>
<meta meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Insert title here</title>
</head>
<body>
<form name="enetering" action="/qizhongzuoye/EclipseServlet" method="post">
<table border="1">
<tr>
<td>姓名</td>
<td><input name="name" type="text"></td>
</tr>
<tr>
<td>学号</td>
<td><input name="ID" type="text" maxlength="8" onkeyup = "value=value.replace(/[^\d]/g,'')"></td>
</tr>
<tr>
<td>学生类别</td>
<td><input name="stutype" type="radio" value="本科生">本科生<input name="stutype" type="radio" value="本科生">研究生</td>
</tr>
<tr>
<td>院系</td>
<td><input name="stuc" type="text" list="sc"><datalist id="sc"><option>土木学院</option><option>机械学院</option><option>交通学院</option><option>信息学院</option><option>经管学院</option></datalist></td>
</tr>
<tr>
<td>联系电话</td>
<td><input name="phone" type="text" maxlength="11" onkeyup = "value=value.replace(/[^\d]/g,'')"></td>
</tr>
<tr>
<td>健康码颜色</td>
<td><input name="color" type="radio" value="绿码">绿码<input name="color" type="radio" value="黄码">黄码<input name="color" type="radio" value="红码">红码</td>
</tr>
<tr>
<td>行程统计</td>
<td><input name="route" type="checkbox" value="10月30日去过人民医院">10月30日去过人民医院<br>
<input name="route" type="checkbox" value="10月25日以来去过深泽县人民医院">10月25日以来去过深泽县人民医院<br>
<input name="route" type="checkbox" value="10月16日以来去过深泽县庄泽村">10月16日以来去过深泽县庄泽村<br>
<input name="route" type="checkbox" value="10月29日以来去过黑龙江哈尔滨市或者黑河市">10月29日以来去过黑龙江哈尔滨市或者黑河市<br>
<input name="route" type="checkbox" value="10月18日以来途径贵州遵义市;北京丰台、昌平">10月18日以来途径贵州遵义市;北京丰台、昌平<br>
<input name="route" type="checkbox" value="10月18日以来途径贵州遵义市;北京丰台、昌平">10月17日以来到过湖南长沙;青海海东市</td>
</tr>
<tr>
<td>其他设计疫情信息</td>
<td><input name="other" type="text"></td>
</tr>
<tr>
<td><input type="submit" value="提交" /></td>
<td><input type="reset" value="重置" /></td>
</tr>
</table>
</form>
</body>
</html>

sqk_conn类,用于连接数据库并通过方法返回一个Statement对象操纵数据库

package sql_conn;
import java.sql.*;
public class sql_conn {
private static final String JDBC_driver="com.mysql.cj.jdbc.Driver";
private static final String DB_url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
private static final String username="dz";
private static final String password="123456";
private static final String tablename="qizhong";
private Connection conn;
private java.sql.Statement stmt;
public static String getTablename() {
return tablename;
}
public Statement lianjie() {
conn=null;
try {
Class.forName(JDBC_driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn=DriverManager.getConnection(DB_url,username,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stmt=conn.createStatement();
System.out.println("数据库连接成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stmt;
}
public void closethis() {
try {
stmt.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

bean类,用于储存从页面获取的数据并将其合并成适合数据库语句的字符串对象

package bean;

public class bean {
private String name;
private String ID;
private String stutype;
private String stuc;
private String phone;
private String color;
private String[] route;
private String other;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public String getStutype() {
return stutype;
}
public void setStutype(String stutype) {
this.stutype = stutype;
}
public String getStuc() {
return stuc;
}
public void setStuc(String stuc) {
this.stuc = stuc;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getRoute() {
String route0="";
for(int i=0;i<route.length;i++) {
route0+=route[i];
if(i!=route.length)
route0+=",";
}
return route0;
}
public void setRoute(String[] route) {
this.route = route;
}
public String getOther() {
return other;
}
public void setOther(String other) {
this.other = other;
}
public String hebing() {
return "'"+getName()+"','"+getID()+"','"+getStutype()+"','"+getStuc()+"','"+getPhone()+"','"+getColor()+"','"+getRoute()+"','"+getOther()+"'";
}
}

servlet类,用于将数据写入数据库

package servelet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import sql_conn.*;
import bean.*;

import javax.servlet.ServletConfig;
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 javax.sql.rowset.serial.SerialException;
@WebServlet("/EclipseServlet")
public class servelet extends HttpServlet{
public servelet() {
super();
}
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
sql_conn lj=new sql_conn();
bean xinxi=new bean();
xinxi.setName(request.getParameter("name"));
xinxi.setID(request.getParameter("ID"));
xinxi.setStutype(request.getParameter("stutype"));
xinxi.setStuc(request.getParameter("stuc"));
xinxi.setPhone(request.getParameter("phone"));
xinxi.setColor(request.getParameter("color"));
xinxi.setRoute(request.getParameterValues("route"));
xinxi.setOther(request.getParameter("other"));
try {
lj.lianjie().executeUpdate("insert into "+lj.getTablename()+"(name,ID,stutype,stuc,phone,color,route,other)values("+xinxi.hebing()+");");
System.out.println("输入成功!");
PrintWriter out=response.getWriter();
out.println("数据以写入<br><a href=\"NewFile.html\">返回上一页</a>");
} catch (SQLException e) {
// TODO Auto-generated catch block
PrintWriter out=response.getWriter();
out.println("数据未写入<br><a href=\"NewFile.html\">返回上一页</a>");
e.printStackTrace();
}
lj.closethis();
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {
doGet(request,response);
}
public void init(ServletConfig servletconfig) {

}
public void destroy() {

}
public ServletConfig getServletConfig() {
return null;
}
public String getServletinfo() {
return null;
}
}

 

posted @ 2021-11-05 21:00  sdkfn  阅读(83)  评论(0编辑  收藏  举报