Re: CachedRowSet

"[Michelle Huang]" <[email protected]>
Newsgroups gmane.comp.java.sun.servlet
Message-ID <7EDB294F3EE1744BA2AB357D5C39579803F26B19@us-bv-m05>
Ben,

The answer is yes.
Here is my sample code of using CachedRowSet (a JavaBean) in a Java class
(another JavaBean).

Hope this is what you want.

Michelle


----------------------------------------------------------------------------
---------------
import java.sql.*;                      //Connection
import javax.sql.*;                     //DataSource
import javax.naming.*;          //JNDI
import sun.jdbc.rowset.*;       //using a CachedRowSet

/**
 * The DatabaseManager class handles database connectivity and
communication.
 */
public class DatabaseManager {


//**************************************************************************
    // CLASS VARIABLES

//**************************************************************************

    final private static boolean DEBUG = false;

    //*****************************************************************
    // METHODS
    //*****************************************************************

    /**
     * Opens a database connection.
     *
     * @return  Connection to the MySQL database via a JDBC DataSource.
     * lookup the DataSource using JNDI.
     */
    private Connection getConnection() {
        Connection con = null;
        try {
                InitialContext jndiContext = new InitialContext();
                DataSource ds =
(DataSource)jndiContext.lookup("java:comp/env/jdbc/mydatabase");
                con = ds.getConnection();
        }
        //Catch SQLException, ClassNotFoundException, and NamingException
        catch (Exception e) {e.printStackTrace();}
        return con;
    }


    /**
     * Gets a JDBC Connection, executes the specified SQL query,
     * and returns a ResultSet containing the result of the query,
     * and load a CachedRowSet with a ResultSet, and then close the
Connection
     *
     * @param SQL SQL query
     * @return CachedRowSet representing the records that matched the SQL
query.
     */
    public ResultSet executeQuery(String SQL) {

        ResultSet rsReturn = null;
        CachedRowSet crs = null;

        try {
                //Get a database Connection
                Connection con = getConnection();

                //Create a connection Statement
                Statement stmt = con.createStatement();

                //Query the database and return a ResultSet
                rsReturn = stmt.executeQuery(SQL);

                //Load a disconnected CachedRowSet
                //and populate it with the query's result set
                crs = new CachedRowSet();
                crs.populate(rsReturn);

                //Release the JDBC connection back to the connection pool
                con.close();

                if (DEBUG) {System.out.println(SQL);}
        }
        catch (SQLException se) {se.printStackTrace();}
        return crs;
    }

........

}

----------------------------------------------------------------------------
---------------------

-----Original Message-----
From: Benoit Jodoin [mailto:[email protected]]
Sent: Thursday, June 12, 2003 8:10 AM
To: [email protected]
Subject: Re: CachedRowSet


Is there a way i can use a Bean in a java class?

thanks

Ben


On Thu, 2003-06-12 at 10:43, [Michelle Huang] wrote:
> Ben,
>
> try this:
> import sun.jdbc.rowset.*;
> CachedRowSet crs = new CachedRowSet();
>
> from the document that you pointed out, it said:
> Because it is a JavaBean, you can simply use the default constructor when
> creating a new instance of the CachedRowSet object:
> CachedRowSet crs = new CachedRowSet();
>
> Regards,
>
> Michelle
>
>
>
> -----Original Message-----
> From: Benoit Jodoin [mailto:[email protected]]
> Sent: Thursday, June 12, 2003 7:11 AM
> To: [email protected]
> Subject: CachedRowSet
>
>
> Anyone here using a CachedRowSet?
>
> all examples i found looked like this :
>
> import sun.jdnc.rowset.*;
> sun.jdbc.rowset.CachedRowSet crs = new sun.jdbc.rowset.CachedRowSet();
>
> this is the error message i get:
>
> class sun.jdbc.rowset.CachedRowSet is an abstract class. It can't be
> instantiated. CachedRowSet crs = new CachedRowSet();
>
> http://www.javaworld.com/javaworld/jw-02-2001/jw-0202-cachedrow.html
>
> Well, i know  you cant instanciate an abstract class.
>
>
> any ideas would be very helpful.
> thanks in advance
> Ben
>
>
> --
> Benoit Jodoin <[email protected]>
> Vertical 7
>
>
___________________________________________________________________________
> To unsubscribe, send email to [email protected] and include in the
body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>
>
___________________________________________________________________________
> To unsubscribe, send email to [email protected] and include in the
body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
--
Benoit Jodoin <[email protected]>
Vertical 7

___________________________________________________________________________
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.