程序设计思想:

   在Input.jsp中创建一个表格里边分别是课程名称,任课老师,教学地点,并分别用三个文本框来接受输入的三个属性,

并传到另外的Jsp页面中,又来接受三个数据,并判断传来的教师,与教室地点是否符合题目要求,若不符合,则设置

一个Error,返回到第一个JSP中,并显示在JSP中,允许重新输入,若符合要求,就想数据库中保存输入的是三个属性。

程序源代码:

连接数据库的源代码:

复制代码
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 {
        
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String user = "sa";
        String password = "123456";
        String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=jaovo_msg";
        Connection connection = null;
        try {
            
             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();
        }
    }
    
}
复制代码

自己创建的类的源代码:

复制代码
package com.jaovo.msg.model;

public class User {
    
    
    private String  name;
    private String  teacher;
    private String  place;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    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;
    }
    
    
    
}
复制代码

向数据库添加数据的源代码:

复制代码
import com.jaovo.msg.Util.DBUtil;
//import com.jaovo.msg.Util.UserException;
import com.jaovo.msg.model.User;

public class UserDaoImpl 
{
    
    public void add(User user) {
        
        Connection connection = DBUtil.getConnection();
        


        PreparedStatement preparedStatement = null;

            try{
            String sql = "insert into t_teacher(name,teacher,place) values (?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getName());
            preparedStatement.setString(2, user.getTeacher());
            preparedStatement.setString(3, user.getPlace());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            
            //DBUtil.close(resultSet);
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
        
    }
    

    

}
复制代码

刚开始输入时的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>
<%
String s=(String)request.getAttribute("Error");
if("".equals(s)||s==null)
{
    s="";
}
%>
<%=s %>
<form action="LoginInput.jsp" method="get">
        <table align="center" border="1">
<tr>
<td colspan="2">
课程名称
 <input type="text" name="name"/>
</td>
</tr>
<tr>
   <td colspan="2">
    任课教师
 <input type="text" name="teacher"/>
   </td>
  </tr>
  <tr>
   <td colspan="2">
    上课地点
 <input type="text" name="place"/>
   </td>
  </tr>
 
  <tr>
  <td align="center" colspan="2">
   <input type="submit" value="保存"/></td>
  </tr>
  </table>
  </form>
</body>
</html>
复制代码

结束数据并判断是否符合要求的JSP:

复制代码
<%@page import="com.jaovo.msg.dao.UserDaoImpl"%>
<%@page import="com.jaovo.msg.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=UTF-8">
<%
         int i=0;
         boolean flag=true;
         boolean flag1=true;
         String s[]={"王建民","刘丹","刘立嘉","杨子光","王辉"};
         String s1[]={"基教","一教","二教","三教"};
         String name=request.getParameter("name");
         String teacher=request.getParameter("teacher");
         String place=request.getParameter("place");
        while(i<s.length)
         {
        if(!s[i].equals(teacher))
     {
      flag=false;
     }
        else
            {
            flag=true;
            break;
            }
      i++;
     }%>
     <%
     if(!flag)
     {
         request.setAttribute("Error", "老师不对");
     
     %>
     <jsp:forward page="Login.jsp"></jsp:forward>
       <% 
     }
     i=0;
     String s3=place.substring(0,2);
 
     while(flag==true&&i<s1.length)
     {
        
    if(!s1[i].equals(s3))
         {
             flag1=false;
          }
         else
        {
        flag1=true;
        break;
        }
          i++;
       }
     if(!flag1)
     {
         request.setAttribute("Error", "教室不对");
     
     %>
     <jsp:forward page="Login.jsp"></jsp:forward>
       <% 
     }
        UserDaoImpl userdao=new UserDaoImpl();
         User user=new User();
         user.setName(name);
         user.setTeacher(teacher);
         user.setPlace(place);
         userdao.add(user);
         System.out.println(teacher);
%>

<<jsp:forward page="Login.jsp"></jsp:forward>
复制代码

源程序截图: