import java.io.*;
import java.awt.*;
import java.applet.*;public class DataBaseApplet extends Applet implements Runnable
{
  int i;
  String response;
  Thread theThread;  TextArea messInfo=new TextArea("",16,45);
  TextField txtDbIP=new TextField(30)
  ,txtPort=new TextField(4)
  ,txtUID=new TextField(10)
  ,txtPass=new TextField(10)
  ,txtSql=new TextField(30);
  Button btnConnect=new Button("Connect");  public void init()
  { this.setSize(500,300); setLayout(new BorderLayout());
Panel mainPanel=new Panel();
Panel panel1=new Panel();
Panel panel2=new Panel();
Panel panel3=new Panel(); messInfo.setEditable(false);
add("North",messInfo);
//add("North",panel1); panel2.add(new Label("DB IP:"));
panel2.add(txtDbIP);
panel2.add(new Label("Port:4444"));
panel2.add(txtPort);
add("Center",panel2); panel3.add(new Label("username:"));
panel3.add(txtUID);
panel3.add(new Label("password:"));
panel3.add(txtPass);
panel3.add(btnConnect);
add("South",panel3); theThread=new Thread(this);
  }      public void start()
  {
theThread.start();
  }      public void run()
  { while(true)
{
  i++;
  this.req();   // Graphics g=getGraphics();
  //g.drawString("read from server:"+this.response,10,20*i);
  //g.dispose();
  this.messInfo.append("read from server:"+this.response);
  try
  {
theThread.sleep(2000);
  }
  catch(InterruptedException e){}
}  }        public void req()
  {
try
{
  java.net.URL url=
new java.net.URL(getDocumentBase(),"/servlet/DataBaseServlet");
  System.out.println("getDocumentBase(): "+getDocumentBase());
  java.net.URLConnection con = url.openConnection();
  con.setUseCaches(false);
  con.setDoOutput(true);
  con.setDoInput(true);
  
  ByteArrayOutputStream byteout=new ByteArrayOutputStream();
  DataOutputStream out=new DataOutputStream(byteout);
  out.writeUTF("Hello world!?");
  out.flush();
  
  byte buf[]=byteout.toByteArray();
  con.setRequestProperty("Content-type","application/octet-stream");
  con.setRequestProperty("Content-length",""+buf.length);
  
  DataOutputStream dataout=new DataOutputStream(con.getOutputStream());
  dataout.write(buf);
  dataout.flush();
  dataout.close();   DataInputStream in=new DataInputStream(con.getInputStream());
  this.response=in.readUTF();
  //repaint();
  //System.out.println("read from server:"+response);   in.close();
}
//catch(java.net.MalformedURLException e){}
catch(Exception e){
e.printStackTrace();
}
  }                public void paint(Graphics g)
  {
//g.drawString("read from server:"+this.response,10,20);
  }    
}
-------------------------------------------------------
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.naming.*;import javax.sql.*;
public class DataBaseServlet extends HttpServlet{
  String respData;
  public void service(HttpServletRequest req,HttpServletResponse resp)
  {
try
{
  DataInputStream in=new DataInputStream(req.getInputStream());
  resp.setContentType("application/octet-stream");
  ByteArrayOutputStream byteout=new ByteArrayOutputStream();
  DataOutputStream out=new DataOutputStream(byteout);
  this.getDbData();
  String response=in.readUTF()+" "+this.respData+" "+this.nowTime()+"\n";
  System.out.println(response);
  out.writeUTF(response);
  out.flush();
  byte buf[]=byteout.toByteArray();
  resp.setContentLength(buf.length);
  ServletOutputStream servletout=resp.getOutputStream();
  servletout.write(buf);
  servletout.close();
}
catch(java.io.IOException e){
e.printStackTrace();
}
  }                
  public void getDbData()
  {   try
  { String url="jdbc:microsoft:sqlserver://10.0.0.1:1433;DatabaseName=HKISC1103";
String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";    Class.forName(driver).newInstance();    String login = "boom";
  String password = "password";
  Connection con=DriverManager.getConnection(url,login,password);    Statement st = con.createStatement();
   ResultSet rs = st.executeQuery("SELECT * FROM customer");    rs.next();
   this.respData=String.valueOf(rs.getInt("cust_id"))+"tttt";    st.close();
   con.close();   }
catch(java.sql.SQLException e){System.out.println(e.toString());}
catch(java.lang.ClassNotFoundException e){System.out.println(e.toString());}
catch(java.lang.InstantiationException e){System.out.println(e.toString());}
catch(java.lang.IllegalAccessException e){System.out.println(e.toString());}
  }              public String nowTime()
  {
java.util.Calendar currTimeCal=java.util.Calendar.getInstance();
int currDate=currTimeCal.get(Calendar.DATE);
int currYear=currTimeCal.get(Calendar.YEAR);
int currMonth=currTimeCal.get(Calendar.MONTH);
int currHour=currTimeCal.get(Calendar.HOUR_OF_DAY);
int currMinute=currTimeCal.get(Calendar.MINUTE);
int currSecond=currTimeCal.get(Calendar.SECOND); String currTime=String.valueOf(currYear)
  +"-"+String.valueOf(currMonth)
  +"-"+String.valueOf(currDate)
  +":"+String.valueOf(currHour)
  +":"+String.valueOf(currMinute)
  +":"+String.valueOf(currSecond);
return currTime;
  }    
}

解决方案 »

  1.   

    你的代码太长,我也不想看。就给你谈谈我以前做的一个项目吧。
    前台:Applet,中间层:Servlet、JDBC,数据库:SQL Server。
    1、Applet和Servlet之间建立URLConnection,通过连接获取输入流、输出流对象。
    2、Applet发送请求。
    3、Servlet访问SQL Server,获取数据,将数据组装成对象。
    4、Servlet将对象发送到Applet。
    5、Applet操作对象。
    一点问题也没有,你的问题出在1-5中哪一步?
    在线回答,仅限今天上午。
      

  2.   

    这里有一个例子,applet向servlet传一个对象,servlet收到后发一个回执给applet。applet端程序:
    try {
      URL urlSave = new URL(getCodeBase(), "/wmc/ProcessSaver");
      HttpURLConnection conn = (HttpURLConnection)urlSave.openConnection();
      conn.setDoOutput(true);
      ObjectOutputStream objOut = new ObjectOutputStream(conn.getOutputStream());
      objOut.writeObject(procItem);
      objOut.flush();
      objOut.close();
      if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
        System.err.println("Http error!");
      }
      DataInputStream dataIn = new DataInputStream(conn.getInputStream());
      int id = dataIn.readInt();
      dataIn.close();
    } catch (Exception e) {
      e.printStackTrace();
    }servlet端程序:
    try {
      ObjectInputStream objIn = new ObjectInputStream(req.getInputStream());
      ProcessItem procItem = (ProcessItem)objIn.readObject();
      objIn.close();
      DataOutputStream dataOut = new DataOutputStream(res.getOutputStream());
      dataOut.writeInt(id);
      dataOut.flush();
      dataOut.close();
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
      

  3.   

    对, sojkey的代码和我以前做的项目代码非常相似。
      

  4.   

    稍等,我正在试!
    我好象不能把对象传到APPLET里