package kecheng
Kc.java
1 package kecheng; 2 3 public class Kc { 4 private int id; 5 private String classname; 6 private String teachername; 7 private String place; 8 public String getClassname() { 9 return classname; 10 } 11 public void setClassname(String classname) { 12 this.classname = classname; 13 } 14 public int getId() { 15 return id; 16 } 17 public void setId(int id) { 18 this.id = id; 19 } 20 public String getTeachername() { 21 return teachername; 22 } 23 public void setTeachername(String teachername) { 24 this.teachername = teachername; 25 } 26 public String getPlace() { 27 return place; 28 } 29 public void setPlace(String place) { 30 this.place = place; 31 } 32 33 }
Mysqlopear.java
1 package kecheng; 2 3 import java.sql.*; 4 5 6 public class Mysqlopear { 7 private String driverStr="com.mysql.cj.jdbc.Driver"; 8 private String connStr="jdbc:mysql://127.0.0.1:3306/db_database17?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT"; 9 private String dbusername="root"; 10 private String dbpassword="123456"; 11 private Connection conn=null; 12 private Statement stmt=null; 13 public Connection getCon() { 14 try { 15 Class.forName(driverStr); 16 conn=DriverManager.getConnection(connStr, dbusername, dbpassword); 17 return conn; 18 } catch (Exception e) { 19 System.out.println("数据连接失败!"); 20 System.out.println(e.getMessage()); 21 return null; 22 } 23 } 24 public int executeUpdate(String s) { 25 int result=0; 26 System.out.println("更新语句:"+s+" "); 27 try { 28 result=stmt.executeUpdate(s); 29 }catch(Exception e) { 30 System.out.println("执行更新错误!"); 31 } 32 return result; 33 } 34 35 //查询操作 36 public ResultSet executeQuery(String s) { 37 ResultSet rs=null; 38 System.out.println("查询语句:"+s+" "); 39 try { 40 rs=stmt.executeQuery(s); 41 while(rs.next()) { 42 System.out.println("课程名称:"+rs.getString(1)+" "+"教师名称:"+rs.getString(2)+" "+"教室位置:"+rs.getString(3)); 43 } 44 }catch(Exception e) { 45 System.out.println("执行查询错误!"); 46 } 47 return rs; 48 } 49 public void close() { 50 try { 51 stmt.close(); 52 conn.close(); 53 }catch(Exception e) { 54 } 55 } 56 57 }
package service
Kcservice.java
1 package service; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.util.ArrayList; 8 import java.util.List; 9 import kecheng.Kc; 10 11 12 public class Kcservice { 13 private Connection conn; 14 private PreparedStatement pstmt; 15 public Kcservice() { 16 conn=new kecheng.Mysqlopear().getCon(); 17 } 18 19 //录入部分 20 public boolean addStu(Kc kc){ 21 try { 22 pstmt=conn.prepareStatement("insert into kc" 23 +"(classname,teachername,place)" 24 +"values(?,?,?)"); 25 pstmt.setString(1, kc.getClassname()); 26 pstmt.setString(2, kc.getTeachername()); 27 pstmt.setString(3, kc.getPlace()); 28 pstmt.executeUpdate(); 29 return true; 30 } catch (SQLException e) { 31 e.printStackTrace(); 32 return false; 33 } 34 } 35 public Kc queryStuByID(int id) { 36 try { 37 pstmt=conn.prepareStatement("select * from kc where id=?"); 38 pstmt.setInt(1, id); 39 ResultSet rs=pstmt.executeQuery(); 40 if(rs.next()) { 41 Kc stu=new Kc(); 42 stu.setId(rs.getInt(1)); 43 stu.setClassname(rs.getString(2)); 44 stu.setTeachername(rs.getString(3)); 45 stu.setPlace(rs.getString(4)); 46 return stu; 47 } 48 return null; 49 } 50 catch(SQLException e) { 51 e.printStackTrace(); 52 return null; 53 } 54 } 55 public List queryAllStu() { 56 List stus=new ArrayList(); 57 try { 58 pstmt=conn.prepareStatement("select * from kc"); 59 ResultSet rs=pstmt.executeQuery(); 60 while(rs.next()) { 61 Kc stu=new Kc(); 62 stu.setId(rs.getInt(1)); 63 stu.setClassname(rs.getString(2)); 64 stu.setTeachername(rs.getString(3)); 65 stu.setPlace(rs.getString(4)); 66 stus.add(stu); 67 } 68 return stus; 69 } 70 catch(SQLException e) { 71 e.printStackTrace(); 72 return null; 73 } 74 } 75 public boolean updateStu(Kc kc) { 76 try { 77 pstmt=conn.prepareStatement("update kc set classname=?,teachername=?,place=? where id=? "); 78 pstmt.setString(1, kc.getClassname()); 79 pstmt.setString(2, kc.getTeachername()); 80 pstmt.setString(3, kc.getPlace()); 81 pstmt.setInt(4, kc.getId()); 82 pstmt.executeUpdate(); 83 return true; 84 }catch(Exception e) { 85 e.printStackTrace(); 86 return false; 87 } 88 } 89 //删除部分 90 public boolean deleteStu(int id) { 91 try { 92 pstmt=conn.prepareStatement("delete from kc where id=?"); 93 pstmt.setInt(1, id); 94 pstmt.executeUpdate(); 95 return true; 96 }catch(Exception e) { 97 e.printStackTrace(); 98 return false; 99 } 100 } 101 102 }
框架:main.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <frameset rows="18%,*"> 10 <frame src="title.jsp"> 11 <frameset cols="10%,*"> 12 <frame src="main_left.jsp"> 13 <frame src="main_right.jsp" name="main_right"> 14 </frameset> 15 <body> 16 17 </body> 18 </html>
左侧菜单:main_left.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <a href="tianjia.jsp" target="main_right">录入学生信息</a><br/> 11 <a href="chaxun.jsp" target="main_right">查看学生信息</a><br/> 12 <a href="xiugai.jsp" target="main_right">修改学生信息</a><br/> 13 <a href="shanchu.jsp" target="main_right">删除学生信息</a><br/> 14 </body> 15 </html>
右侧界面:main_right.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 11 </body> 12 </html>
标题:title.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>石家庄铁道大学课程信息管理系统</title> 8 9 </head> 10 <body> 11 <center> 12 <h1>石家庄铁道大学课程信息管理系统</h1> 13 </center> 14 </body> 15 </html>
添加新课程:tianjia.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>添加新课程页</title> 8 </head> 9 <body> 10 <h1 style=color:red>添加新课程</h1> 11 <form action="tj_result.jsp"method="post"> 12 <table> 13 14 <tr> 15 <td>课程名称:</td> 16 <td><input type="text"name="classname"></td> 17 </tr> 18 <tr> 19 <td>任课教师:</td> 20 <td><input type="text"name="teachername"></td> 21 </tr> 22 <tr> 23 <td>上课地点:</td> 24 <td><input type="text"name="place"></td> 25 </tr> 26 </table> 27 <br> 28 <input type="submit" value="保存"> 29 </form> 30 </body> 31 </html>
添加处理:tj_result.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>添加处理</title> 8 </head> 9 <body> 10 <% request.setCharacterEncoding("utf-8"); %> 11 <jsp:useBean id="kc" class="kecheng.Kc" ></jsp:useBean> 12 <jsp:useBean id="kcservice" class="service.Kcservice"></jsp:useBean> 13 <jsp:setProperty property="*" name="kc"/> 14 <% 15 if(!(kc.getTeachername().equals("王建民")||kc.getTeachername().equals("刘丹") 16 ||kc.getTeachername().equals("刘立嘉")||kc.getTeachername().equals("王辉") 17 ||kc.getTeachername().equals("杨子光"))){ 18 out.print("<script language='javaScript'> alert('教师姓名非法');</script>"); 19 response.setHeader("refresh", "0;url=tianjia.jsp"); 20 } 21 else{ 22 if(kcservice.addStu(kc)) 23 out.print("success"); 24 else 25 out.print("error");} 26 %> 27 </body> 28 </html>
删除:shanchu.jsp
1 <%@page import="kecheng.Kc"%> 2 <%@page import="java.util.Iterator"%> 3 <%@page import="java.util.List"%> 4 <%@ page language="java" contentType="text/html; charset=UTF-8" 5 pageEncoding="UTF-8"%> 6 <!DOCTYPE html> 7 <html> 8 <head> 9 <meta charset="UTF-8"> 10 <title>删除课程</title> 11 </head> 12 <body> 13 <jsp:useBean id="stuSer" class="service.Kcservice"></jsp:useBean> 14 <% 15 List stus=stuSer.queryAllStu(); 16 //out.print(stus.size()); 17 Iterator iter=stus.iterator(); 18 %> 19 <table> 20 <tr> <td>ID </td> <td>课程姓名 </td> <td>教师姓名</td><td>上课地点</td> </tr> 21 22 <% 23 int i=0; 24 while(iter.hasNext()) 25 { 26 Kc kc=(Kc)iter.next(); 27 %> 28 <tr <%if(i%2==0){ %>bgcolor="#F0F8FF"<%} %>> 29 <td> <%=kc.getId() %></td> 30 <td> <%=kc.getClassname() %></td> 31 <td> <%=kc.getTeachername()%></td> 32 <td> <%=kc.getPlace()%></td> 33 <td> <a href="sc_result.jsp?id=<%=kc.getId() %>">删除</a></td> 34 </tr> 35 <% 36 i++; 37 } 38 %> 39 </table> 40 </body> 41 </html>
删除处理:sc_result.jsp
1 <%@page import="kecheng.Kc"%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>删除处理</title> 9 </head> 10 <body> 11 <jsp:useBean id="stuSer" class="service.Kcservice"></jsp:useBean> 12 <% 13 int id=Integer.parseInt(request.getParameter("id")); 14 Kc stu=stuSer.queryStuByID(id); 15 if(stuSer.deleteStu(id)) 16 out.print("success"); 17 else 18 out.print("false"); 19 %> 20 </body> 21 </html>
修改:xuigai.jsp
1 <%@page import="kecheng.Kc"%> 2 <%@page import="java.util.Iterator"%> 3 <%@page import="java.util.List"%> 4 <%@ page language="java" contentType="text/html; charset=UTF-8" 5 pageEncoding="UTF-8"%> 6 <!DOCTYPE html> 7 <html> 8 <head> 9 <meta charset="UTF-8"> 10 <title>修改</title> 11 </head> 12 <body> 13 <jsp:useBean id="stuSer" class="service.Kcservice"></jsp:useBean> 14 <% 15 List stus=stuSer.queryAllStu(); 16 Iterator iter=stus.iterator(); 17 %> 18 <table> 19 <tr> <td>ID </td> <td>课程姓名 </td> <td>教师姓名</td><td>上课地点</td> </tr> 20 21 <% 22 int i=0; 23 while(iter.hasNext()) 24 { 25 Kc kc=(Kc)iter.next(); 26 %> 27 <tr <%if(i%2==0){ %>bgcolor="#F0F8FF"<%} %>> 28 <td> <%=kc.getId() %></td> 29 <td> <%=kc.getClassname() %></td> 30 <td> <%=kc.getTeachername()%></td> 31 <td> <%=kc.getPlace()%></td> 32 <td> <a href="修改处理.jsp?id=<%=kc.getId() %>">修改</a></td> 33 </tr> 34 <% 35 i++; 36 } 37 %> 38 </table> 39 </body> 40 </html>
修改处理:xg_result.jsp
1 <%@page import="kecheng.Kc"%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>修改处理</title> 9 </head> 10 <body> 11 <jsp:useBean id="stuSer" class="service.Kcservice"></jsp:useBean> 12 <% 13 int id=Integer.parseInt(request.getParameter("id")); 14 Kc stu=stuSer.queryStuByID(id); 15 %> 16 <form action="xg_0.jsp" method="post"> 17 <table> 18 <tr><td colspan="2">修改学生信息</td></tr> 19 <tr> 20 <td><input type="hidden" name="id" value="<%=stu.getId()%>"/></td> 21 </tr> 22 <tr> 23 <td>课程名称</td> 24 <td><input type="text" name="classname" value="<%=stu.getClassname()%>"/></td> 25 </tr> 26 <tr> 27 <td>教师姓名</td> 28 <td><input type="text" name="teachername" value="<%=stu.getTeachername()%>"/></td> 29 </tr> 30 <tr> 31 <td>上课地点</td> 32 <td><input type="text" name="place" value="<%=stu.getPlace()%>"/></td> 33 </tr> 34 <tr> 35 <td align="center" colspan="2"> 36 <input type="submit" value="提交"> 37 <input type="reset" value="重置"> 38 39 </tr> 40 </table> 41 </form> 42 </body> 43 </html>
修改结果:xg_0.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% request.setCharacterEncoding("utf-8"); %> 11 <jsp:useBean id="kc" class="kecheng.Kc" ></jsp:useBean> 12 <jsp:useBean id="kcservice" class="service.Kcservice"></jsp:useBean> 13 <jsp:setProperty property="*" name="kc"/> 14 <% 15 if(kcservice.updateStu(kc)) 16 out.print("success"); 17 else 18 out.print("error"); 19 %> 20 </body> 21 </html>
查询:chaxun.jsp
1 <%@page import="kecheng.Kc"%> 2 <%@page import="java.util.Iterator"%> 3 <%@page import="java.util.List"%> 4 <%@ page language="java" contentType="text/html; charset=UTF-8" 5 pageEncoding="UTF-8"%> 6 <!DOCTYPE html> 7 <html> 8 <head> 9 <meta charset="UTF-8"> 10 <title>查询</title> 11 </head> 12 <body> 13 <jsp:useBean id="stuSer" class="service.Kcservice"></jsp:useBean> 14 <% 15 List stus=stuSer.queryAllStu(); 16 //out.print(stus.size()); 17 Iterator iter=stus.iterator(); 18 %> 19 <table> 20 <tr> <td>ID </td> <td>课程姓名 </td> <td>教师姓名</td><td>上课地点</td> </tr> 21 22 <% 23 int i=0; 24 while(iter.hasNext()) 25 { 26 Kc kc=(Kc)iter.next(); 27 %> 28 <tr <%if(i%2==0){ %>bgcolor="#F0F8FF"<%} %>> 29 <td> <%=kc.getId() %></td> 30 <td> <%=kc.getClassname() %></td> 31 <td> <%=kc.getTeachername()%></td> 32 <td> <%=kc.getPlace()%></td> 33 </tr> 34 <% 35 i++; 36 } 37 %> 38 </table> 39 </body> 40 </html>
数据库的表:
运行结果:
程序首界面:
添加:
查看:
修改:
删除:
数据库: