ankit rai
ankit rai

Reputation: 334

data structure for ResultSet

Working with database in java requires a set of lines of code to be written. If the database connection is established again-n-again the same set of lines need to be repeated again-n-again which creates an overhead to programmer.

Hence, I am making a utility class which works as a database agent. The method of this class will be responsible for all the database related stuff from establishing a connection to executing a query and returning the result.

My class goes like this -

package connect;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Connector
{
private ResultSet rs;
public String password;

public void connect(String dbUrl, String dbClass, String dbUserName, String dbPassword, String query)
{
    try
    {
        Class.forName(this.dbClass).newInstance();
        Connection conn = DriverManager.getConnection (dbUrl, dbUserName, dbPassword);
        Statement stmt = conn.createStatement();
        rs = stmt.executeQuery(query);

        while(rs.next())
        {
            this.password = (String) rs.getObject(1);
        }

        conn.close();
    } //end try
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
}

When a programmer needs a query to be executed, below lines can be written for that purpose -

Connector con = new Connector();
con.connect("your database URL",
            "your database driver",
            "your database username",
            "your database password",
            "your query");

My question here is that right now I am retreiving the data from ResultSet in the connect method itself. The retreived data can vary from query-to-query hence I want a data structure in which I can store the data from rs in connect() method and return it.

It will look like -

<data structure> result = con.connect("your database URL",
                                      "your database driver",
                                      "your database username",
                                      "your database password",
                                      "your query");

Can someone please suggest me a data structure for this purpose?

Upvotes: 1

Views: 2673

Answers (3)

Aditya Naidu
Aditya Naidu

Reputation: 1380

If you can't use any libraries already available to solve your problem, you could use

 Vector<Map<String, Object>>

Where String contains column name and Object is the value of the column. Each element in the Vector corresponds to a row of the ResultSet. This may not be the most efficient data structure but should get the job done.

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160261

Not sure why you're re-inventing this particular wheel, and it seems like if you're repeatedly connecting for every query you're basically writing yourself out of any performance, but why not just use RowSetDynaClass if you're not going to map to objects?

Just be aware that there are a ton of pre-existing solutions that have been tested and proven. And use connection pools; creating connections is expensive.

Upvotes: 3

Peter Svensson
Peter Svensson

Reputation: 6173

There is a lot of Open Source, implementation like the one you want to do above.

Spring JDBCTemplate for example.

Upvotes: 1

Related Questions