期中考试

1.userbean.java//设置数据
package com.bean;

public class userbean {
private String name;
private String studentid;
private String studenttype;
private String faculty;
private String phone;
private String color;
private String path;
private String other;

 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 getStudenttype() {
     return studenttype;
}
public void setStudenttype(String studenttype) {
     this.studenttype = studenttype;
 }

public String getFaculty() {
     return faculty;     }
public void setFaculty(String faculty) {
    this.faculty = faculty;
}

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 getPath() {
return path; }
public void setPath(String path) {
this.path = path;
}

public String getOther() {
return other; }
public void setOther(String other) {
this.other = other;
}
}
2.userdao.java//连接数据库
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.bean.userbean;
import com.util.userutil;
public class userdao {
public void add(userbean claz) throws ClassNotFoundException, SQLException
{
//获得链接对象
Connection connection = userutil.getConnection();
//准备sql语句
String sql = "insert into course(name,studentid,studenttype,faculty,phone,color,path,other) values(?,?,?,?,?,?,?,?)";
PreparedStatement preparedStatement = null;

         try {
         //创建语句传输对象
         preparedStatement = connection.prepareStatement(sql);
       // preparedStatement = connection.prepareStatement(sql);
         preparedStatement.setString(1, claz.getName());
         preparedStatement.setString(2, claz.getStudentid());
         preparedStatement.setString(3, claz.getStudenttype());
         preparedStatement.setString(4, claz.getFaculty());
         preparedStatement.setString(5, claz.getPhone());
         preparedStatement.setString(6, claz.getColor());
         preparedStatement.setString(7, claz.getPath());
         preparedStatement.setString(8, claz.getOther());
         preparedStatement.executeUpdate();
         } catch (SQLException e) {
            // TODO Auto-generated catch block
           e.printStackTrace();
        }finally {
            //关闭资源
             
            //DBUtil.close(preparedStatement);
            //DBUtil.close(connection);
        }
        
    }

}
3.userutil.java//向数据库里导入数据
package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
public class userutil {
public static Connection getConnection() throws ClassNotFoundException, SQLException {

     Connection connection = null;//连接数据库
     Statement stmt = null;//Statement 对象用于将 SQL 语句发送到数据库中。
     ResultSet rs = null;
  //1. 导入驱动jar包
  //2.注册驱动
  Class.forName("com.mysql.jdbc.Driver");
   connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/work", "root", "lyf123456");

return connection;
}

public static void close(Connection connection ) {
try {
if (connection != null) {
connection.close();
}

  } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
 }

}
public static void close(PreparedStatement preparedStatement ) {
try {
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(ResultSet resultSet ) {
try {
if (resultSet != null) {
resultSet.close();
}

   } catch (SQLException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
   }

}

}
4.add.jsp//后端接收数据
<%@page import="com.bean.userbean"%>
<%@page import="com.dao.userdao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<% //接收客户端传递过来的参数
 String name = request.getParameter("name");
 String studentid = request.getParameter("studentid");
 String studenttype = request.getParameter("studenttype");
 String faculty = request.getParameter("faculty");
 String phone = request.getParameter("phone");
 String color = request.getParameter("color");
 String path = request.getParameter("path");
 String other = request.getParameter("other");

// try{
//if()
{
// throw new ClassException("信息填报错误");
}
// else if()
{
// throw new ClassException("信息填报错误");
}
// else
{
userbean A = new userbean();
A.setName(name);
A.setStudentid(studentid);
A.setStudenttype(studenttype);
A.setFaculty(faculty);
A.setPhone(phone);
A.setColor(color);
A.setPath(path);
A.setOther(other);
userdao Dao = new userdao();
Dao.add(A);

 }    

%>

填报成功

5.jm.jsp//前端显示界面 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 石家庄铁道大学在校学生信息行程信息统计
        <tr>
            <td>姓名 : </td>
            <td>
                <input type="text" name="name" />
            </td>
        </tr>
        
        <tr>
            <td>学号:</td>
            <td>
                <input type="tel" name="studentid" required pattern="[0-9]{8}"/>
            </td>
        </tr>
        
         <tr>
            <td>学生类型:</td>
            <td>
                <input type="radio" name="studenttype" value="本科生" >本科生
                <input type="radio" name="studenttype" value="研究生" >研究生
            </td>
        </tr>
        
          <tr>
         <td>院系:</td>
        <td>
		<select name="faculty">
				<option value="土木学院">土木学院</option>
				<option value="机械学院">机械学院</option>
				<option value="交通学院">交通学院</option>
				<option value="信息学院">信息学院</option>
				<option value="经管学院">经管学院</option>
				</select>
		</td>
        </tr>
        
       
        
        
         <tr>
            <td>联系电话:</td>
            <td>
                <input type="tel" name="phone" required pattern="[0-9]{11}"/>
            </td>
        </tr>
        
        <tr>
            <td>行程统计:</td>
            <td>
               <input type="checkbox" name="path" value="10月30日去过人民医院">10月30日去过人民医院<br>
               <input type="checkbox" name="path" value="10月25日以来去过深泽县人民医院">10月25日以来去过深泽县人民医院<br>
               <input type="checkbox" name="path" value="10月16日以来去过深泽县庄泽村">10月16日以来去过深泽县庄泽村<br>
               <input type="checkbox" name="path" value="10月29日以来去过黑龙江哈尔滨市或者黑河市">10月29日以来去过黑龙江哈尔滨市或者黑河市<br>
               <input type="checkbox" name="path" value="10月18日以来途径贵州遵义市;北京丰台、昌平">10月18日以来途径贵州遵义市;北京丰台、昌平<br>
               <input type="checkbox" name="path" value="10月17日以来到过湖南长沙;青海海东市">10月17日以来到过湖南长沙;青海海东市<br>
            </td>
        </tr>
        
        
        <tr>
            <td>健康码颜色:</td>
            <td>
                <input type="radio" name="color" value="绿码" >绿码
                <input type="radio" name="color" value="黄码" >黄码
                <input type="radio" name="color" value="红码" >红码
            </td>
        </tr>
                
        
        <tr>
            <td>其他涉疫信息需要填报的:</td>
            <td>
                <input type="text" name="other" />
            </td>
        </tr>
        
        <tr align="center">
            <td colspan="2">
               <input type="submit" value="提交">
            </td>
        </tr>
    </table>
</form>
posted @ 2021-11-05 22:40  Lindseyyip  阅读(33)  评论(0)    收藏  举报