小程序--课程导入
1.设计思想:首先要建立一个新的数据库进行连接,数据库分别存储三列信息:课程名称(classname)、任课教师(teachername)、上课地点(building)。三类信息均为String类,在mysql里面是varchar类型;接着先写一个Class类,有三个信息的定义和其set和get方法;一个IClassDao类定义add方法;一个ClassDaoImpl类,实现add方法(在数据库中add):1。获得链接对象 2。创建语句传输对象 3。接收遍历结果集 4。关闭资源;一个DBUtil类链接数据库:1。 加载驱动 2 。创建链接对象connection 3.关闭资源 ;一个异常类UserException;最后JSP文件:add.jsp和addInput.jsp设计界面和输入要求。
2.程序源代码:
Class.java
package com.jaovo.msg.model; public class Class { //private int id; private String classname; private String teachername; private String building; public String getTeachername() { return teachername; } public void setTeachername(String teachername) { this.teachername = teachername; } public String getClassname() { return classname; } public void setClassname(String classname) { this.classname = classname; } public String getBuilding() { return building; } public void setBuilding(String building) { this.building = building; } /*public int getId() { return id; } public void setId(int id) { this.id = id; }*/ }
IClassDao.java
package com.jaovo.msg.dao; import com.jaovo.msg.model.Class; public interface IClassDao { public void add(Class user); }
ClassDaoImpl.java
package com.jaovo.msg.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.jaovo.msg.Util.DBUtil; import com.jaovo.msg.Util.UserException; import com.jaovo.msg.model.Class; public class ClassDaoImpl implements IClassDao { @SuppressWarnings("resource") @Override public void add(Class user) { //获得链接对象 Connection connection = DBUtil.getConnection(); //准备sql语句 //String sql = "select count(*) from t_user where classname = ?"; //创建语句传输对象 PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { //preparedStatement = connection.prepareStatement(sql); //preparedStatement.setString(1, user.getClassname()); //接收结果集 //resultSet = preparedStatement.executeQuery(); //遍历结果集 //while(resultSet.next()) { // if (resultSet.getInt(1) > 0) { //throw new UserException("用户已存在") ; // } //} String sql = "insert into t_user(classname,teachername,building) value (?,?,?)"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, user.getClassname()); preparedStatement.setString(2, user.getTeachername()); preparedStatement.setString(3, user.getBuilding()); preparedStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { //关闭资源 DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } } }
DBUtil.java
package com.jaovo.msg.Util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBUtil { public static Connection getConnection() { try { //1 加载驱动 Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } String user = "root"; String password = "root"; String url = "jdbc:mysql://localhost:3306/class?useUnicode=true&characterEncoding=UTF-8"; Connection connection = null; try { //2 创建链接对象connection connection = DriverManager.getConnection(url,user,password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } 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(); } } }
UserException.java
package com.jaovo.msg.Util; public class UserException extends RuntimeException{ /** * */ private static final long serialVersionUID = 1L; public UserException() { super(); // TODO Auto-generated constructor stub } public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); // TODO Auto-generated constructor stub } public UserException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } public UserException(String message) { super(message); // TODO Auto-generated constructor stub } public UserException(Throwable cause) { super(cause); // TODO Auto-generated constructor stub } }
add.jsp
<%@page import="com.jaovo.msg.Util.UserException"%> <%@page import="com.jaovo.msg.dao.ClassDaoImpl"%> <%@page import="com.jaovo.msg.model.Class"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <% //接收客户端传递过来的参数 String classname = request.getParameter("classname"); String teachername = request.getParameter("teachername"); String building = request.getParameter("building"); try{ if(!teachername.trim().equals("王建民")&&!teachername.trim().equals("刘立嘉")&&!teachername.trim().equals("刘丹")&&!teachername.trim().equals("王辉")&&!teachername.trim().equals("杨子光")) { throw new UserException("不能输入"); } else if (!building.trim().startsWith("基教")&&!building.trim().startsWith("一教")&&!building.trim().startsWith("二教")&&!building.trim().startsWith("三教")&&!building.trim().startsWith("四教")) { throw new UserException("不能输入"); } else { Class user = new Class(); user.setClassname(classname); user.setTeachername(teachername); user.setBuilding(building); ClassDaoImpl userDao = new ClassDaoImpl(); userDao.add(user); %> 课程保存成功!!<br> <a href="addInput.jsp">继续添加</a><br> <a href="#">课程列表</a> <% } } catch(UserException e){ %> <h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2> <% } %> </html>
addInput.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>课程登记页面</title> </head> <body> <%=request.getAttribute("error") %> <form action="add.jsp" method="get"> <table align="center" border="1" width="500"> <tr> <td>课程名称 : </td> <td> <input type="text" name="classname" /> </td> </tr> <tr> <td>教师名称:</td> <td> <input type="text" name="teachername" /> </td> </tr> <tr> <td>上课地点:</td> <td> <input type="text" name="building" /> </td> </tr> <tr align="center"> <td colspan="2"> <input type="submit" value="保存" /> </td> </tr> </table> </form> </body> </html>
3.截图:
4.
日期 |
开始时间 |
结束时间 |
中断时间 |
净时间 |
活动 |
备注 |
C |
U |
2017.11.28 |
8.00 |
8.50 |
|
50分钟 |
听课 |
|
√ |
|
|
9.00 |
9.50 |
|
50分钟 |
编程 |
|
√ |
|
|
13.30 |
14.40 |
|
70分钟 |
编程 |
|
√ |
|
|
15.00 |
15.50 |
|
50分钟 |
开会 |
学习十九大,班会 |
√ |
|
|
16.20 |
18.20 |
|
120分钟 |
编程,写报告 |
减肥,不吃饭,然后上选修 |
√ |
|
|
20.30 |
22.00 |
|
90分钟 |
编程 |
准备考试 |
|
|
任务 |
听课 |
编程 |
准备考试 |
总计时间(分钟) |
11.21 |
70 |
90 |
30 |
190 |
11.22 |
N |
40 |
20 |
60 |
11.23 |
N |
30 |
40 |
70 |
11.24 |
160 |
50 |
40 |
250 |
11.25 |
180 |
180 |
20 |
380 |
11.26 |
N |
60 |
0 |
60 |
11.27 |
N |
90 |
30 |
120 |
11.28 |
80 |
90 |
40 |
210 |
日期 |
编号 |
类型 |
引入阶段 |
修复阶段 |
缺陷描述 |
类型 |
11.28 |
1 |
20 |
编码 |
测试阶段 |
书写对象名时,写错了字母顺序 |
编写错误 |
|
2 |
20 |
编码 |
测试阶段 |
无法跳转, |
编写出错 |