原创
通过JSP+JDBC制作一个简单的操作数据库中表信息的系统。
总体界面如下,一共有5个功能块。
功能一:列出全部学生
功能二:按条件查询学生
功能三:新添加学生
功能四:按条件删除学生
功能五:按条件修改学生
JSP页面介绍(连接的数据名称为DB OF CWM,操作的表为Student):
综合数据库系统总页面.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=UTF-8"> <title>综合数据库系统总页面title> head> <frameset rows="80,*"> <frame src="综合数据库页面_up.jsp" scrolling="no"> <frameset cols="140,*"> <frame src="综合数据库页面_down_left.jsp" scrolling="no"> <frame src="综合数据库_down_right.jsp" name="right" scrolling="no"> frameset> frameset> html>
综合数据库页面_up.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> <h1 align="center">学生数据库管理系统h1> head> html>
综合数据库页面_down_left.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> <title>菜单页面title> head> <body> <br><br><br><br><br><br> <p><a href="综合数据库_down_left_列出全部学生.jsp" target="right">列出全部学生a> <p><a href="综合数据库_down_left_按条件查询学生.jsp" target="right">按条件查询学生a> <p><a href="综合数据库_down_left_新添加学生.jsp" target="right">新添加学生a> <p><a href="综合数据库_down_left_按条件删除学生.jsp" target="right">按条件删除学生a> <p><a href="综合数据库_down_left_按条件修改学生.jsp" target="right">按条件修改学生a> body> html>
综合数据库_down_right.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=UTF-8"> <title>综合数据库_down_righttitle> head> <body> <h2 align="center">综合数据库管理h2> body> html>
综合数据库_down_left_列出全部学生.jsp —— (连接数据库显示表Student的所有学生信息,数据库连接密码自己修改)
<%@ page language="java" import="java.util.*,java.sql.*" 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> <% String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; Class.forName(driverName); String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=DB OF CWM"; String userName="sa"; String pwd="*******"; Connection conn=DriverManager.getConnection(dbURL,userName,pwd); String sql="select *from student"; PreparedStatement stmp=conn.prepareStatement(sql); ResultSet rs=stmp.executeQuery(); while(rs.next()){ %> <table cellpadding="1"> <tr> <td><%=rs.getString("Sno") %>td> <td><%=rs.getString("Sname") %>td> <td><%=rs.getString("Ssex") %>td> <td><%=rs.getString("Sage") %>td> <td><%=rs.getString("Sdept") %>td> tr> table> <% }%> <% if(rs!=null){ rs.close(); } if(stmp!=null){ stmp.close(); } if(conn!=null){ conn.close(); } %> body> html>
综合数据库_down_left_按条件查询学生.jsp
<%@ page language="java" import="java.sql.*,java.util.*" 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> 请输入学生的以下信息进行查询:<br><br> <form action="综合数据库_down_left_按条件查询学生_查询结果.jsp" method="post"> 学号: <input type="text" name="Sno"><br><br> 姓名: <input type="text" name="Sname"><br><br> <input type="submit" value="确定"> <input type="reset" value="取消"> form> body> html>
综合数据库_down_left_按条件查询学生_查询结果.jsp
<%@ page language="java" import="java.sql.*,java.util.*" 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>综合数据库_down_left_按条件查询学生_查询结果title> head> <body> <h2>查询结果如下:h2><br><br> <% request.setCharacterEncoding("UTF-8"); String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; Class.forName(driverName); String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=DB OF CWM"; String userName="sa"; String pwd="******"; Connection conn=DriverManager.getConnection(dbURL,userName,pwd); String snonum=request.getParameter("Sno"); String Ssname=request.getParameter("Sname"); String sql="select * from Student where Sno=? and Sname=?"; PreparedStatement stmp=conn.prepareStatement(sql); stmp.setString(1, snonum); stmp.setString(2, Ssname); ResultSet rs=stmp.executeQuery(); while(rs.next()){%> <table cellpadding="1"> <tr> <td><%=rs.getString("Sno") %>td> <td><%=rs.getString("Sname") %>td> <td><%=rs.getString("Ssex") %>td> <td><%=rs.getString("Sage") %>td> <td><%=rs.getString("Sdept") %>td> tr> table> <% } if(rs!=null){ rs.close(); } if(stmp!=null){ stmp.close(); } if(conn!=null){ conn.close(); } %> body> html>
综合数据库_down_left_新添加学生.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=UTF-8"> <title>新添加学生title> head> <body> <b>请填入以下信息(信息不能为空):b><br><br> <form action="综合数据库_down_left_新添加学生_插入.jsp" method="post"> <table> <tr>学号: <input type="text" name="Sno">tr><br><br> <tr>姓名: <input type="text" name="Sname">tr><br><br> <tr>性别: 男 <input type="radio" name="Ssex" value="男"> 女 <input type="radio" name="Ssex" value="女">tr><br><br> <tr>年龄: <input type="text" name="Sage">tr><br><br> <tr>学院: <input type="text" name="Sdept">tr><br><br> <tr colspan="3"> <input type="submit" value="确定"> <input type="reset" value="取消">tr> table> form> body> html>
综合数据库_down_left_新添加学生_插入.jsp
<%@ page language="java" import="java.util.*,java.sql.*" 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>综合数据库_down_left_新添加学生_插入title> head> <body> <% request.setCharacterEncoding("UTF-8"); String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; Class.forName(driverName); String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=DB OF CWM"; String userName="sa"; String pwd="******"; Connection conn=DriverManager.getConnection(dbURL,userName,pwd); String sql="insert into Student(Sno,Sname,Ssex,Sage,Sdept)values(?,?,?,?,?)"; String sno=request.getParameter("Sno"); String sname=request.getParameter("Sname"); String ssex=request.getParameter("Ssex"); int sage=Integer.parseInt(request.getParameter("Sage")); String sdept=request.getParameter("Sdept"); PreparedStatement stmp=conn.prepareStatement(sql); stmp.setString(1,sno); stmp.setString(2,sname); stmp.setString(3,ssex); stmp.setInt(4,sage); stmp.setString(5,sdept); int n=stmp.executeUpdate(); if(n==1){%> <h3>数据插入成功!h3> <% } else{%> <h3>数据插入错误,请再次插入!h3> <%}%> <% if(stmp!=null){ stmp.close(); } if(conn!=null){ conn.close(); } %> body> html>
综合数据库_down_left_按条件删除学生.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=UTF-8"> <title>按条件删除学生title> head> <body> <h2>请输入需要删除的学生的信息:h2><br> <form action="综合数据库_down_left_按条件删除学生_删除.jsp" method="post"> <table> <tr><td>学号: td><td><input type="text" name="Sno">td>tr><br> <tr><td>姓名: td><td><input type="text" name="Sname">td>tr> <tr><td colspan="1"><input type="submit" value="确认"> td><td><input type="reset" value="取消">td>tr> table> form> body> html>
综合数据库_down_left_按条件删除学生_删除.jsp
<%@ page language="java" import="java.util.*,java.sql.*" 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>综合数据库_down_left_按条件删除学生_删除title> head> <body> <% request.setCharacterEncoding("UTF-8"); String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; Class.forName(driverName); String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=DB OF CWM"; String userName="sa"; String pwd="********"; Connection conn=DriverManager.getConnection(dbURL,userName,pwd); String sql="delete from Student where Sno=? and Sname=?"; PreparedStatement stmp=conn.prepareStatement(sql); String Sno=request.getParameter("Sno"); String Sname=request.getParameter("Sname"); stmp.setString(1, Sno); stmp.setString(2, Sname); int n=stmp.executeUpdate(); if(n==1){%> 此学生信息已被删除! <% }else{%> 删除失败,请再次操作! <% } %> <% if(stmp!=null){ stmp.close(); } if(conn!=null){ conn.close(); } %> body> html>
综合数据库_down_left_按条件修改学生.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=UTF-8"> <title>综合数据库_down_left_按条件修改学生title> head> <body> 请输入需要修改信息的学生的学号和姓名:<br><br> <form action="综合数据库_down_left_按条件修改学生_显示原本信息.jsp" method="post"> 学号: <input type="text" name="Sno"><br><br> 姓名: <input type="text" name="Sname"><br><br> <input type="submit" value="确认"> <input type="reset" value="取消"> form> body> html>
综合数据库_down_left_按条件修改学生_显示原本信息.jsp
<%@ page language="java" import="java.util.*,java.sql.*" 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>综合数据库_down_left_按条件修改学生_显示原本信息title> head> <body> <% request.setCharacterEncoding("UTF-8"); String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; Class.forName(driverName); String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=DB OF CWM"; String userName="sa"; String pwd="******"; Connection conn=DriverManager.getConnection(dbURL,userName,pwd); String sql="select * from Student where Sno=? and Sname=?"; String Sno=request.getParameter("Sno"); String Sname=request.getParameter("Sname"); session.setAttribute("Sno", Sno); session.setAttribute("Sname",Sname); PreparedStatement stmp=conn.prepareStatement(sql); stmp.setString(1, Sno); stmp.setString(2, Sname); ResultSet rs=stmp.executeQuery(); if(rs.next()){ String S_no=rs.getString("Sno"); String S_name=rs.getString("Sname"); String S_sex=rs.getString("Ssex"); int S_age=rs.getInt("Sage"); String S_dept=rs.getString("Sdept"); if(rs!=null){ rs.close(); } if(stmp!=null){ stmp.close(); } if(conn!=null){ conn.close(); } %> 此学生信息如下:<br><br> <form action="综合数据库_down_left_按条件修改学生_更新数据.jsp" method="past"> <table> <tr><td><input type="text" name="Sno" value=<%=S_no %>>td>tr> <tr><td><input type="text" name="Sname" value=<%=S_name %>>td>tr> <tr><td><input type="text" name="Ssex" value=<%=S_sex %>>td>tr> <tr><td><input type="text" name="Sage" value=<%=S_age %>>td>tr> <tr><td><input type="text" name="Sdept" value=<%=S_dept %>>td>tr> <tr><td><input type="submit" value="确认">td> <td><input type="reset" value="取消">td>tr> table> form> <% } %> body> html>
综合数据库_down_left_按条件修改学生_更新数据.jsp
<%@ page language="java" import="java.util.*,java.sql.*" 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>综合数据库_down_left_按条件修改学生_更新数据title> head> <body> <% request.setCharacterEncoding("UTF-8"); String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; Class.forName(driverName); String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=DB OF CWM"; String userName="sa"; String pwd="*******"; Connection conn=DriverManager.getConnection(dbURL,userName,pwd); String sql="update Student set Sno=?,Sname=?,Ssex=?,Sage=?,Sdept=? where Sno=? and Sname=?"; String S_no=request.getParameter("Sno"); String S_name=request.getParameter("Sname"); String S_sex=request.getParameter("Ssex"); int S_age=Integer.parseInt(request.getParameter("Sage")); String S_dept=request.getParameter("Sdept"); String Sno=(String)session.getAttribute("Sno"); String Sname=(String)session.getAttribute("Sname"); PreparedStatement stmp=conn.prepareStatement(sql); stmp.setString(1, S_no); stmp.setString(2, S_name); stmp.setString(3, S_sex); stmp.setInt(4, S_age); stmp.setString(5, S_dept); stmp.setString(6, Sno); stmp.setString(7, Sname); int n=stmp.executeUpdate(); if(n==1){%> 数据更新成功<br> <% } else{%> 数据更新失败<br> <% } if(stmp!=null){ stmp.close(); } if(conn!=null){ conn.close(); } %> body> html>
22:24:21
2018-10-02