设计思想
三个文件Class_add.java add.jsp addInput.jsp
Class_add.java : 内封装方法:连接数据库、向数据库添加课程信息、判断非合理的输入情况、判断添加的课程是否重复。
addInput.jsp : 完成显示输入界面,点击保存后跳转到addInput.jsp。
add.jsp : 处理输入的数据,并给出相应的交互提示。
源程序代码
Class_add.java :
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class Classe_add { private String className; private String teacher; private String classSpace; 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 getClassSpace() { return classSpace; } public void setClassSpace(String classSpace) { this.classSpace = classSpace; } public Connection getConnection() { try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); }catch(ClassNotFoundException e) { e.printStackTrace(); } String user="SYH"; String password="q735415630q"; String url ="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Test"; Connection con=null; try{ con=DriverManager.getConnection(url,user,password); }catch(SQLException e) { e.printStackTrace(); } return con; } public void addClass(String className,String teacher,String classSpace ) { int judge=1; Connection connection = getConnection(); String sql="select count(*) from class_add where className = ?"; PreparedStatement preparedStatement=null; ResultSet resultSet=null; try { preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1, className); resultSet = preparedStatement.executeQuery(); while(resultSet.next()) { if (resultSet.getInt(1) > 0) { System.out.println("课程已存在,无法添加") ; judge=0; } } if(judge==1) { sql = "insert class_add (className,teacher,classSpace) values (?,?,?)"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1,className); preparedStatement.setString(2,teacher); preparedStatement.setString(3,classSpace); preparedStatement.executeUpdate(); System.out.println("添加成功") ; } } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } finally { close(resultSet); close(preparedStatement); close(connection); } } public void close (Connection con) { try{ if(con!=null) { con.close(); } }catch(SQLException e) { e.printStackTrace(); } } public void close (PreparedStatement preparedStatement) { try{ if(preparedStatement!=null) { preparedStatement.close(); } }catch(SQLException e) { e.printStackTrace(); } } public void close(ResultSet resultSet) { try{ if(resultSet!=null) { resultSet.close(); } }catch(SQLException e) { e.printStackTrace(); } } public boolean judgeInputNull(String className,String teacher,String classSpace) { boolean flag=false; if(className==null||"".equals(className)&&teacher==null||"".equals(teacher)&&classSpace==null||"".equals(classSpace)) { flag=true; } return flag; } public boolean judgeClassRepeat(String className,String teacher,String classSpace) { boolean flag=false; Connection connection = getConnection(); String sql="select * from class_add where className = ?"; PreparedStatement preparedStatement=null; ResultSet resultSet=null; try { preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1, className); resultSet = preparedStatement.executeQuery(); while(resultSet.next()) { if(teacher.equals(resultSet.getString("teacher"))&&classSpace.equals(resultSet.getString("classSpace"))) { flag=true; } } }catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { close(resultSet); close(preparedStatement); close(connection); } return flag; } public boolean judeInputTeacher(String teacher) { boolean flag=false; if((!teacher.equals("王建民"))&&!teacher.equals("刘立嘉")&&!teacher.equals("刘丹")&&!teacher.equals("王辉")&&!teacher.equals("杨子光")) { flag=true; } return flag; } public boolean judgeInputClassSpace(String space) { boolean flag=false; if(!space.startsWith("一教")&&!space.startsWith("二教")&&!space.startsWith("三教")&&!space.startsWith("基教")) { flag=true; } return flag; } public static void main(String[] args) { Classe_add a=new Classe_add(); System.out.println(a.judgeClassRepeat("软件工程", "王建民", "基教405")); } }
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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>课程添加页面 </title> </head> <body background="C:\Users\win8\workspace\Arithmetic\image\d602adc701a429f16a32aac5579f113e_7205982_162234868593_2.jpg"> <center style="color:white;font-family:楷体; font-size:20px"> <form action="add.jsp" method="get"> <br> 课程名称: <input type="text" name="className" /> <br><br> 任课教师: <input type="text" name="teacher" /> <br><br> 上课地点: <input type="text" name="classSpace" /> <br><br> <input type="submit" value="保存" /> <input type="reset" value="重置" /> </form> </center> </body> </html>
add.jsp :
<%@page import="util.Classe_add"%> <%@ 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=utf-8"> <title>Insert title here</title> </head> <body> <jsp:useBean id="util" class="util.Classe_add" scope="page" /> <% Classe_add a=new Classe_add(); String className=(String)request.getParameter("className"); String teacher=(String)request.getParameter("teacher"); String classSpace=(String)request.getParameter("classSpace"); if(util.judgeInputNull(className, teacher, classSpace)) { out.print("<script language =‘javaScript‘> alert(‘输入不能为空,请输入‘);window.history.back(-1); </script>"); //response.setHeader("refresh", "0;url=addInput.jsp"); } else if(util.judeInputTeacher(teacher)) { out.print("<script language=‘javaScript‘> alert(‘该老师不存在,重新输入‘);window.history.back(-1);</script>"); // response.setHeader("refresh", "0;url=addInput.jsp"); } else if(util.judgeInputClassSpace(classSpace)) { out.print("<script language=‘javaScript‘> alert(‘教室不存在,重新输入‘);window.history.back(-1);</script>"); //response.setHeader("refresh", "0;url=addInput.jsp"); } else if(util.judgeClassRepeat(className, teacher, classSpace)) { out.print("<script language=‘javaScript‘> alert(‘该课程已存在‘);window.history.back(-1);</script>"); // response.setHeader("refresh", "0;url=addInput.jsp"); } else { util.addClass(className,teacher,classSpace); out.print("<script language=‘javaScript‘> alert(‘添加成功‘);</script>"); response.setHeader("refresh", "0;url=addInput.jsp"); } %> </body> </html>
运行结果截图
界面:
什么都不输入:
正常输入;
重复输入:
PSP0级记录开发过程
项目计划日志
时间记录日志
缺陷记录日志
时间: 2024-10-27 18:41:07