课程信息管理系统(javabean + Servlet + jsp)
Posted 一往无前!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了课程信息管理系统(javabean + Servlet + jsp)相关的知识,希望对你有一定的参考价值。
此项目做的事一个课程管理系统,需要通过web做一个可以实现课程的增删改查的功能。
需要用到数据库,Servlet和jsp等(第一次使用Servlet和数据库连接,所以代码都比较低级,页面也比较粗糙,还没有实现Servlet处理后数据的回传,还未实现模糊查询)
程序所建的项目如下:
数据表的格式:
1.首先建立数据库链接
course/src/com.jdbc.util/BaseConnection.java
代码如下:
1 package com.jdbc.util; 2 3 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 7 public class BaseConnection { 8 9 public static Connection getConnection(){//用这个方法获取mysql的连接 10 Connection conn=null; 11 String driver = "com.mysql.jdbc.Driver"; 12 String url = "jdbc:mysql://localhost:3306/sql?characterEncoding=utf8&useSSL=true"; 13 String user = "root"; 14 String password = "";//此处填写数据库连接密码 15 try{ 16 Class.forName(driver);//加载驱动类 17 conn=DriverManager. 18 getConnection(url,user,password);//(url数据库的IP地址,user数据库用户名,password数据库密码) 19 }catch(Exception e){ 20 e.printStackTrace(); 21 } 22 return conn; 23 } 24 25 public static void main(String[] args) { 26 System.out.println("连接成功"); 27 } 28 }
利用javabean建立一个课程类:
course/src/com.jdbc.bean/Course:
代码如下:
1 package com.jdbc.bean; 2 3 public class Course { 4 5 private String classname; 6 private String teacher; 7 private String place; 8 9 public String getClassname() { 10 return classname; 11 } 12 public void setClassname(String classname) { 13 this.classname = classname; 14 } 15 public String getTeacher() { 16 return teacher; 17 } 18 public void setTeacher(String teacher) { 19 this.teacher = teacher; 20 } 21 public String getPlace() { 22 return place; 23 } 24 public void setPlace(String place) { 25 this.place = place; 26 } 27 28 29 30 }
为了后期操作的方便我将一些增删改查的方法写到了一个类中,把他们封装为一个一个的具体的方法
course/src/com.jdbc.dao/JdbcMain.java
代码如下:
1 package com.jdbc.dao; 2 3 4 import java.sql.Connection; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 import java.util.Scanner; 9 10 import com.jdbc.bean.Course; 11 import com.jdbc.util.BaseConnection; 12 13 public class JdbcMain { 14 static Scanner in = new Scanner(System.in); 15 public static void main(String[] args) { 16 // TODO 自动生成的方法存根 17 18 19 } 20 21 22 public static int add(Course cour) 23 { 24 Connection conn= BaseConnection.getConnection(); 25 26 String sql = "insert into course values(?,?,?)"; 27 28 int b = 0; 29 PreparedStatement ps=null; 30 try{ 31 32 ps= conn.prepareStatement(sql);//把写好的sql语句传递到数据库,让数据库知道我们要干什么 33 34 ps.setString(1,cour.getClassname()); 35 36 ps.setString(2,cour.getTeacher()); 37 38 ps.setString(3, cour.getPlace()); 39 40 int a=ps.executeUpdate();//这个方法用于改变数据库数据,a代表改变数据库的条数 41 if(a>0){ 42 b++; 43 System.out.println("添加成功"); 44 45 }else{ 46 System.out.println("添加失败"); 47 48 } 49 }catch(Exception e){ 50 e.printStackTrace(); 51 }try{ 52 if(ps!=null){ 53 ps.close(); 54 }if(conn!=null){ 55 conn.close(); 56 } 57 }catch(Exception e2){ 58 e2.printStackTrace(); 59 } 60 61 return b; 62 } 63 64 65 66 public static int update(Course cour ) 67 { 68 int b = 0; 69 70 Connection conn= BaseConnection.getConnection(); 71 PreparedStatement ps=null; 72 String sql="update course set place=?,teacher=? where class=?"; 73 try{ 74 ps=conn.prepareStatement(sql); 75 76 ps.setString(1,cour.getPlace()); 77 ps.setString(2,cour.getTeacher()); 78 ps.setString(3,cour.getClassname()); 79 int a=ps.executeUpdate(); 80 if(a>0){ 81 b++; 82 System.out.println("修改成功"); 83 }else{ 84 System.out.println("修改失败"); 85 } 86 }catch(Exception e){ 87 e.printStackTrace(); 88 }finally{ 89 try{ 90 if(ps!=null){ 91 ps.close(); 92 }if(conn!=null){ 93 conn.close(); 94 } 95 }catch(Exception e2){ 96 e2.printStackTrace(); 97 } 98 } 99 100 101 return b; 102 } 103 104 105 public static int delete(String classname) 106 107 { 108 int b = 0; 109 Connection conn=BaseConnection.getConnection(); 110 PreparedStatement ps=null; 111 String sql="delete from course where class =?"; 112 113 try{ 114 ps=conn.prepareStatement(sql); 115 ps.setString(1, classname); 116 int a=ps.executeUpdate(); 117 if(a>0){ 118 b++; 119 System.out.println("删除成功"); 120 }else{ 121 System.out.println("删除失败"); 122 } 123 }catch(Exception e){ 124 e.printStackTrace(); 125 }finally{ 126 try{ 127 if(ps!=null){ 128 ps.close(); 129 }if(conn!=null){ 130 conn.close(); 131 } 132 }catch(Exception e2){ 133 e2.printStackTrace(); 134 } 135 } 136 return b; 137 } 138 139 public static Course find(String s) 140 { 141 Connection conn= BaseConnection.getConnection(); 142 PreparedStatement ps=null; 143 144 ResultSet rs = null; 145 146 Course cour = new Course(); 147 148 String sql="select * from course where class =?"; 149 150 try { 151 ps=conn.prepareStatement(sql); 152 153 ps.setString(1, s); 154 rs=ps.executeQuery(); 155 if(rs.next()){ 156 cour.setClassname(rs.getString("class")); 157 cour.setTeacher(rs.getString("teacher")); 158 cour.setPlace(rs.getString("place")); 159 160 } 161 162 163 164 } catch (SQLException e) { 165 // TODO 自动生成的 catch 块 166 e.printStackTrace(); 167 } finally{ 168 try{ 169 if(ps!=null){ 170 ps.close(); 171 }if(conn!=null){ 172 conn.close(); 173 } 174 if(rs!=null) 175 { 176 rs.close(); 177 } 178 }catch(Exception e2){ 179 e2.printStackTrace(); 180 } 181 } 182 183 return cour; 184 } 185 186 187 188 }
下面建立主界面:
主界面主要就是在界面中介入了多个链接,通过对链接的点击,进行页面的跳转,实现不同的操作。
路径 /course/WebContent/admin/main.jsp
代码如下:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>主页面</title> 8 </head> 9 <body> 10 11 <a href="javascript:location.reload()">刷新页面</a></br> 12 <a href="add.jsp" target="iframe_a">增加课程</a></br> 13 <a href="delete.jsp" target="iframe_a">删除课程</a></br> 14 <a href="update.jsp" target="iframe_a">修改课程</a></br> 15 <a href="find.jsp" target="iframe_a">查询课程</a></br> 16 <a href="ShowAll.jsp" target="iframe_a">显示全部课程</a> 17 18 19 </body> 20 </html>
课程添加页面:
路径/course/WebContent/admin/add.jsp
代码如下:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 6 <title>Insert title here</title> 7 </head> 8 <body> 9 <form action="${pageContext.request.contextPath}/Manage?method=add" method="post" id="add"> 10 11 课程名称:<input name = "class" type = "text" ></br> 12 任课教师:<input name = "teacher" type = "text"></br> 13 上课地点:<input name = "place" type = "text"></br> 14 <input type="submit"value="保存" > 15 16 </form> 17 </body> 18 </html>
删除页面:主要操作就是通过课程名称找到课程位置,输出一下他的具体信息并将其删除
路径/course/WebContent/admin/delete.jsp
代码如下:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 6 <title>删除界面</title> 7 </head> 8 <body> 9 <form action="${pageContext.request.contextPath}/Manage?method=delete" method="post" id="delete"> 10 11 12 输入您要删除的课程:<input type = "text" name = "class"> 13 14 <input type="submit"value="提交" > 15 </form> 16 </body> 17 </html>
修改页面:通过课程名称进行搜索,并对课程信息进行修改,现在还存在的问题是都是通过课程名称找到的还能修改课程名称。
路径/course/WebContent/admin/update.jsp
代码如下:
<%@ 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=ISO-8859-1"> <title>修改课程信息</title> </head> <body> <form action="${pageContext.request.contextPath}/Manage?method=update" method="post" id="update"> 课程名称:<input name = "class" type = "text"></br> 任课教师:<input name = "teacher" type = "text"></br> 上课地点:<input name = "place" type = "text"></br> <input type="submit"value="提交" > </form> </body> </html>
查找界面:现在只实现了通过课程名称查找,还没有实现模糊查询
路径/course/WebContent/admin/find.jsp
代码如下:
<%@ 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=ISO-8859-1">
<title>查找</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/Manage?method=find" method="post" id="find">
课程名称:<input name = "class" type = "text">
<input type="submit"value="提交" >
</form>
</body>
</html>
显示所有页面:输出数据库中的全部数据
路径/course/WebContent/admin/show.jsp
代码如下:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 3 <%@page import="com.jdbc.dao.JdbcMain"%> 4 <%@page import="com.jdbc.util.BaseConnection"%> 5 <%@page import="java.sql.PreparedStatement"%> 6 <%@page import="java.sql.ResultSet"%> 7 <%@page import="java.sql.SQLException"%> 8 <%@page import="java.sql.Connection"%> 9 10 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 11 <html> 12 <head> 13 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 14 <title>显示全部</title> 15 </head> 16 <body> 17 <form name = "form6" action = "main.jsp" method = "post"> 18 <% 19 20 Connection conn= BaseConnection.getConnection(); 21 PreparedStatement ps=null; 22 ResultSet rs = null; 23 String sql = "select * from course"; 24 try { 25 ps=conn.prepareStatement(sql); 26 rs=ps.executeQuery();//执行数据库查询的方法,放到rs中 27 28 29 String classname = null; 30 String teacher = null; 31 String place = null; 32 while(rs.next()){ 33 34 classname = rs.getString("class"); 35 36 teacher = rs.getString("teacher"); 37 place = rs.getString("place"); 38 39 //输出结果 40 out.println(classname + "\\t" + teacher + \'\\t\' + place + "</br>"); 41 } 42 43 } catch (SQLException e) { 44 // TODO 自动生成的 catch 块 45 e.printStackTrace(); 46 }finally{ 47 try{ 48 if(ps!=null){ 49 ps.close(); 50 }if(conn!=null){ 51 conn.close(); 52 }if(rs!=null) 53 { 54 rs.close(); 55 } 56 }catch(Exception e2){ 57 e2.printStackTrace(); 58 } 59 } 60 61 62 63 64 65 66 %> 67 <input type = "submit" value = "返回" > 68 </form> 69 </body> 70 </html>
根据界面出入的数据,都将其传入Servlet中然后对其进行处理,由于不会使用数据的回传,无法通过页面的形式给出提示信息,所以就用java的提示框进行对结果的提示了
Servlet路径/course/src/servelet/Manage.java
代码如下:
1 package servelet; 2 3 import java.io.IOException; 4 import java.io.UnsupportedEncodingException; 5 import java.sql.Connection; 6 import java.sql.PreparedStatement; 7 import java.sql.ResultSet; 8 import java.sql.SQLException; 9 import java.sql.Statement; 10 11 import javax.servlet.ServletException; 12 import javax.servlet.annotation.WebServlet; 13 import javax.servlet.http.HttpServlet; 14 import javax.servlet.http.HttpServletRequest; 15 import javax.servlet.http.HttpServletResponse; 16 import javax.swing.JOptionPane; 17 import javax.swing.plaf.metal.MetalIconFactory.PaletteCloseIcon; 18 19 import com.jdbc.bean.Course; 20 import com.jdbc.dao.JdbcMain; 21 import com.jdbc.util.BaseConnection; 22 23 /** 24 * Servlet implementation class Manage 25 */ 26 @WebServlet("/Manage") 27 public class Manage extends HttpServlet { 28 private static final long serialVersionUID = 1L; 29 30 /** 31 * @see HttpServlet#HttpServlet() 32 */ 33 public Manage() { 34 super(); 35 // TODO Auto-generated constructor stub 36 } 37 38 /** 39 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 40 */ 41 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 42 // TODO Auto-generated method stub 43 response.getWriter().append("Served at: ").append(request.getContextPath()); 44 45 request.setCharacterEncoding("UTF-8"); 46 String method = request.getParameter("method"); 47 48 49 if("add".equals(method)) //添加课程 50 { 51 52 53 try { 54 add(request,response); 55 } catch (SQLException e) { 56 // TODO 自动生成的 catch 块 57 e.printStackTrace(); 58 } 59 60 }else if("delete".equals(method)) //通过课程名称查找并删除对应的课程(必须是具体的课程名称,否则就显示未找到该课程,也就无法删除) 61 { 62 delete(request,response); 63 }else 64 65 if("find".equals(method)) //查找课程信息 66 { 67 find(request,response); 68 }else 69 70 if("update".equals(method)) //通过课程名称进行查找,并对其进行修改(由于只能通过课程查找) 71 { 72 update(request,response); 73 } 74 75 } 76 77 78 79 80 private void update(HttpServletRequest request, HttpServletResponse response) { 81 // TODO 自动生成的方法存根 82 Course cour = new Course(); 83 84 String name = request.getParameter("class"); 85 String teacher = request.getParameter("teacher"); 86 String place = request.getParameter("place"); 87 88 cour.setClassname(name); 89 cour.setTeacher(teacher); 90 cour.setPlace(place); 91 String teachers = "王建民刘立嘉刘丹王辉杨子光"; 92 String places = "一教二教三教基教"; 93 String pl = place.substring(0, 2); 以上是关于课程信息管理系统(javabean + Servlet + jsp)的主要内容,如果未能解决你的问题,请参考以下文章使用Jsp+Servlet+JavaBean技术做学生成绩管理系统