一个J2EE实现的反面例子续(java代码)
- Index.java
- import classnotepad.*;
- import javax.servlet.http.*;
- import javax.servlet.*;
- import java.io.*;
- public class index extends {
- public static ConnectionPool pool;
- /** init the index servlet and init db connection pool*/
- public void init( config) throws {
- super.init(config);
- pool = ConnectionPool.instance();
- pool.createConnection();
- }
- public void doGet( request, response) throws , {
- response.setContentType("text/html; charset=gb2312");
- out = response.getWriter();
- out.println("<html>");
- out.println("<title>XX同学留言簿</title>");
- out.println(" <body bgColor=#456589 >");
- out.println(" <head><CENTER><h2>XX同学留言簿登陆页面 </head>");
- out.println(" <form action=/ustc9611/liuyan.jsp method=POST>");
- out.println(" 请输入登陆名 <CENTER>");
- out.println(" 姓名:<input type=text name=id><CENTER><BR>");
- out.println(" 请输入密码 <CENTER>");
- out.println(" 密码:<input type=password name=passwd><CENTER><BR>");
- out.println(" <input type=submit value=确定><input type=reset value=取消><CENTER>");
- out.println(" </form>");
- out.println(" <form action=/ustc9611/zhuce.html method=POST>");
- out.println(" 注册新用户");
- out.println(" <input type=submit value=确定>");
- out.println(" </form>");
- out.println(" <BR>");
- out.println(" <form action=/ustc9611/visit.jsp method=POST>");
- out.println(" 查询同学最近登陆时间<input type=submit value=确定>");
- out.println(" </form>");
- out.println(" </body>");
- out.println("</html>");
- }
- public void doPost( request, response) throws , {
- doGet(request,response);
- }
- /** destroy the index servlet and close the connections in pool*/
- public void destroy(){
- super.destroy();
- pool.closeConnection();
- pool = null;
- }
- }
- LiuYanBean.java
- package classnotepad;
- import java.util.*;
- import java.io.*;
- import java.sql.*;
- import javax.servlet.jsp.*;
- public class LiuYanBean{
- private id;
- private text;
- private subject;
- private passwd;
- private hash;
- private conn;
- private statement;
- private stmt;
- private static int classID = 1;
- private ConnectionPool pool;
- public LiuYanBean(){
- id = null;
- text = null;
- pool = ConnectionPool.instance();
- }
- /** id */
- public void setId( ID){
- id = ID;
- }
- public getId(){
- return id;
- }
- /** passwd */
- public getPasswd(){
- return passwd;
- }
- public void setPasswd( pwd){
- passwd = pwd;
- }
- /** text */
- public void setText( ptext){
- text = ptext;
- }
- public getText(){
- return text;
- }
- /** subject */
- public void setSubject( psub){
- subject = psub;
- }
- public getSubject(){
- return subject;
- }
- /** show the registed users leaved words */
- public void show( out){
- try{
- openConnection();
- statement = conn.createStatement();
- select = "select user_name,title,context,create_dt from coninfo where class_id ="
- +classID+" order by create_dt DESC";
- set = statement.executeQuery(select);
- while(set.next()){
- out.println("留言人:"+set.getString(1));
- out.println("<p>");
- out.println("标题:"+set.getString(2));
- out.println("<p>");
- out.println("<pre>");
- // byte[] db = set.getString(3).getBytes("ISO-8859-1");
- out.println( set.getString(3) );
- out.println("</pre>");
- out.println("<p>");
- out.println("<right>留言时间:"+set.getString(4));
- out.println("<p>");
- out.println("<center>-------------------------------------------------------------------------------------------------------------------</center>");
- out.println("<p>");
- out.println("<BR>");
- }
- }catch( sqle){sqle.printStackTrace();}
- catch( ioe){ioe.printStackTrace();}
- // catch(java.io.UnsupportedEncodingException ue){ue.printStackTrace();}
- finally{
- closeConnection();
- }
- }
- /** show the registed users visiting dates */
- public void showVisitDate( out){
- try{
- openConnection();
- statement = conn.createStatement();
- select = "select user_name,visit_dt from passwd where class_id ="
- +classID+" order by visit_dt DESC";
- set = statement.executeQuery(select);
- while(set.next()){
- out.println("<pre>");
- out.println("访问者:"+set.getString(1)+" 最新访问时间:"+set.getString(2));
- out.println("---------------------------------------------------------------------------------------------------------------");
- out.println("</pre>");
- }
- }catch( sqle){sqle.printStackTrace();}
- catch( ioe){ioe.printStackTrace();}
- // catch(java.io.UnsupportedEncodingException ue){ue.printStackTrace();}
- finally{
- closeConnection();
- }
- }
- public void addLiuYan(){
- if(subject==null || text==null) return;
- try{
- openConnection();
- statement = conn.createStatement();
- java.util. dt = .getInstance().getTime();
- insert = "insert into coninfo(class_id,user_name,title,context,create_dt) values("+classID+","
- //+new String(id.getBytes(),"ISO-8859-1")+","+new String(subject.getBytes(),"ISO-8859-1")+","+new String(text.getBytes(),"ISO-8859-1")+","+"Date())";//Date()是access的内部函数
- +id+","+subject+","+text+","+"getdate())";//Date()是access的内部函数
- statement.executeUpdate(insert);
- }catch( sqle){sqle.printStackTrace();}
- // catch(java.io.UnsupportedEncodingException ue){ue.printStackTrace();}
- finally{
- closeConnection();
- }
- }
- public boolean check(){
- boolean flag = false;
- if (id == null ) return flag;
- try{
- openConnection();
- statement = conn.createStatement();
- search = "select * from passwd where class_id = "+classID+" and user_name = "+id+" and passwd = "+passwd+"";
- set = statement.executeQuery(search);
- if(set.next()){
- flag = true;
- }
- if(flag){
- buffer = new ();
- buffer.append("update passwd set visit_dt = getdate() where class_id = ");
- buffer.append(classID);
- buffer.append(" and user_name = ");
- buffer.append(id);
- buffer.append(" and passwd = ");
- buffer.append(passwd);
- buffer.append("");
- statement.executeUpdate(buffer.toString());
- }
- }catch( sqle){sqle.printStackTrace();
- }finally{
- closeConnection();
- }
- return flag;
- }
- private void openConnection(){
- hash = pool.getConnection();
- if(hash.elements().hasMoreElements()){
- conn = ()hash.elements().nextElement();
- }
- }
- private void closeConnection(){
- try{
- if(statement!= null){
- statement.close();
- }
- if(stmt!=null){
- stmt.close();
- }
- statement = null;
- stmt = null;
- pool.putConnection(hash);
- }catch( sqle){sqle.printStackTrace();}
- }
- }
- UploadBean.java
- package classnotepad;
- import java.util.*;
- import java.io.*;
- import java.sql.*;
- import javax.servlet.jsp.*;
- public class UploadBean{
- private id;
- private passwd;
- private imageName;
- private subject;
- private hash;
- private conn;
- private statement;
- private stmt;
- private static int classID = 1;
- private ConnectionPool pool;
- public UploadBean(){
- id = null;
- imageName = null;
- pool = ConnectionPool.instance();
- }
- /** id */
- public void setId( ID){
- id = ID;
- }
- public getId(){
- return id;
- }
- /** passwd */
- public getPasswd(){
- return passwd;
- }
- public void setPasswd( pwd){
- passwd = pwd;
- }
- /** imageName */
- public void setImageName( imgName){
- imageName = imgName;
- }
- public getImageName(){
- return imageName;
- }
- /** subject */
- public void setSubject( sub){
- subject = sub;
- }
- public getSubject(){
- return subject;
- }
- public void addImageInfo(){
- if( imageName == null ) return;
- try{
- openConnection();
- statement = conn.createStatement();
- java.util. dt = .getInstance().getTime();
- insert = "insert into imageinfo(class_id,create_dt,image_name,title) values("+classID+","
- //+new String(id.getBytes(),"ISO-8859-1")+","+new String(subject.getBytes(),"ISO-8859-1")+","+new String(text.getBytes(),"ISO-8859-1")+","+"Date())";//Date()是access的内部函数
- +"getdate(),"+imageName+","+subject+")";//Date()是access的内部函数
- statement.executeUpdate(insert);
- }catch( sqle){sqle.printStackTrace();}
- // catch(java.io.UnsupportedEncodingException ue){ue.printStackTrace();}
- finally{
- closeConnection();
- }
- }
- private void openConnection(){
- hash = pool.getConnection();
- if(hash.elements().hasMoreElements()){
- conn = ()hash.elements().nextElement();
- }
- }
- private void closeConnection(){
- try{
- if(statement!= null){
- statement.close();
- }
- if(stmt!=null){
- stmt.close();
- }
- statement = null;
- stmt = null;
- pool.putConnection(hash);
- }catch( sqle){sqle.printStackTrace();}
- }
- }
- AddUserBean.java
- package classnotepad;
- import java.util.*;
- import java.sql.*;
- public class AddUserBean{
- private id;
- private passwd;
- private confirm;
- private connection;
- private statement;
- private stmt;
- private static hash;
- private ConnectionPool pool;
- public AddUserBean(){
- id = null;
- passwd = null;
- pool = ConnectionPool.instance();
- }
- public void setId( ID){
- id = ID;
- }
- public getId(){
- return id;
- }
- public void setConfirm( con){
- confirm = con;
- }
- public getConfirm(){
- return confirm;
- }
- public void setPasswd( pswd){
- passwd = pswd;
- }
- public getPasswd(){
- return passwd;
- }
- public boolean isNewUser(){
- try{
- openConnection();
- statement = connection.createStatement();
- search = "select * from passwd where class_id=1 and user_name= "+id+"";
- set = statement.executeQuery(search);
- if( set.next()){
- return false;
- }
- }catch( sqle){sqle.printStackTrace();}
- finally{closeConnection();}
- return true;
- }
- public boolean addUser(){
- try{
- openConnection();
- statement = connection.createStatement();
- insert = "insert into passwd(class_id,user_name,passwd) values( 1,"
- +""+this.id+","+this.passwd+")";
- statement.executeUpdate(insert);
- return true;
- }catch( sqle){sqle.printStackTrace();}
- finally{
- closeConnection();
- }
- return false;
- }
- private void openConnection(){
- hash = pool.getConnection();
- if(hash.elements().hasMoreElements()){
- connection = ()hash.elements().nextElement();
- }
- }
- private void closeConnection(){
- try{
- if(statement!= null){
- statement.close();
- }
- if(stmt!=null){
- stmt.close();
- }
- statement = null;
- stmt = null;
- pool.putConnection(hash);
- }catch( sqle){sqle.printStackTrace();}
- }
- }
- ConnectionPool.java
- package classnotepad;
- import java.sql.*;
- import java.util.*;
- public final class ConnectionPool{
- private static int DB_MAX = 4;
- private int STATUS_UNINIT = 1;//未初始化状态
- private int STATUS_PREPARED = 2;//已经初始化状态
- private int STATUS_INUSED = 3;//正在使用状态
- private int STATUS_CLOSED = 4;//关闭状态
- private int status[] = {STATUS_UNINIT,STATUS_UNINIT,STATUS_UNINIT,STATUS_UNINIT};
- private static boolean singleFlag = false;
- private static [] connection = new [DB_MAX];
- private final sqlDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
- private final url = "jdbc:odbc:liuyan";
- private final user = "administrator";
- private final passwd = "";
- private static ConnectionPool pool;
- /** singleton design pattern */
- private ConnectionPool(){
- for(int i =0;i < DB_MAX;i++){
- connection[i] = null;
- }
- // createConnection();
- }
- /** create a instance pool */
- public static synchronized ConnectionPool instance(){
- if(!singleFlag){
- singleFlag = true;
- pool = new ConnectionPool();
- }
- return pool;
- }
- /** init connection pool */
- &nb;