Course.java
package nuc.select.course;
public class Course {
private String cno;
private String cname;
private String cteacher;
private int ccredit;
public String getCno(){
return cno;
}
public void setCno(String cno){
this.cno=cno;
}
public String getCname(){
return cname;
}
public void setCname(String cname){
this.cname=cname;
}
public String getCteacher(){
return cteacher;
}
public void setCteacher(String cteacher){
this.cteacher=cteacher;
}
public int getCcredit(){
return ccredit;
}
public void setCcredit(int ccredit){
this.ccredit=ccredit;
}
}
CouDao.java
package nuc.select.Dao;Coon.java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import nuc.select.coon.Coon;
import nuc.select.course.Course;
import nuc.select.student.Student;
public class CouDao {
public int Insert(Course course){
int rst=0;
Coon coursecoon=new Coon();
Connection ccoona=coursecoon.getCoon();
String sql_insert="insert into course(cno,cname,cteacher,ccredit) values (?,?,?,?)";
try {
PreparedStatement pst=ccoona.prepareStatement(sql_insert);
pst.setString(1,course.getCno());
pst.setString(2,course.getCname());
pst.setString(3,course.getCteacher());
pst.setInt(4,course.getCcredit());
rst=pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rst;
}
public int Delete(Course course){
int rst=0;
Coon coursecoon=new Coon();
Connection ccoona=coursecoon.getCoon();
String sql_delete="delete from course where cno=?";
try {
PreparedStatement pst=ccoona.prepareStatement(sql_delete);
pst.setString(1,course.getCno());
rst=pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rst;
}
public ResultSet Query(){
ResultSet rst=null;
Coon coursecoon=new Coon();
Connection ccoona=coursecoon.getCoon();
String sql_query="select * from course";
try {
PreparedStatement pst=ccoona.prepareStatement(sql_query);
rst=pst.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rst;
}
public int Updates(Course course){
int rst=0;
Coon coursecoon=new Coon();
Connection ccoona=coursecoon.getCoon();
String sql_update="update course set cname=?,cteacher=?,ccredit=? where cno=?";
try {
PreparedStatement pst=ccoona.prepareStatement(sql_update);
pst.setString(1,course.getCname());
pst.setString(2,course.getCteacher());
pst.setInt(3,course.getCcredit());
pst.setString(4,course.getCno());
rst=pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rst;
}
public ResultSet Select(Course course){
ResultSet rst=null;
Coon coursecoon=new Coon();
Connection ccoona=coursecoon.getCoon();
String sql_query="select * from course where cno=?";
try {
PreparedStatement pst=ccoona.prepareStatement(sql_query);
pst.setString(1,course.getCno());
rst=pst.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rst;
}
}
package nuc.select.coon;insertCourse.jsp
import java.sql.*;
public class Coon {
public static final String DBDRIVER="org.gjt.mm.mysql.Driver";
public static final String DBURL="jdbc:mysql://localhost:3306/select";
public static final String DBUSER="root";
public static final String DBPASS="*****";
Connection coon=null;
public Connection getCoon(){
try{
Class.forName(DBDRIVER);
coon=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
}catch(Exception e){
e.printStackTrace();
}
return coon;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"deleteCourse.jsp
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>
<form action="doinsertCourse.jsp" method="post">
课程号<input type="text" name="cno">
课程名<input type="text" name="cname">
老师<input type="text" name="cteacher">
学分<input type="text" name="ccredit">
<input type="submit" value="添加">
<input type="reset" value="重置">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"updateCourse.jsp
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="nuc.select.Dao.*" %>
<%@ page import="nuc.select.course.*" %>
<!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>
<%
CouDao cou=new CouDao();
Course coun=new Course();
coun.setCno(request.getParameter("cno"));
int rs=cou.Delete(coun);
if(rs>0){
out.println("删除成功!");
}
else{
out.println("删除失败!");
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"doupdateCourse.jsp
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="nuc.select.Dao.*" %>
<%@ page import="nuc.select.course.*" %>
<!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>
<%
request.setCharacterEncoding("utf-8");
CouDao cou=new CouDao();
Course coun=new Course();
coun.setCno(request.getParameter("cno"));
ResultSet rs=cou.Select(coun);
if(rs.next()){
%>
<form action="doupdateCourse.jsp?cno=<%=rs.getString("cno") %>" method="post">
课程号<input type="text" name="cno" value="<%=rs.getString("cno") %>">
课程名<input type="text" name="cname" value="<%=rs.getString("cname") %>">
老师<input type="text" name="cteacher" value="<%=rs.getString("cteacher") %>">
学分<input type="text" name="ccredit" value="<%=rs.getString("ccredit") %>">
<input type="submit" value="修改">
<input type="reset" value="取消">
</form>
<%} %>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"queryCourse.jsp
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="nuc.select.course.*" %>
<%@ page import="nuc.select.Dao.*" %>
<!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>
<% request.setCharacterEncoding("utf-8"); %>
<jsp:useBean id="cour" class="nuc.select.course.Course">
<jsp:setProperty name="cour" property="*" />
</jsp:useBean>
<%
CouDao cou=new CouDao();
Course coun=new Course();
coun.setCno(request.getParameter("cno"));
int rs=cou.Updates(cour);
if(rs>0){
out.println("修改成功!");
}else{
out.println("修改失败!");
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="nuc.select.course.*" %>
<%@ page import="nuc.select.Dao.*" %>
<!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>
<style type="text/css">
td{
text-align:center;
}
a{
text-decoration:none;
}
</style>
</head>
<body>
<center>
<%
CouDao cou=new CouDao();
Course coun=new Course();
ResultSet rs=cou.Query();
%>
<table border="1" width="500" height="500">
<caption>课程信息</caption>
<tr><td>课程号</td><td>课程名</td><td>老师</td><td>学分</td><td colspan="2">数据操作</td></tr>
<%
while(rs.next()){
%>
<tr>
<td><%=rs.getString("cno") %></td>
<td><%=rs.getString("cname") %></td>
<td><%=rs.getString("cteacher") %></td>
<td><%=rs.getString("ccredit") %></td>
<td><a href="deleteCourse.jsp?cno=<%=rs.getString("cno") %>">删除</a></td>
<td><a href="updateCourse.jsp?cno=<%=rs.getString("cno") %>">修改</a></td>
</tr>
<%
}
%>
</table>
<a href="insertCourse.jsp" style="text-decoration:none;">添加</a>
</center>
</body>
</html>