我做过一个
applet、jdbc和servlet进行数据库访问
可以吗[email protected]

解决方案 »

  1.   

    不好意思我忘了留自己的信箱了。
    [email protected]
    还请大家多多帮助呀!
      

  2.   

    主要就是applet和servlet的通讯问题!
    下面是两个很简单的程序,只要将访问数据库的代码加在servlet中就行了,显示图形的代码加在applet中。DbServlet.java
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;public class DbServlet extends HttpServlet
    {
      private static final String CONTENT_TYPE = "text/html";
      /**Initialize global variables*/
      public void init() throws ServletException
      {
      }
      /**Process the HTTP Get request*/
      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      {
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();    String query=request.getParameter("query");
        try
        {
          out.println("Succeed!"+query);
        }
        catch(Exception e)
        {
        }
        out.println();
        out.close();
      }
      /**Clean up resources*/
      public void destroy()
      {
      }
    }DbSearchApplet.java
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import javax.swing.*;
    import java.net.*;
    import java.io.*;public class DbSearchApplet extends JApplet implements Runnable
    {
      boolean isStandalone = false;
      JPanel panel1=new JPanel();
      JTextField searchfield=new JTextField("",25);
      JButton excutebutton=new JButton("GO");
      JLabel searchlabel = new JLabel("Search:");
      JScrollPane jScrollPane1 = new JScrollPane();
      JTextArea resultarea = new JTextArea(10,80);
      /**Get a parameter value*/
      public String getParameter(String key, String def)
      {
        return isStandalone ? System.getProperty(key, def) :
          (getParameter(key) != null ? getParameter(key) : def);
      }  /**Construct the applet*/
      public DbSearchApplet()
      {
      }
      /**Initialize the applet*/
      public void init()
      {
        try
        {
          jbInit();
        }
        catch(Exception e)
        {
          e.printStackTrace();
        }
      }
      /**Component initialization*/
      private void jbInit() throws Exception
      {
        this.setSize(new Dimension(400,300));
        panel1.setLayout(new FlowLayout(FlowLayout.LEFT));    excutebutton.addActionListener(new java.awt.event.ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            excutebutton_actionPerformed(e);
          }
        });
        resultarea.setEditable(false);
        panel1.add(searchlabel, null);
        panel1.add(searchfield);    panel1.add(excutebutton);
        this.getContentPane().add(jScrollPane1,  BorderLayout.CENTER);
        jScrollPane1.getViewport().add(resultarea, null);
        this.getContentPane().add(panel1,  BorderLayout.NORTH);
      }
      /**Get Applet information*/
      public String getAppletInfo()
      {
        return "Applet Information";
      }
      /**Get parameter info*/
      public String[][] getParameterInfo()
      {
        return null;
      }
      /**Execute Query*/
      public void executeQuery()
      {
        String input=searchfield.getText();
        try
        {
          URL url=new URL("http://localhost:7001/DbServlet");
          String query=URLEncoder.encode("query")+"="+URLEncoder.encode(input);      URLConnection uc=url.openConnection();
          uc.setDoInput(true);
          uc.setDoOutput(true);
          uc.setUseCaches(false);
          uc.setRequestProperty("Content-type","application/x-www-form-urlencoded");      DataOutputStream dos=new DataOutputStream(uc.getOutputStream());
          dos.writeBytes(query);
          dos.flush();
          dos.close();      InputStreamReader in=new InputStreamReader(uc.getInputStream());      int chr=in.read();
          while(chr!=-1)
          {
    resultarea.append(String.valueOf((char)chr));
    chr=in.read();
          }
          in.close();
        }
        catch(Exception e)
        {
          resultarea.setText(e.toString());
        }
        //resultarea.append(input);
      }
       //static initializer for setting look & feel
      static
      {
        try
        {
          //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        }
        catch(Exception e)
        {
        }
      }  void excutebutton_actionPerformed(ActionEvent e)
      {
        executeQuery();
      }
    }
      

  3.   

    //DataOutputStream dos=new DataOutputStream(uc.getOutputStream());
          
    该行出错!!java.net.UnknownServiceException: protocol doesn't support output为什么???