课程管理
1.程序设计思想
⑴在类中定义连接MySQL的方法,实现添加课程信息的方法,以及关闭资源的方法。
⑵定义类,类中自己定义各种异常处理。
⑶在html文件中,绘制界面,对于任课教师以及上课地点的限制利用下拉菜单控制。
⑷在JSP文件中,先接受用户传递过来的参数,调用类中定义的添加课程信息的函数,成功,则 “显示添加课程成功!”。
2.源程序代码
实现连接MySQL数据库,
package Util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class AddCourse {
//信1705-1 20173522 李秦
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 = "chen123";
String url = "jdbc:mysql://localhost:3306/course";
Connection connection = null;
try {
//2 创建链接对象connection
connection = DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("连接失败!");
}
return connection;
}
public void add(String name,String teacher,String location) {
//获得链接对象
Connection connection = getConnection();
//准备sql语句
String sql = "select count(*) from course_1 where courseName = ?";
//创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, name);
//接收结果集
resultSet = preparedStatement.executeQuery();
//遍历结果集
while(resultSet.next()) {
if (resultSet.getInt(1) > 0) {
throw new UserException("用户已存在") ;
}
}
sql = "insert into course_1(courseName,teacherName,location) value (?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, name);
preparedStatement.setString(2, teacher);
preparedStatement.setString(3, location);
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//关闭资源
close(resultSet);
close(preparedStatement);
close(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 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
}
}
绘制界面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加课程信息</title>
</head>
<body>
<center>
<h1 style="color:black">添加课程信息</h1>
<form action="addcourse.jsp" method="get">
<table border="0">
<tr>
<td>课题名称</td>
<td>
<input type="text" maxlength="8" name="name">
</td>
</tr>
<tr>
<td>任课教师:</td>
<td>
<select name="teacher">
<option value="王建民">王建民</option>
<option value="刘立嘉">刘立嘉</option>
<option value="杨子光">杨子光</option>
<option value="刘丹">刘丹</option>
<option value="王辉">王辉</option>
</select>
</td>
</tr>
<tr>
<td>上课地点:</td>
<td>
<select name="point">
<option value="一教">一教</option>
<option value="二教">二教</option>
<option value="三教">三教</option>
<option value="基教">基教</option>
</select>
</td>
</tr>
</table>
</form>
<input type="button" value="保存" onclick="confir()">
</center>
</body>
</html>
<script language="javascript">
function confir(){
var n=document.forms[0].name.value;
if(n==""){
alert("课程名称输入为空!");
}
else{
document.forms[0].submit();
}
}
</script>
在界面中进行输入并添加信息
<%@page import="Util.AddCourse"%>
<%@ 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>
<%
request.setCharacterEncoding("UTF-8");
//接收客户传递过来的参数
String courseName = request.getParameter("courseName");
String teacherName = request.getParameter("teacherName");
String location = request.getParameter("location");
AddCourse add=new AddCourse();
try{
add.add(courseName, teacherName, location);
out.print("<script language='javaScript'> alert('添加课程成功!');</script>");
response.setHeader("refresh", "0;url=course.html");
}
catch(Exception e){
out.print("<script language='javaScript'> alert('"+e.getMessage()+"');</script>");
response.setHeader("refresh", "0;url=course.html");
}
%>
</body>
</html>
3.运行结果截图




浙公网安备 33010602011771号