www.pudn.com > StudentDBServlet_java.rar > StudentDBServlet.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
import java.io.*;
/**
* Title: 三层结构数据库应用系统
* Description: Java语言演示程序:三层结构数据库应用系统,用于北京师范大学计算机系Java课程教学示范。
* Copyright: Copyright (c) 2002
* Company: 北京师范大学计算机系
* @author 孙一林
* @version 1.0
*/
public class StudentDBServlet extends HttpServlet{
protected Connection dbConnection;
protected PreparedStatement displayStatement;
protected PreparedStatement registerStatement;
private static final String CONTENT_TYPE = "text/html;charset=gb2312";
protected String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
protected String dbURL = "jdbc:odbc:student";
protected String userID = "student";
protected String passwd = "123456";
protected String CR = "\n";
protected final int NUMBER_POSITION = 1;
protected final int NAME_POSITION = 2;
protected final int AGE_POSITION = 3;
protected final int SEX_POSITION = 4;
protected final int DEPARTMENT_POSITION = 5;
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
System.out.println("开始初始化");
System.out.println("加载数据库驱动程序");
Class.forName(driverName);
System.out.println("开始连接数据库:" + dbURL);
dbConnection = DriverManager.getConnection(dbURL, userID, passwd);
System.out.println("准备显示数据库数据");
displayStatement = dbConnection.prepareStatement("select * from studentbase");
System.out.println("准备添加数据库记录");
registerStatement = dbConnection.prepareStatement("insert into Studentbase" + "(学号, 姓名, 年龄, 性别, 系别)" + " values ( ?, ?, ?, ?, ?)");
System.out.println("初始化准备工作结束");
}
catch (Exception e){
cleanUp();
e.printStackTrace();
}
}
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userOption = null;
userOption = request.getParameter("Register");
if (userOption != null){
registerStudent(request, response);
}
else{
displayStudents(request, response);
}
}
public void displayStudents(HttpServletRequest request,HttpServletResponse response){
Student aStudent = null;
try {
String htmlHead = "学生记录 " + CR;
String htmlBody = "" + CR;
htmlBody += "学生记录
" + CR;
htmlBody += "
" + CR;
String tableHead = "
" + CR;
tableHead += "" + CR;
tableHead += " " + CR;
tableHead += "学号 " + CR;
tableHead += "姓名 " + CR;
tableHead += "年龄 " + CR;
tableHead += "性别 " + CR;
tableHead += "系别 " + CR;
tableHead += " " + CR;
ResultSet dataResultSet = displayStatement.executeQuery();
String tableBody = "";
int rowNumber = 1;
while (dataResultSet.next()){
aStudent = new Student(dataResultSet);
tableBody += aStudent.toTableString(rowNumber);
rowNumber++;
}
dataResultSet.close();
String tableBottom = "
";
String htmlBottom = "";
htmlBody += tableHead + tableBody + tableBottom;
htmlBody += "
";
htmlBody += "返回首页";
htmlBody += "" + this.getServletInfo() + "";
htmlBody += "
";
String htmlPage = htmlHead + htmlBody + htmlBottom;
PrintWriter outputToBrowser = new PrintWriter(response.getOutputStream());
response.setContentType(CONTENT_TYPE);
outputToBrowser.println(htmlPage);
outputToBrowser.close();
}
catch (Exception e)
{
cleanUp();
e.printStackTrace();
}
}
public void registerStudent(HttpServletRequest request,HttpServletResponse response){
try {
Student aStudent = new Student(request);
registerStatement.setString(NUMBER_POSITION, aStudent.getNumber());
registerStatement.setString(NAME_POSITION, aStudent.getName());
registerStatement.setString(AGE_POSITION, aStudent.getAge());
registerStatement.setString(SEX_POSITION, aStudent.getSex());
registerStatement.setString(DEPARTMENT_POSITION, aStudent.getDepartment());
registerStatement.executeUpdate();
String htmlPage = "确认 ";
htmlPage += "";
htmlPage += "确认正确与否
";
htmlPage += "以下为输入的信息";
htmlPage += aStudent.toWebString();
htmlPage += "
";
htmlPage += "返回首页 | ";
htmlPage += "查看记录";
htmlPage += "" + this.getServletInfo() + "";
htmlPage += "
";
PrintWriter outputToBrowser = new PrintWriter(response.getOutputStream());
response.setContentType(CONTENT_TYPE);
outputToBrowser.println(htmlPage);
outputToBrowser.close();
}
catch (Exception e) {
cleanUp();
e.printStackTrace();
}
}
public void cleanUp(){
try {
System.out.println("关闭数据库连接");
dbConnection.close();
}
catch (SQLException e){
e.printStackTrace();
}
}
public void destroy() {
System.out.println("撤消StudentDBServlet程序");
cleanUp();
}
public String getServletInfo(){
return "在线学生注册系统 v1.0";
}
}