java实现课表的增加
程序设计思想:
在login.jsp中创建一个登录界面里包括课程名称,任课老师,教学地点,设置三个文本框来接受输入的三个属性,
并传到另外的logininput.jsp页面中,又来接受三个数据,并判断传来的教师,与教室地点是否符合题目要求,若不符合,则设置
一个error,返回到第一个login.jsp中,并接受和输出到login.jsp界面,允许重新输入,若符合要求,就保存到数据库,实现1课表的添加。
源代码:
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 {
//1 鍔犺浇椹卞姩
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String user = "root";
String password = "";
String url = "jdbc:mysql://localhost:3306/jaovo_msg";
Connection connection = null;
try {
//2 鍒涘缓閾炬帴瀵硅薄connection
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.dao;
import java.util.List;
import com.jaovo.msg.model.User;
public interface IUserDao {
public void add(User user);
}
package com.jaovo.msg.model;
public class User {
private String classname;
private String teachername;
private String point;
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
public String getTeachername() {
return teachername;
}
public void setTeachername(String teachername) {
this.teachername = teachername;
}
public String getPoint() {
return point;
}
public void setPoint(String point) {
this.point = point;
}
}
package com.jaovo.msg.dao;
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.Util.UserException;
import com.jaovo.msg.model.User;
import sun.net.www.content.text.plain;
public class UserDaoImpl implements IUserDao {
@Override
public void add(User user) {
//获得链接对象
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
try
{
String sql = "insert into t_teacher(classname,teachername,point) value (?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, user.getClassname());
preparedStatement.setString(2, user.getTeachername());
preparedStatement.setString(3, user.getPoint());
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//关闭资源
//DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
}
}
<%@ 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>
<%
String loginError = (String)request.getAttribute("loginError");
if(loginError == null){
loginError = "";
}
%>
<%=loginError %><br>
<form action="logininput.jsp" method="get">
课程名称 : <input type="text" name="classname" /><br>
任课教师 : <input type="text" name="teachername" /><br>
上课地点: <input type="text" name="point" /><br>
<input type="submit" value="用户登录" />
</form>
</body>
</html>
<%@page import="com.jaovo.msg.dao.UserDaoImpl"%>
<%@page import="com.jaovo.msg.Util.UserException"%>
<%@page import="com.jaovo.msg.model.User"%>
<%@page import="com.jaovo.msg.Util.DaoFactory"%>
<%@page import="com.jaovo.msg.dao.IUserDao"%>
<%@ 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>
<%
String classname=request.getParameter("classname");
String teachername=request.getParameter("teachername");
String point=request.getParameter("point");
if(classname==null||"".equals(classname.trim()))
{
request.setAttribute("error", "课程名称不能为空!");
%>
<jsp:forward page="login.jsp"></jsp:forward>
<%
}
if(teachername==null||"".equals(teachername.trim()))
{
request.setAttribute("error", "任课教师不能为空!");
%>
<jsp:forward page="login.jsp"></jsp:forward>
<%
}
if(point==null||"".equals(point.trim()))
{
request.setAttribute("error", "上课地点不能为空!");
%>
<jsp:forward page="login.jsp"></jsp:forward>
<%
}
if(!teachername.trim().equals("王建民")&&!teachername.trim().equals("刘丹")
&&!teachername.trim().equals("刘立嘉")&&!teachername.trim().equals("王辉")
&&!teachername.trim().equals("杨子光"))
{
request.setAttribute("error", "老师不存在");
%>
<jsp:forward page="login.jsp"></jsp:forward>
<%
}
if(!point.trim().substring(0,2).equals("基教")
&&!point.trim().substring(0,2).equals("一教")
&&!point.trim().substring(0,2).equals("二教")
&&!point.trim().substring(0,2).equals("三教"))
{
request.setAttribute("error", "上课地点不存在!");
%>
<jsp:forward page="login.jsp"></jsp:forward>
<%
}
User user=new User();
user.setClassname(classname);
user.setTeachername(teachername);
user.setPoint(point);
UserDaoImpl UserDao=new UserDaoImpl();
UserDao.add(user);
%>
<h2 style="color:pink" align="center">课程信息保存成功</h2>
<form>
<table align="center" border="2" width="100">
<tr align="center">
<td colspan="2">
<a href="kechengjiemian.jsp"><u>继续添加</u></a>
</td>
</tr>
</table>
</form>
<%
%>
</html>
结果截图:
posted on 2017-11-28 20:04 xiaohaigege666 阅读(296) 评论(0) 编辑 收藏 举报