用到了EL和JSTL
Course.java
package javabean;
public class Course {
private int id;
private String name;
private String teacher;
private String classroom;
public Course() {
}
public Course(String name, String teacher, String classroom) {
super();
this.name = name;
this.teacher = teacher;
this.classroom = classroom;
}
public Course(int id, String name, String teacher, String classroom) {
super();
this.id = id;
this.name = name;
this.teacher = teacher;
this.classroom = classroom;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 getClassroom() {
return classroom;
}
public void setClassroom(String classroom) {
this.classroom = classroom;
}
}
DBUtil.java
package Util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil {
private static String driverName="com.mysql.jdbc.Driver";
private static String userName="root";
private static String userPwd="1234";
private static String dbName="course";
private static String url="jdbc:mysql://localhost:3306/"+dbName+"?useUnicode=true&characterEncoding=utf-8";
//获取数据库连接
public static Connection getConnection() {
Connection conn=null;
try {
Class.forName(driverName);
conn=DriverManager.getConnection(url,userName,userPwd);
} catch (Exception e) {
System.out.println("获取连接失败!");
}
return conn;
}
//关闭连接
public static void close(Connection conn,Statement stmt) {
if(stmt!=null) {
try {
stmt.close();
} catch (SQLException e) {
System.out.println("关闭失败!");
}
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
System.out.println("关闭失败!");
}
}
}
//关闭连接
public static void close(Connection conn,Statement stmt,ResultSet rs) {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
System.out.println("关闭失败!");
}
}
if(stmt!=null) {
try {
stmt.close();
} catch (SQLException e) {
System.out.println("关闭失败!");
}
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
System.out.println("关闭失败!");
}
}
}
}
CourseDao.java
package Dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import Util.DBUtil;
import javabean.Course;
//与数据库进行交流
public class CourseDao {
//添加
public boolean add(Course course) {
Connection conn=DBUtil.getConnection();
Statement stmt=null;
String sql="insert into course(name,teacher,classroom) values('"+course.getName()+"','"+course.getTeacher()+"','"+course.getClassroom()+"')";
int k=0;
try {
stmt=conn.createStatement();
k=stmt.executeUpdate(sql);
} catch (SQLException e) {
System.out.println("添加操作失败!");
}finally{
DBUtil.close(conn, stmt);
}
if(k>0) {
return true;
}else {
return false;
}
}
//删除
public boolean delete(int id) {
Connection conn=DBUtil.getConnection();
Statement stmt=null;
String sql="delete from course where id="+id;
int k=0;
try {
stmt=conn.createStatement();
k=stmt.executeUpdate(sql);
} catch (SQLException e) {
System.out.println("删除操作失败!");
}finally {
DBUtil.close(conn, stmt);
}
if(k>0) {
return true;
}else {
return false;
}
}
//修改
public boolean update(Course course) {
Connection conn=DBUtil.getConnection();
Statement stmt=null;
String sql="update course set name='"+course.getName()+"',teacher='"+course.getTeacher()+"',classroom='"+course.getClassroom()+"' where id="+course.getId();
int k=0;
try {
stmt=conn.createStatement();
k=stmt.executeUpdate(sql);
} catch (SQLException e) {
System.out.println("修改操作失败!");
}finally {
DBUtil.close(conn, stmt);
}
if(k>0) {
return true;
}else {
return false;
}
}
//验证课程名称是否唯一
//true代表唯一
//false代表不唯一
public boolean name(String name) {
boolean flag=true;
Connection conn=DBUtil.getConnection();
Statement stmt=null;
ResultSet rs=null;
String sql="select name from course where name='"+name+"'";
try {
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()) {
flag=false;
}
} catch (SQLException e) {
System.out.println("验证姓名是否唯一操作失败!");
}finally {
DBUtil.close(conn, stmt, rs);
}
return flag;
}
//通过id得到课程信息
public Course getCourseById(