Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8264911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:37:20+00:00 2026-06-08T04:37:20+00:00

I have an old application which I need to optimize in which my method

  • 0

I have an old application which I need to optimize in which my method returns java.sql.ResultSet and also I have single connection so now I need to implement connection pooling now the problem is if I close the connection than resultset will give exception when doing rs.next() so I want such a class similar to ResultSet which we can say is detached from Connection also such that i don’t have to make changes on my JSPs where i have used ResultSet object.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:37:22+00:00Added an answer on June 8, 2026 at 4:37 am

    Got the solution I had used CachedRowSet

    package taher.connection;
    import com.sun.rowset.CachedRowSetImpl;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import javax.sql.rowset.CachedRowSet;
    import org.apache.commons.dbcp.BasicDataSource;
    
    /**
     *
     * @author taher_JAVAHUNTER
     */
    
    public class DataTransaction {
    
    private final static String username = "test";
    private final static String password = "test";
    private final static String url = "jdbc:mysql://127.0.0.1:3309/test";
    public Connection connection = null;
    public Statement statement = null;
    ResultSet rs = null;
    public static int connectionCount = 0;
    
    public DataTransaction(boolean setCon) {
        try {
            setConnection();
        } catch (Exception e) {
            System.out.println("Error in Connection:" + e.toString());
        }
    }
    public static BasicDataSource dataSource;
    
    public void setConnection() throws SQLException {
        try {
            if (dataSource == null) {
                dataSource = new BasicDataSource();
                String driver = "com.mysql.jdbc.Driver";
                try {
                    dataSource.setDriverClassName(driver);
                    dataSource.setUrl(url);
                    dataSource.setUsername(username);
                    dataSource.setPassword(password);
                    dataSource.setMaxActive(100);
                    dataSource.setMaxWait(10000);
                    dataSource.setMaxIdle(10);
                    if (connection == null || connection.isClosed()) {
                        System.out.println(" requeition CONNECTION WITH FIRST SERVER.");
                        connection = dataSource.getConnection();
                        connectionCount++;
                    }
                } catch (SQLException e) {
                    System.out.println("***Connection Requisition*** Could not connect to the database msg :" + e.getMessage());
                }
            } else {
                System.out.println("NumActive : "+dataSource.getNumActive());
                System.out.println("NumIdle : "+dataSource.getNumIdle());
                System.out.println("NumTestsPerEvictionRun : "+dataSource.getNumTestsPerEvictionRun());
                if (connection == null || connection.isClosed()) {
                    connection = dataSource.getConnection();
                    connectionCount++;
                }
            }
        } catch (Exception e) {
            System.out.println("open connection exception" + e);
        }
    }
    
    public CachedRowSet viewQuery(String query) throws SQLException, Exception {
        //query = query;
        CachedRowSetImpl crs = new CachedRowSetImpl();
        CachedRowSet crs2 = null;
    
        try {
            if (connection.isClosed()) {
                setConnection();
            }
        System.out.println("Connection count 1 : " + connectionCount);
    
            statement = connection.createStatement();
            rs = statement.executeQuery(query);
    
            crs.populate(rs);
            crs2 = crs.createCopy();
            closeConnection();
            System.out.println("Connection count 2 : " + connectionCount);
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return crs2;
    }
    
    public void closeConnection() throws SQLException {
        try {
            if (statement != null) {
                statement.close();
            } else {
            }
            if (connection != null) {
                connection.close();                
            }
        } catch (Exception e) {
        }
    
    
    }
    
    public static void main(String[] args) throws SQLException, Exception {
        DataTransaction dt = new DataTransaction(true);
        for (int i = 0; i < 10; i++) {
            ResultSet rs = dt.viewQuery("select * from tbl_test");
            //ResultSet rs = dt.viewQuery("select * from tbl_test",0);
            System.out.println("Connection closed : " + dt.connection.isClosed());
            while (rs.next()) {
                System.out.println("testId : " + rs.getString(1));
                System.out.println("testName : " + rs.getString(2));
            }
        }
    
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an old J2EE application (J2EE 1.3), which packages into an EAR, and
I have successfully upgraded an MFC application which was compiled with an old version
I have an application that is responsible for archiving old applications, which will do
Retrofitting ASP.NET WebForms themes to an old application I have a need to theme
I have a java application which has a GUI in both English and French,
I have an application that used another php cms system, which I am now
I have an application in Zend where I need to implement system which will
I have an iPad application which has many categorized images I need to switch
I have a Rails 3 application using Devise for authentication. Now I need to
I have 'inherited' a test harness application which uses Enterprise Library for its SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.