1,程序设计思想
(1)设计三个类,分别是工具类(用来写连接数据库的方法和异常类的方法)、信息类(用来写存储信息的方法)、实现类(用来写各种操作数据库的方法)
(2)定义两个jsp文件,一个用来写入数据,另一个用来读取数据
(3)先在一个jsp文件中写入数据,然后另一个jsp用来读取数据,同时做出判断,判断读取的信息与数据库的信息是否相同或是数据库中没有该数据,将判断后的信息显示出来
2,源程序代码
(1)工具类
package Util; import java.sql.*; public class DBUtil { public static Connection getConnection() { try { //1 加载驱动 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } String user1 = "sa"; String password = "123456"; String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=user"; Connection connection = null; try { //2 创建链接对象connection connection = DriverManager.getConnection(url,user1,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(); } } }
(2)信息类
package model; public class User { String className; String teacher; String place; public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public String getTeacher() { return teacher; } public void setTeacher(String teacher) { this.teacher = teacher; } public String getPlace() { return place; } public void setPlace(String place) { this.place = place; } }
(3)实现类
package dao; import java.sql.*; import Util.DBUtil; import model.User; public class UserDaoImpl { public void add(User equ) { Connection connection = DBUtil.getConnection(); try { String sql="insert into teacher(className,teacher,place)values('"+equ.getClassName()+"','"+equ.getTeacher()+"','"+equ.getPlace()+"')"; Statement stmt=connection.createStatement(); stmt.executeUpdate(sql);//更新 } catch (SQLException e) { e.printStackTrace(); }finally { DBUtil.close(connection); } } public User load(String className) { Connection connection = DBUtil.getConnection(); //准备sql语句 String sql = "select * from teacher where className = ?"; //创建语句传输对象 PreparedStatement preparedStatement = null; ResultSet resultSet = null; User user = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, className); resultSet = preparedStatement.executeQuery(); while(resultSet.next()) { user = new User(); user.setClassName(className); user.setTeacher(resultSet.getString("teacher")); user.setPlace(resultSet.getString("place")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { DBUtil.close(connection); } return user; } }
(4)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> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>登录界面</title> </head> <body background="D:图片动漫20150422H2607_fKHLB.jpeg"> <%if(request.getAttribute("error1")!=null){ out.print("<script language='javaScript'>alert('该信息已经存在'); </script>)"); } %> <%if(request.getAttribute("error2")!=null){ out.print("<script language='javaScript'>alert('请正确输入完整信息'); </script>)"); } %> <%if(request.getAttribute("成功")!=null){ out.print("<script language='javaScript'> alert('保存成功'); </script>)"); } %> <%if(request.getAttribute("error3")!=null){ out.print("<script language='javaScript'> alert('教室输入错误'); </script>)"); } %> <%if(request.getAttribute("error4")!=null){ out.print("<script language='javaScript'> alert('老师输入错误'); </script>)"); } %> <form action="add.jsp" method="get"> <table align="center" border="4" width="400"> <h4 align="center">登录界面</h4> <tr> <td>课程名称: </td> <td> <input type="text" name="className" /> </td> </tr> <tr> <td>任课老师: </td> <td> <input type="text" name="teacher" /> </td> </tr> <tr> <td>上课地点:</td> <td> <input type="text" name="place" /> </td> </tr> <tr align="center"> <td colspan="2"> <input type="submit" value="保存" /> </td> </tr> </table> </form> </body> </html>
(5)jsp文件 读取数据
<%@page import="dao.UserDaoImpl"%> <%@page import="model.User"%> <%@ 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> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% String className = request.getParameter("className"); String place = request.getParameter("place"); String teacher=request.getParameter("teacher"); User user = new User(); UserDaoImpl userDao = new UserDaoImpl(); user.setClassName(className); user.setTeacher(teacher); user.setPlace(place); if(userDao.load(className)!=null||userDao.load(place)!=null||userDao.load(teacher)!=null){ request.setAttribute("error1", "该信息已经存在"); %> <jsp:forward page="addInput.jsp"></jsp:forward> <% } %> <% if("".equals(teacher.trim())||"".equals(className.trim())||"".equals(place.trim())){ request.setAttribute("error2", "请正确输入完整信息"); %> <jsp:forward page="addInput.jsp"></jsp:forward> <% } %> <% if(userDao.load(className)==null&&((teacher).equals("王建民")||(teacher).equals("刘丹")||(teacher).equals("刘立嘉")||(teacher).equals("王辉")||(teacher).equals("杨子光")&&(((place).substring(0,2)).equals("基教")))){ { userDao.add(user); request.setAttribute("成功", "保存成功"); } %> <jsp:forward page="addInput.jsp"></jsp:forward> <% } if(userDao.load(className)==null&&(!place.startsWith("基教"))&&(!place.startsWith("一教"))&&(!place.startsWith("二教")&&(!place.startsWith("三教")))){ request.setAttribute("error3", "教室输入错误"); %> <jsp:forward page="addInput.jsp"></jsp:forward> <% }else{ request.setAttribute("error4","老师输入错误"); %> <jsp:forward page="addInput.jsp"></jsp:forward> <% } %> </body> </html>
3,运行结果截图
如果不输入信息
若输入已存在的信息
若输入错误教室信息
多输入错误的老师信息
4,开发过程的日志
项目计划日志
时间 任务 |
课堂测试 |
课下编写程序 |
阅读书籍 |
半天总计 |
|
上午 |
50分钟 |
50分钟 |
|||
下午 |
3个小时 |
20分钟 |
3小时20分钟 |
时间记录日志
时间段 |
开始 |
结束 |
中断时间 |
净时 |
活动 |
备注 |
C |
U |
上午 |
9:00 |
9:50 |
0 |
50分钟 |
编程序 |
课堂测试 |
||
下午 |
12:30 |
4:30 |
1个小时 |
3个小时 |
编程序 |
作业 |
缺陷记录日志
日期 |
编号 |
类型 |
引入阶段 |
排除阶段 |
修复时间 |
修复缺陷 |
|
11/28 |
1 |
粗心 |
运行 |
编译 |
2分钟 |
||
描述:无法将数据传进数据库,表单名写错 |
|||||||
2 |
功能不完善 |
运行 |
编译 |
2小时 |
|||
描述:判断数据库与输入的数据的匹配,String类型的数据的截取方法 如substring(a,b) startsWith(“”) |
|||||||
3 |
界面不美观 |
运行 |
编译 |
30分钟 |
|||
描述:在jsp中利用alert弹出信息 |