import java.io.*;
import java.net.*;
import java.sql.*;
import com.sun.rowset.*;
import javax.sql.rowset.*;
import java.util.*;
import java.awt.geom.*;
public class Req 
{ public static void main(String args[])
{
try
{
URL url = new URL("http://localhost/workingroom/exapplet/PostToApplet?DBSQL=Select%20*%20from%20rkmx");
        //注意url中的空格用%20替代/exapplet/PostToApplet
        URLConnection urlcon = url.openConnection();
        urlcon.connect();
           
        ObjectInputStream ois=new ObjectInputStream(urlcon.getInputStream());
        CachedRowSetImpl crs = (CachedRowSetImpl)ois.readObject();
        
        
        ois.close();
        ResultSetMetaData rsmd = (ResultSetMetaData)crs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            
if(crs.next())
{
            for(int k = 1; k < columnCount + 1; k ++)
            {
             System.out.println(crs.getString(k));
            }
            }
            crs.close();
            System.out.println("Changed");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
在tomcat下新建一工程,在server.xml中添加如下语句
<Context path="/workingroom" docBase="D:\WorkingRoom" debug="0" reloadable="true"/>
然后在D盘根目录下新建一文件夹名字叫WorkingRoom然后在该目录下建立文件夹WEB-INF(注意必须为大写),接着在该目录下建立文件夹classes和文件web.xml
其中web.xml文件内容为
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
 <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>exapplet.MyServlet</servlet-class>
 </servlet>
 <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/exapplet/MyServlet</url-pattern>
 </servlet-mapping>
</web-app>

解决方案 »

  1.   

    然后在classes目录下继续建一文件夹名称为exapplet接着在该文件夹下面建立名称为MyServlet.java的程序,内容为:
    package exapplet;import com.sun.rowset.*;
    import javax.sql.rowset.*;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.sql.*;
    import javax.servlet.ServletException;
    import javax.servlet.http.*;public class MyServlet extends HttpServlet
    {    public void doGet(HttpServletRequest httpservletrequest,   
            HttpServletResponse httpservletresponse)
            throws ServletException, IOException
        {
            System.out.println("Start");
            try
            {
                String s = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=gysys";
                Connection connection = DriverManager.getConnection(s, "sa", "");
                Statement statement = connection.createStatement();    String s1 = httpservletrequest.getParameter("DBSQL");
       ResultSet resultset = statement.executeQuery(s1);
       CachedRowSetImpl cachedrowsetimpl = new CachedRowSetImpl();
       cachedrowsetimpl.populate(resultset);
       resultset.close();
       statement.close();
       connection.close();
       httpservletresponse.setContentType("application/octet-stream");
       ObjectOutputStream objectoutputstream = new ObjectOutputStream(httpservletresponse.getOutputStream());
                
       objectoutputstream.writeObject(cachedrowsetimpl);
       System.out.println("before");
       while(cachedrowsetimpl.next())
       {
           System.out.println(cachedrowsetimpl.getString(1));
           System.out.println(cachedrowsetimpl.getString(2));
           System.out.println(cachedrowsetimpl.getString(3));
           System.out.println("in");
       }         
       objectoutputstream.close();
            }
            catch(Exception exception)
            {
                exception.printStackTrace();
            }
        }    public String getServletInfo()
        {
            return "A simple Servlet!";
        }    static 
        {
            try
            {
                Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
            }
            catch(Exception exception)
            {
                exception.printStackTrace();
            }
        }
    }
    编译之后,启动tomacat,然后运行Req即可看到结果!
    祝好运!
      

  2.   

    CachedRowSetImpl是什么?RowSet和ResultSet有什么区别?
      

  3.   

    缓存行集,你看他的单词,cache就知道是什么了
      

  4.   

    CachedRowSetImpl是关键字吗在JBUILDER中不认,import com.sun.rowset.*;中没有*这个项.
      

  5.   

    package pz;
    import javax.sql.RowSet.*;
    import java.sql.*;
    import com.sun.rowset.*;
    import java.util.*;
    public class lgrowset
    {
    public static void main(String[] args)
    {
    try
    {
    Connection conn =null;
                      Statement stmt=null;
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
       String url="jdbc:microsoft:sqlserver://192.168.180.31:1433;DatabaseName=ht_htcw"; 
    String user="sa"; 
      String password="12345678"; 
       conn= DriverManager.getConnection(url,user,password); 
       stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
       String sql="select * from zwpzmb";
       ResultSet rs= stmt.executeQuery(sql);
                      CachedRowSetImpl cachedrowsetimpl = new CachedRowSetImpl();
                               CachedRowSetImpl.populate(rs);
       System.out.println("数据库连接成功!");
    }
    catch(Exception e)
    {
    System.out.println(e);
    System.out.println("数据库连接失败!");
         }
    }
    }
    为什么没有populate(rs)方法,程序这样写对吗?
      

  6.   

    要导入import javax.sql.rowset.*;不是import javax.sql.RowSet.*;
      

  7.   

    package pz;
    import javax.sql.rowset.*;
    import java.sql.*;
    import javax.sql.*;
    import com.sun.rowset.*;
    import java.util.*;
    public class lgrowset
    {
    public static void main(String[] args)
    {
    try
    {
                             CachedRowSetImpl cachedrowsetimpl=new CachedRowSetImpl();
                      String Sql="select * from zwpzmb";
                      cachedrowsetimpl.setUrl("jdbc:microsoft:sqlserver://192.168.180.31:1433;DatabaseName=ht_htcw");
              cachedrowsetimpl.setUsername("sa");
              cachedrowsetimpl.setPassword("12345678");
              cachedrowsetimpl.setCommand(Sql);
              cachedrowsetimpl.execute();
              while(cachedrowsetimpl.next())
                    {
                         System.out.println(cachedrowsetimpl.getString(1));
                         System.out.println(cachedrowsetimpl.getString(2));
                         System.out.println(cachedrowsetimpl.getString(3));
                         System.out.println("in");
                    }         
             System.out.println("数据库连接成功!");
    }
    catch(Exception e)
    {
    System.out.println(e);
    System.out.println("数据库连接失败!");
                  }
    }
    }
    报了如下错误:
    java.sql.SQLException: No suitable driver
    数据库连接失败!
    数据库驱动包我已经加上了,已经用它做了一个项目了,肯定没有问题,为什么driver错误?这是怎么回事?
      

  8.   

                      CachedRowSetImpl cachedrowsetimpl = new CachedRowSetImpl();
                               CachedRowSetImpl.populate(rs);
    你这里就错了嘛                   CachedRowSetImpl cachedrowsetimpl = new CachedRowSetImpl();
                               cachedrowsetimpl.populate(rs);
      

  9.   

    这个错我找到了.
    java.sql.SQLException: No suitable driver是怎么回事?
      

  10.   

    没有JDBC驱动啊,还能会是怎么回事
      

  11.   

    JDBC驱动应该怎么加啊,不就是加上SQLSERVER(我用的是SQLSERVER数据库)的三个包吗?
      

  12.   

    你第一段程序的错误在于
    CachedRowSetImpl cachedrowsetimpl = new CachedRowSetImpl();
    CachedRowSetImpl.populate(rs);
    这里,你说找到了,
    你第于段程序的错误在于
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
    这句没加
      

  13.   

    怎么添加addRowSetListener事件监听这句话应该怎么写.
    那个事件的实现应该写在哪,是main里面吗?