高校学生考勤管理系统设计与简单实现
我进行这次设计的初衷是想把这个系统放到易班上,所以正常的情况下是应该是两个端口访问的,但是这里我就只用一个index链过去了吗,方便测试。
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>学生请假管理系统</title>
</head>
<body>
<a href="student.jsp">学生端</a><br>
<a href="teacher.jsp">教师端</a>
</body>
</html>
mysql:这里使用的mysql管理工具是 Navicat Premium 12百度网盘链接:https://pan.baidu.com/s/1MMmK_o5LiviNQkxGnG4R4g 提取码:dmdi 强烈推荐,非常好用。
student.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page language="java" import=" java.util.Date"%>
<%@ page language="java" import=" java.text.SimpleDateFormat"%>
<HTML><body bgcolor=#33ccff>
<%
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(sdf.format(date));
String qingjia_time =(String)sdf.format(date);
%>
<center>
<font size=2>
<form action="insert_Servlet?dataBase=student&tableName=qingjia&qingjia_time=<%=qingjia_time%>" method=post>
<h3>请假</h3>
<table border=1>
<tr><td>学号: </td><td><Input type="text" name="stu_number"></td></tr>
<tr><td>班级: </td><td><Input type="text" name="class_id"></td></tr>
<tr><td>姓名: </td><td><Input type="text" name="stu_name"></td></tr>
<tr><td>请假开始时间:</td><td><Input type="text" name="start_time"></td></tr>
<tr><td>请假结束时间:</td><td><Input type="text" name="end_time"></td></tr>
<tr><td>请假原因: </td><td><Input type="text" name="qingjia_yuanyin"></td></tr>
</table>
<br><input type="submit" name="b" value="提交">
</font>
</center>
</body></HTML>
teacher.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<BODY>
<Font size=4>
<FORM action="Select_class_Servlet?dataBase=student&tableName=qingjia" Method="post">
<P>班级查询
<P>输入班级:
<Input type=text name="class_id">
<Input type=submit name="a" value="提交">
</Form>
<FORM action="Select_number_Servlet?dataBase=student&tableName=qingjia" Method="post">
<P>学号查询
<P>输入学号:
<Input type=text name="stu_number">
<Input type=submit name="b" value="提交">
</Form>
</BODY></HTML>
showRecord.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<jsp:useBean id="resultBean" class="mybean.data.studentinfo" scope="request"/>
<HTML><body bgcolor=#DEEFF9>
<font size=2>
<table border=1>
<% String []columnName=resultBean.getColumnName();
%>
<tr>
<th>编号</th>
<th>学生学号</th>
<th>班级</th>
<th>姓名</th>
<th>开始时间</th>
<th>结束时间</th>
<th>申请请假时间</th>
<th>请假原因</th>
<th>辅导员审批状态</th>
<th>辅导员审批时间</th>
<th>辅导员回复</th>
</tr>
<% String [][] record = resultBean.getTableRecord();
for(int i=0;i<record.length;i++) {
%> <tr>
<% for(int j=0;j<record[i].length;j++) {
%> <td><%= record[i][j] %> </td>
<% }
%> </tr>
<% }
%>
</table>
<center>
<font size=2>
<form action="delete_Servlet?dataBase=student&tableName=qingjia" method=post>
<h3>输入ID删除请假记录</h3>
<table border=1>
<tr><td>ID:</td><td><Input type="text" name="ID"></td></tr>
<tr><td>学号:</td><td><Input type="text" name="stu_number"></td></tr>
</table>
<br><input type="submit" name="b" value="提交">
</font>
</center>
</font></body></HTML>
showRecord_teacher.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page language="java" import=" java.util.Date"%>
<%@ page language="java" import=" java.text.SimpleDateFormat"%>
<jsp:useBean id="resultBean" class="mybean.data.studentinfo" scope="request"/>
<HTML><body bgcolor=#DEEFF9>
<font size=2>
<table border=1>
<%
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(sdf.format(date));
String class_teacher_sp_time =(String)sdf.format(date);
%>
<% String []columnName=resultBean.getColumnName();
%>
<tr>
<th>编号</th>
<th>学生学号</th>
<th>班级</th>
<th>姓名</th>
<th>开始时间</th>
<th>结束时间</th>
<th>申请请假时间</th>
<th>请假原因</th>
<th>辅导员审批状态</th>
<th>辅导员审批时间</th>
<th>辅导员回复</th>
</tr>
<% String [][] record = resultBean.getTableRecord();
for(int i=0;i<record.length;i++) {
%> <tr>
<% for(int j=0;j<record[i].length;j++) {
%> <td><%= record[i][j] %> </td>
<% }
%> </tr>
<% }
%>
</table>
<center>
<font size=2>
<form action="insert_Sp_Servlet?dataBase=student&tableName=qingjia&class_teacher_sp_time=<%=class_teacher_sp_time%>" method=post>
<h3>输入ID进行审批</h3>
<table border=1>
<tr><td>ID:</td><td><Input type="text" name="ID"></td></tr>
<tr><td>辅导员审批状态:</td><td><Input type="text" name="class_teacher_status"></td></tr>
<tr><td>辅导员回复:</td><td><Input type="text" name="class_teacher_restore"></td></tr>
<tr>注:0为等待审批,1为同意请假,2为不同意请假</tr>
</table>
<br><input type="submit" name="b" value="提交">
</font>
</center>
</font></body></HTML>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>Insert_Servlet</servlet-name>
<servlet-class>myservlet.control.Insert_Servlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>Delete_Servlet</servlet-name>
<servlet-class>myservlet.control.Delete_Servlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>Select_class_Servlet</servlet-name>
<servlet-class>myservlet.control.Select_class_Servlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>Select_number_Servlet</servlet-name>
<servlet-class>myservlet.control.Select_number_Servlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>Insert_Sp_Servlet</servlet-name>
<servlet-class>myservlet.control.Insert_Sp_Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Insert_Servlet</servlet-name>
<url-pattern>/insert_Servlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Delete_Servlet</servlet-name>
<url-pattern>/delete_Servlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Select_class_Servlet</servlet-name>
<url-pattern>/Select_class_Servlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Select_number_Servlet</servlet-name>
<url-pattern>/Select_number_Servlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Insert_Sp_Servlet</servlet-name>
<url-pattern>/insert_Sp_Servlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
studentinfo.java
package mybean.data;
public class studentinfo {
String []columnName ; //存放列名
String [][] tableRecord=null; //存放查询到的记录
public studentinfo() {
tableRecord = new String[1][1];
columnName = new String[1];
}
public void setTableRecord(String [][] s){
tableRecord=s;
}
public String [][] getTableRecord(){
return tableRecord;
}
public void setColumnName(String [] s) {
columnName = s;
}
public String [] getColumnName() {
return columnName;
}
}
Delete_Servlet.java
package myservlet.control;
import mybean.data.studentinfo;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Delete_Servlet extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
studentinfo resultBean=null;
try{ resultBean=(studentinfo)request.getAttribute("resultBean");
if(resultBean==null){
resultBean=new studentinfo(); //创建Javabean对象
request.setAttribute("resultBean",resultBean);
}
}
catch(Exception exp){
resultBean=new studentinfo(); //创建Javabean对象
request.setAttribute("resultBean",resultBean);
}
try{ Class.forName("com.mysql.jdbc.Driver");//加载驱动
}
catch(Exception e){}
request.setCharacterEncoding("gb2312");
String dataBase = request.getParameter("dataBase");
String tableName = request.getParameter("tableName");
String stu_number=request.getParameter("stu_number");
String id=request.getParameter("ID");
String stu_name=request.getParameter("stu_name");
String start_time=request.getParameter("start_time");
String end_time=request.getParameter("end_time");
String qingjia_time=request.getParameter("qingjia_time");
String qingjia_yuanyin=request.getParameter("qingjia_yuanyin");
if(id==null||id.length()==0) {
fail(request,response,"添加记录失败,必须给出记录");
return;
}
String condition = "DELETE FROM "+tableName+" where id='"+id+"'";
System.out.println(condition);
Connection con;
Statement sql;
ResultSet rs;
try{
String uri= "jdbc:mysql://localhost/"+dataBase+"?characterEncoding=utf8";
con=DriverManager.getConnection(uri,"root","root1");//建立连接
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
sql.executeUpdate(condition);
// System.out.println("SELECT * FROM "+tableName+" where stu_number='"+stu_number+"'");
rs=sql.executeQuery("SELECT * FROM "+tableName+" where stu_number='"+stu_number+"'");
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount(); //得到结果集的列数
String []columnName = new String[columnCount];
for(int i=0;i<columnName.length;i++) {
columnName[i] = metaData.getColumnName(i+1); //得到列名
}
resultBean.setColumnName(columnName); //更新Javabean数据模型
rs.last();
int rowNumber=rs.getRow(); //得到记录数
String [][] tableRecord=resultBean.getTableRecord();
tableRecord = new String[rowNumber][columnCount];
rs.beforeFirst();
int i=0;
while(rs.next()){
for(int k=0;k<columnCount;k++)
tableRecord[i][k] = rs.getString(k+1);
i++;
}
resultBean.setTableRecord(tableRecord); //更新Javabean数据模型
con.close();
RequestDispatcher dispatcher=
request.getRequestDispatcher("showRecord.jsp");
dispatcher.forward(request,response);
}
catch(SQLException e){
System.out.println(e);
fail(request,response,"添加记录失败:"+e.toString());
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doPost(request,response);
}
public void fail(HttpServletRequest request,HttpServletResponse response,
String backNews) {
response.setContentType("text/html;charset=GB2312");
try {
PrintWriter out=response.getWriter();
out.println("<html><body>");
out.println("<h2>"+backNews+"</h2>") ;
out.println("返回");
out.println("<a href=student.jsp>输入记录</a>");
out.println("</body></html>");
}
catch(IOException exp){}
}
}
Insert_Servlet.java
package myservlet.control;
import mybean.data.studentinfo;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Insert_Servlet extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
studentinfo resultBean=null;
try{ resultBean=(studentinfo)request.getAttribute("resultBean");
if(resultBean==null){
resultBean=new studentinfo(); //创建Javabean对象
request.setAttribute("resultBean",resultBean);
}
}
catch(Exception exp){
resultBean=new studentinfo(); //创建Javabean对象
request.setAttribute("resultBean",resultBean);
}
try{ Class.forName("com.mysql.jdbc.Driver");//加载驱动
}
catch(Exception e){}
request.setCharacterEncoding("gb2312");
String dataBase = request.getParameter("dataBase");
String tableName = request.getParameter("tableName");
String stu_number=request.getParameter("stu_number");
String class_id=request.getParameter("class_id");
String stu_name=request.getParameter("stu_name");
String start_time=request.getParameter("start_time");
String end_time=request.getParameter("end_time");
String qingjia_time=request.getParameter("qingjia_time");
String qingjia_yuanyin=request.getParameter("qingjia_yuanyin");
if(stu_number==null||stu_number.length()==0) {
fail(request,response,"添加记录失败,必须给出记录");
return;
}
String condition = "INSERT INTO "+tableName+"(stu_number,class_id,stu_name,start_time,end_time,qingjia_time,qingjia_yuanyin) VALUES"+
"('"+stu_number+"','"+class_id+"','"+stu_name+"','"+start_time+"','"+end_time+"','"+qingjia_time+"','"+qingjia_yuanyin+"')";
//System.out.println(condition);
Connection con;
Statement sql;
ResultSet rs;
try{
String uri= "jdbc:mysql://localhost/"+dataBase+"?characterEncoding=utf8";
con=DriverManager.getConnection(uri,"root","root1");//建立连接
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
sql.executeUpdate(condition);
System.out.println("SELECT * FROM "+tableName+" where stu_number='"+stu_number+"'");
rs=sql.executeQuery("SELECT * FROM "+tableName+" where stu_number='"+stu_number+"'");
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount(); //得到结果集的列数
String []columnName = new String[columnCount];
for(int i=0;i<columnName.length;i++) {
columnName[i] = metaData.getColumnName(i+1); //得到列名
}
resultBean.setColumnName(columnName); //更新Javabean数据模型
rs.last();
int rowNumber=rs.getRow(); //得到记录数
String [][] tableRecord=resultBean.getTableRecord();
tableRecord = new String[rowNumber][columnCount];
rs.beforeFirst();
int i=0;
while(rs.next()){
for(int k=0;k<columnCount;k++)
tableRecord[i][k] = rs.getString(k+1);
i++;
}
resultBean.setTableRecord(tableRecord); //更新Javabean数据模型
con.close();
RequestDispatcher dispatcher=
request.getRequestDispatcher("showRecord.jsp");
dispatcher.forward(request,response);
}
catch(SQLException e){
System.out.println(e);
fail(request,response,"添加记录失败:"+e.toString());
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doPost(request,response);
}
public void fail(HttpServletRequest request,HttpServletResponse response,
String backNews) {
response.setContentType("text/html;charset=GB2312");
try {
PrintWriter out=response.getWriter();
out.println("<html><body>");
out.println("<h2>"+backNews+"</h2>") ;
out.println("返回");
out.println("<a href=student.jsp>输入记录</a>");
out.println("</body></html>");
}
catch(IOException exp){}
}
}
Insert_Sp_Servlet.java
package myservlet.control;
import mybean.data.studentinfo;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Insert_Sp_Servlet extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
studentinfo resultBean=null;
try{ resultBean=(studentinfo)request.getAttribute("resultBean");
if(resultBean==null){
resultBean=new studentinfo(); //创建Javabean对象
request.setAttribute("resultBean",resultBean);
}
}
catch(Exception exp){
resultBean=new studentinfo(); //创建Javabean对象
request.setAttribute("resultBean",resultBean);
}
try{ Class.forName("com.mysql.jdbc.Driver");//加载驱动
}
catch(Exception e){}
request.setCharacterEncoding("gb2312");
String dataBase = request.getParameter("dataBase");
String tableName = request.getParameter("tableName");
String id=request.getParameter("ID");
String class_teacher_status=request.getParameter("class_teacher_status");
String class_teacher_sp_time=request.getParameter("class_teacher_sp_time");
String class_teacher_restore=request.getParameter("class_teacher_restore");
if(class_teacher_sp_time==null||class_teacher_sp_time.length()==0) {
fail(request,response,"添加记录失败,必须给出记录");
return;
}
String condition = "UPDATE "+tableName+" SET class_teacher_status='"+class_teacher_status+"' , class_teacher_sp_time='"+class_teacher_sp_time+"' ,class_teacher_restore='"+class_teacher_restore+"'"+" where id='"+id+"'";
System.out.println(condition);
Connection con;
Statement sql;
ResultSet rs;
try{
String uri= "jdbc:mysql://localhost/"+dataBase+"?characterEncoding=utf8";
con=DriverManager.getConnection(uri,"root","root1");//建立连接
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
sql.executeUpdate(condition);
System.out.println("SELECT * FROM "+tableName+" where id='"+id+"'");
rs=sql.executeQuery("SELECT * FROM "+tableName+" where id='"+id+"'");
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount(); //得到结果集的列数
String []columnName = new String[columnCount];
for(int i=0;i<columnName.length;i++) {
columnName[i] = metaData.getColumnName(i+1); //得到列名
}
resultBean.setColumnName(columnName); //更新Javabean数据模型
rs.last();
int rowNumber=rs.getRow(); //得到记录数
String [][] tableRecord=resultBean.getTableRecord();
tableRecord = new String[rowNumber][columnCount];
rs.beforeFirst();
int i=0;
while(rs.next()){
for(int k=0;k<columnCount;k++)
tableRecord[i][k] = rs.getString(k+1);
i++;
}
resultBean.setTableRecord(tableRecord); //更新Javabean数据模型
con.close();
RequestDispatcher dispatcher=
request.getRequestDispatcher("showRecord.jsp");
dispatcher.forward(request,response);
}
catch(SQLException e){
System.out.println(e);
fail(request,response,"添加记录失败:"+e.toString());
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doPost(request,response);
}
public void fail(HttpServletRequest request,HttpServletResponse response,
String backNews) {
response.setContentType("text/html;charset=GB2312");
try {
PrintWriter out=response.getWriter();
out.println("<html><body>");
out.println("<h2>"+backNews+"</h2>") ;
out.println("返回");
out.println("<a href=student.jsp>输入记录</a>");
out.println("</body></html>");
}
catch(IOException exp){}
}
}
Select_class_Servlet.java
package myservlet.control;
import mybean.data.studentinfo;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Select_class_Servlet extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
studentinfo resultBean=null;
try{ resultBean=(studentinfo)request.getAttribute("resultBean");
if(resultBean==null){
resultBean=new studentinfo(); //创建Javabean对象
request.setAttribute("resultBean",resultBean);
}
}
catch(Exception exp){
resultBean=new studentinfo(); //创建Javabean对象
request.setAttribute("resultBean",resultBean);
}
try{ Class.forName("com.mysql.jdbc.Driver");//加载驱动
}
catch(Exception e){}
request.setCharacterEncoding("gb2312");
String dataBase = request.getParameter("dataBase");
String tableName = request.getParameter("tableName");
String class_id=request.getParameter("class_id");
if( class_id==null||class_id.length()==0) {
fail(request,response,"添加记录失败,必须给出记录");
return;
}
Connection con;
Statement sql;
ResultSet rs;
try{
String uri= "jdbc:mysql://localhost/"+dataBase+"?characterEncoding=utf8";
con=DriverManager.getConnection(uri,"root","root1");//建立连接
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// System.out.println("SELECT * FROM "+tableName+" where stu_number='"+stu_number+"'");
rs=sql.executeQuery("SELECT * FROM "+tableName+" where class_id='"+ class_id+"'");
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount(); //得到结果集的列数
String []columnName = new String[columnCount];
for(int i=0;i<columnName.length;i++) {
columnName[i] = metaData.getColumnName(i+1); //得到列名
}
resultBean.setColumnName(columnName); //更新Javabean数据模型
rs.last();
int rowNumber=rs.getRow(); //得到记录数
String [][] tableRecord=resultBean.getTableRecord();
tableRecord = new String[rowNumber][columnCount];
rs.beforeFirst();
int i=0;
while(rs.next()){
for(int k=0;k<columnCount;k++)
tableRecord[i][k] = rs.getString(k+1);
i++;
}
resultBean.setTableRecord(tableRecord); //更新Javabean数据模型
con.close();
RequestDispatcher dispatcher=
request.getRequestDispatcher("showRecord_teacher.jsp");
dispatcher.forward(request,response);
}
catch(SQLException e){
System.out.println(e);
fail(request,response,"添加记录失败:"+e.toString());
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doPost(request,response);
}
public void fail(HttpServletRequest request,HttpServletResponse response,
String backNews) {
response.setContentType("text/html;charset=GB2312");
try {
PrintWriter out=response.getWriter();
out.println("<html><body>");
out.println("<h2>"+backNews+"</h2>") ;
out.println("返回");
out.println("<a href=showRecord_teacher.jsp>输入记录</a>");
out.println("</body></html>");
}
catch(IOException exp){}
}
}
Select_number_Servlet.java
package myservlet.control;
import mybean.data.studentinfo;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Select_number_Servlet extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
studentinfo resultBean=null;
try{ resultBean=(studentinfo)request.getAttribute("resultBean");
if(resultBean==null){
resultBean=new studentinfo(); //创建Javabean对象
request.setAttribute("resultBean",resultBean);
}
}
catch(Exception exp){
resultBean=new studentinfo(); //创建Javabean对象
request.setAttribute("resultBean",resultBean);
}
try{ Class.forName("com.mysql.jdbc.Driver");//加载驱动
}
catch(Exception e){}
request.setCharacterEncoding("gb2312");
String dataBase = request.getParameter("dataBase");
String tableName = request.getParameter("tableName");
String stu_number=request.getParameter("stu_number");
if( stu_number==null||stu_number.length()==0) {
fail(request,response,"添加记录失败,必须给出记录");
return;
}
Connection con;
Statement sql;
ResultSet rs;
try{
String uri= "jdbc:mysql://localhost/"+dataBase+"?characterEncoding=utf8";
con=DriverManager.getConnection(uri,"root","root1");//建立连接
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// System.out.println("SELECT * FROM "+tableName+" where stu_number='"+stu_number+"'");
rs=sql.executeQuery("SELECT * FROM "+tableName+" where stu_number='"+stu_number+"'");
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount(); //得到结果集的列数
String []columnName = new String[columnCount];
for(int i=0;i<columnName.length;i++) {
columnName[i] = metaData.getColumnName(i+1); //得到列名
}
resultBean.setColumnName(columnName); //更新Javabean数据模型
rs.last();
int rowNumber=rs.getRow(); //得到记录数
String [][] tableRecord=resultBean.getTableRecord();
tableRecord = new String[rowNumber][columnCount];
rs.beforeFirst();
int i=0;
while(rs.next()){
for(int k=0;k<columnCount;k++)
tableRecord[i][k] = rs.getString(k+1);
i++;
}
resultBean.setTableRecord(tableRecord); //更新Javabean数据模型
con.close();
RequestDispatcher dispatcher=
request.getRequestDispatcher("showRecord_teacher.jsp");
dispatcher.forward(request,response);
}
catch(SQLException e){
System.out.println(e);
fail(request,response,"添加记录失败:"+e.toString());
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doPost(request,response);
}
public void fail(HttpServletRequest request,HttpServletResponse response,
String backNews) {
response.setContentType("text/html;charset=GB2312");
try {
PrintWriter out=response.getWriter();
out.println("<html><body>");
out.println("<h2>"+backNews+"</h2>") ;
out.println("返回");
out.println("<a href=showRecord_teacher.jsp>输入记录</a>");
out.println("</body></html>");
}
catch(IOException exp){}
}
}
值得注意的是,jdbc要用5.0的