设计思想:
首先建立一个User类,用于存储教师姓名,课程,以及教室名称,并且写上赋值和取值方法。
然后建立一个工具类用于连接数据库、
建立一个操作类用于访问数据库,对数据库进行添加,遍历。
最后建立一个Tianbiao.jsp,Baocun.jsp俩个jsp文件,Tianbiao.jsp用于输出表,用户输入信息,Baocun.jsp用于判断输入的信息是否符合要求或者是否已经存在。
源代码:
package com.jaovo.msg.Model;
public class User {
private String Username;
private String Kecheng;
private String Didian;
public String getUsername() {
return Username;
}
public void setUsername(String username) {
Username = username;
}
public String getKecheng() {
return Kecheng;
}
public void setKecheng(String kecheng) {
Kecheng = kecheng;
}
public String getDidian() {
return Didian;
}
public void setDidian(String didian) {
Didian = didian;
}
}
工具类:
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 e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String user = "sa";
String password = "zlq521415";
String url = "jdbc:";// 数据源
Connection con =null;
try {
con = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// 连接数据库对象
return con;
}
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.Util;
public class UserException extends RuntimeException{
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
//代码效果参考:http://www.jhylw.com.cn/222334219.html
}}
package com.jaovo.msg.Util;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
public class UtilEmpty {
public static boolean validateNull(HttpServletRequest request,String【】 fileds) {
boolean validate = true;
//map对象用来装载不同的错误信息
Map errorMsg = new HashMap();
for(String filed :fileds) {
String value = request.getParameter(filed);
if (value == null || "".equals(value.trim())) {
validate = false;
errorMsg.put(filed, filed+"不能为空");
}
if (!validate) {
request.setAttribute("errormsg", errorMsg);
}
}
return validate;
}
public static String showError(HttpServletRequest request , String filed) {
Map errorMsg = (Map)request.getAttribute("errormsg");
if (errorMsg == null) {
return "";
}
String msg = errorMsg.get(filed);
if (msg == null) {
return "";
}
return msg;
}
}
操作类:
package com.jaovo.msg.Dao;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.jaovo.msg.Util.DBUtil;
import com.jaovo.msg.Model.User;
public class UserDao extends User {
public void addMessage(User user) {
//获得连接对象
Connection connection = DBUtil.getConnection();
//创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//遍历结果集
String sql="insert into teacher (Kecheng,Username,Didian) values (?,?,?)";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1, user.getKecheng());
preparedStatement.setString(2, user.getUsername());
preparedStatement.setString(3, user.getDidian());
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭资源
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
}
public boolean load(User user) {
boolean jud=true;
//获得连接对象
Connection connection = DBUtil.getConnection();
//准备sql语句
String sql = "select * from teacher where Username=?";
//创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet=null;
try {
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1, user.getUsername());
resultSet=preparedStatement.executeQuery();
while(resultSet.next())
{
if((user.getUsername().equals(resultSet.getString("Username")))&&(user.getDidian().equals(resultSet.getString("Didian")))&&(user.getKecheng().equals(resultSet.getString("Kecheng"))))
{jud=false;}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
return jud;
}
public static boolean validate(User user)
{
boolean validate = false;
String jiaoshi【】={"王建民","刘丹","刘立嘉","王辉"};
for(int i=0;i<4;i++)
{
if(user.getUsername().equals(jiaoshi【i】))
validate=true;
}
return validate;
}
public static boolean validate1(User user) {
boolean validate = false;
String didian【】={"基教","一教","二教","三教"};
for(int i=0;i<4;i++)
{
if(user.getDidian().startsWith(didian【i】))
validate=true;
}
return validate;
}
}
<%@page import="com.jaovo.msg.Dao.UserDao"%>
<%@page import="com.jaovo.msg.Util.UtilEmpty"%>
<%@page import="com.jaovo.msg.Util.UserException"%>
<%@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" ""
[/span>html
[/span>head
[/span>meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
[/span>title
[/span>body background="C:\Users\张强\Pictures\Saved Pictures/1505615532655.jpg"
<%
String kecheng = request.getParameter("kecheng<span style="background-color: rgba(245, 245,