程序設計思想:
在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>
源程序截圖: