当前位置:Linux教程 - Linux - 如何在servlet中实时地创建图象

如何在servlet中实时地创建图象



        


    linuxbird


    import javax.servlet.* ;
    import javax.servlet.http.* ;
    import java.io.* ;
    import java.sql.* ;
    import java.util.Vector;
    import oracle.jdbc.driver.*;
    import java.util.Enumeration;
    import java.util.Properties;
    import com.unitech.connectionpool.* ;


    public class dbTest extends HttpServlet {
    //Initialize global variables
    public void init(ServletConfig config) throws ServletException {
    super.init(config);

    }
    // 数据库连接:Connetcion conn = null ;
    Connection conn = null ;
    //数据库查询内容执行:Statement stment = null ;
    Statement stment = null ;
    // 数据库连接池的初始化
    DBConnectionManager connMgr = DBConnectionManager.getInstance();

    //初始化数据库连接池,并且获取数据库连接
    private void initDatabaseDriver () {
    conn = connMgr.getConnection("oracle");
    if (conn == null) {
    System.out.println("数据库连接失败。");
    return;
    }
    try {
    stment = conn.createStatement();
    }
    catch (SQLException e) {
    e.printStackTrace() ;
    }
    }

    //释放数据库连接
    private void freeConnectionPool() {
    connMgr.freeConnection("oracle", conn) ;
    }

    //获取记录集,并返回给VERTOR V
    public Vector getForumList() {
    String[] s = {"","","","",""} ;//与选取的列数相等。
    Vector v = new Vector() ;
    this.initDatabaseDriver();
    try{
    String queryStr = null ;
    queryStr = "SELECT BBS_ID,BBS_NAME,DESCRIPTION,MANAGER_ID,CREATE_DATE FROM BBS WHERE IS_SYSTEM= ORDER BY CREATE_DATE DESC" ;
    ResultSet rSet = stment.executeQuery(queryStr) ;
    while (rSet.next()) {
    s[0] = Integer.toString(rSet.getInt("BBS_ID")) ;
    s[1] = rSet.getString("BBS_NAME") ;
    s[2] = rSet.getString("DESCRIPTION") ;
    s[3] = rSet.getString("MANAGER_ID") ;
    Timestamp createdate = rSet.getTimestamp("CREATE_DATE") ;
    String tmp = createdate.toString() ;
    s[4] = tmp.substring(0,(tmp.length()-2)) ;
    v.addElement(s.clone());
    }
    rSet.close();
    stment.close();
    this.freeConnectionPool();
    }
    catch(Exception e) {
    try {
    stment.close();
    this.freeConnectionPool();
    }
    catch(SQLException ee) {
    ee.printStackTrace();
    }
    e.printStackTrace() ;
    }
    return v ;
    }

    //Process the HTTP Get request
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    PrintWriter out = new PrintWriter (response.getOutputStream());
    response.setContentType("text/html");
    out.println("");
    out.println("The servlet has received a GET. This is the reply.");
    out.println("");
    out.println("");

    // 将记录集循环输出到页面。
    Vector v = new Vector() ;
    v = this.getForumList() ;
    for (int i=0; i"
    + ""+s[0]+""
    + ""+s[1]+""
    + ""+s[2]+""
    + ""+s[3]+""
    + ""+s[4]+"");
    }
    out.println("");
    out.close();
    }
    }



    发布人:netbull 来自:Linux技术支持站点