您的位置:程序门 -> java -> web 开发



请教servlet查询数据库并和applet通信问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


请教servlet查询数据库并和applet通信问题
发表于:2007-06-06 21:19:34 楼主
思路:通过applet输入sql代码并点击查询按钮来请求servlet响应,servlet通过访问指定数据库得到相应的查询记录并以resultset数据集对象回送给applet,applet以jtable格式显示查询记录。。
ide:jbuilder2006
数据库:mysql
服务器:tomcat5.5

帮忙看下为什么调不通。。。。。自己照着一个applet和servlet通信的例子改的。。大家有时间的话帮忙调下,看看问题在哪儿?共同进步,谢谢。

源代码如下:
applet:
package   appletdemo;

import   java.awt.*;
import   java.awt.event.*;
import   java.applet.*;
import   java.net.*;
import   java.util.*;
import   java.io.*;
import   java.sql.*;
import   com.sun.rowset.*;
import   javax.swing.jscrollpane;
import   javax.swing.jbutton;
import   javax.swing.jtextarea;
import   javax.swing.jtable;
import   javax.swing.borderfactory;
import   javax.swing.table.defaulttablemodel;
import   com.borland.jbcl.layout.xylayout;
import   com.borland.jbcl.layout.*;

public   class   applet1   extends   applet   {
        boolean   isstandalone   =   false;
        borderlayout   borderlayout1   =   new   borderlayout();
        jbutton   jbutton1   =   new   jbutton();
        jtextarea   jtextarea1   =   new   jtextarea();
        xylayout   xylayout1   =   new   xylayout();
        hashtable   hash   =   new   hashtable();
        cachedrowsetimpl   crsi;
        jtable   jtable   =   new   jtable();
        defaulttablemodel   dtm   =   new   defaulttablemodel();
        jscrollpane   jscrollpane1   =   new   jscrollpane();
        public   void   init()   {
                try   {
                        jbinit();
                }   catch   (exception   e)   {
                        e.printstacktrace();
                }
        }

        public   void   invoke()   {
                try   {
                        url   url   =   new   url(getcodebase(),   "/servlet1 ");
                        urlconnection   con   =   url.openconnection();
                        con.setdooutput(true);
                        con.setusecaches(false);
                        con.setrequestproperty( "content-type ",   "application/octet-stream ");
                        objectoutputstream   outstream   =   new   objectoutputstream(con.
                                        getoutputstream());
hashtable   setmsg   =   new   hashtable();
                        setmsg.put( "action ",   jtextarea1.gettext());
                        outstream.writeobject(setmsg);
                        outstream.flush();
                        outstream.close();
objectinputstream   instream   =   new   objectinputstream(con.
                                        getinputstream());
cachedrowsetimpl   crs   =   (cachedrowsetimpl)   instream.readobject();
crsi   =   crs;
                }   catch   (exception   e)   {}
        }

        //component   initialization
        private   void   jbinit()   throws   exception   {
                jbutton1.settext( "查询 ");
                jbutton1.addactionlistener(new   applet1_jbutton1_actionadapter(this));
                jtextarea1.setborder(borderfactory.createetchedborder());
                jtextarea1.settext( "select   *   from   test ");
                this.setlayout(xylayout1);
                jscrollpane1.setborder(borderfactory.createloweredbevelborder());
                this.add(jbutton1,   new   xyconstraints(142,   13,   -1,   -1));
                this.add(jtextarea1,   new   xyconstraints(80,   48,   199,   25));
                this.add(jscrollpane1,   new   xyconstraints(35,   85,   320,   183));
                jtable.setmodel(dtm);
                jscrollpane1.getviewport().add(jtable);
        }

        public   void   jbutton1_actionperformed(actionevent   e)   {
                this.invoke();
                resultset   rs;
                int   columncount;
                try   {
                        rs   =   crsi.getoriginal();
                        columncount   =   rs.getmetadata().getcolumncount();
                        for   (int   i   =   1;   i   <=   columncount;   i++)   {
                                dtm.addcolumn(rs.getmetadata().getcolumnname(i));
                        }
                        string[]   s   =   new   string[columncount];
                        while   (rs.next())   {
                                for   (int   i   =   1;   i   <   columncount;   i++)   {
                                        s[i   -   1]   =   rs.getstring(i);
                                }
                                dtm.addrow(s);
                        }
                }   catch   (exception   ex)   {
                        ex.printstacktrace();
                }
        }
}


class   applet1_jbutton1_actionadapter   implements   actionlistener   {
        private   applet1   adaptee;
        applet1_jbutton1_actionadapter(applet1   adaptee)   {
                this.adaptee   =   adaptee;
        }

        public   void   actionperformed(actionevent   e)   {
                adaptee.jbutton1_actionperformed(e);
        }
}

servlet代码:
package   servletdemo;

import   java.io.*;
import   java.sql.*;
import   com.sun.rowset.*;
import   javax.servlet.*;
import   java.util.*;
import   javax.servlet.http.*;


public   class   servlet1   extends   httpservlet   {
        private   static   final   string   content_type   =   "text/html;   charset=gbk ";
        string   action   =   "succeed! ";
        public   void   init()   throws   servletexception   {}

        static   {
                try   {
                        class.forname( "com.mysql.jdbc.driver ");
                }   catch   (exception   e)   {
                        e.printstacktrace();
                }
        }

        //process   the   http   get   request
        public   void   doget(httpservletrequest   req,   httpservletresponse   res)   throws   servletexception,   ioexception   {
        objectinputstream   in   =   new   objectinputstream(req.getinputstream());
                hashtable   getmsg   =   new   hashtable();
                try   {
                        getmsg   =   (hashtable)   in.readobject();

                }   catch   (exception   e)   {}

                try   {
                        connection   dbconn   =   drivermanager.getconnection(
                                        "jdbc:mysql://localhost:3306/test ",   "root ",
                                        "password ");
                        statement   stmt   =   dbconn.createstatement();
                        resultset   dbrest   =   stmt.EXECutequery(getmsg.tostring());
                        cachedrowsetimpl   crs   =   new   cachedrowsetimpl();
                        crs.populate(dbrest);

                        dbrest.close();
                        stmt.close();
                        dbconn.close();
                        file:   //传回对象给applet
                        res.setcontenttype( "application/octet-stream ");
                        objectoutputstream   oos   =   new   objectoutputstream(res.getoutputstream());
                        oos.writeobject(crs);
                        oos.close();
                }   catch   (exception   exp)   {
                        exp.printstacktrace();
                }
        }

        public   void   destroy()   {
        }
}
发表于:2007-06-06 21:21:541楼 得分:0
string     action     =     "succeed! ";    

这行好像没用:)

大家耐心看看,帮帮忙


快速检索

最新资讯
热门点击