There are thee tables inside my database. One is employee, the second is employee_Project, and the third is employee_Reporting. Each table has a common employee_Number as its primary key, and there is a one to many relationship among them such that an employee has many projects and reporting dates.
I have run select * from employee, select * from employee_project, select * from employee_reporting in three data holder classes which have methods fillResultSet(Result set) and List<T> getData(). This is based on a SqlDbEngine class with a runQuery(PreparedStatement,DataHolder) method, and the implementation has been completed.
Now I have to design a getAllEmployee() method along with project and reporting detail with optimal code in java using JDBC. I have used an iterator but this solution is not acceptable; now I have to use a foreach loop.
This is what I have done:
public List<Employee> getAllEmployees() {
EmployeeDataHolderImpl empdataholder = new EmployeeDataHolderImpl();
List<Employee> list_Employee_Add = null;
try {
Connection connection = mySqlDbConnection.getConnection();
PreparedStatement preparedStatement = connection
.prepareStatement(GET_ALL_EMPLOYEE_DETAILS);
mySqlDBEngineImpl.runQuery(preparedStatement, empdataholder);
} catch (SQLException e) {
e.printStackTrace();
}
for (Employee employee : empdataholder.getData()) {
new EmployeeDAOImpl().getProject(employee);
new EmployeeDAOImpl.getReport(employee);
}
list_Employee_Add = empdataholder.getData();
return list_Employee_Add;
}
and make another method
public void getProject(Employee emp) {
EmployeeProjectDataHolderImpl employeeProjectHolder = new EmployeeProjectDataHolderImpl();
try {
Connection connection = mySqlDbConnection.getConnection();
PreparedStatement preparedStatement = connection
.prepareStatement(GET_ALL_PROJECT_DETAILS);
mySqlDBEngineImpl
.runQuery(preparedStatement, employeeProjectHolder);
} catch (SQLException e) {
e.printStackTrace();
}
for (EmployeeProject employee_Project : employeeProjectHolder.getData()) {
if (employee_Project.getEmployeeNumber() == emp.getEmpNumber()) {
emp.getProjects().add(employee_Project);
}
}
}
public void getReport(Employee emp) {
EmployeeReportDataHolderImpl employeeReportHolder = new EmployeeReportDataHolderImpl();
try {
Connection connection = mySqlDbConnection.getConnection();
PreparedStatement preparedStatement = connection
.prepareStatement(GET_ALL_REPORT_DETAILS);
mySqlDBEngineImpl
.runQuery(preparedStatement, employeeReportHolder);
} catch (SQLException e) {
e.printStackTrace();
}
for (EmployeeReport employee_Report : employeeReportHolder.getData()) {
if (employee_Report.getEmployeeNumber() == emp.getEmpNumber()) {
emp.getProjects().add(employee_Project);
}
}
}
}
and same for Employee Reporting but doing, this performance is going to decrease.no body worry about closing connection i will do it
Please tell me how I could improve my solution..
There are some issue with your code.
1.you are initializing EmployeeDAOImpl everytime, rather you can just keep one instance and call the operations over it.
new EmployeeDAOImpl().getProject(employee); new
EmployeeDAOImpl.getReport(employee);
2.I don't see where you close your connection after performing an SQL operation.
You should be having
try {
--code statements
}
catch(SQLException e){
e.printStackTrace();
}
finally{
-- close your connection and preparedStatement
}
Closing database connections is very vital.
If you use your actual code, you will have 3 impacts in your code:
You're opening a connection to get the employee's data.
For every employee, you open (and close) a new connection to get his projects.
For every employee, you open (and close) a new connection to get his reports.
Note that opening a new connection is a performance hit on your application. It doesn't matter if you use an enhanced for-loop or an Iterator, there would be many hits that can slow down your application.
Two ways to solve this problem:
Open a single connection where you run all your select statements. This will be better than opening/closing lot of connections.
Create a single SQL statement to retrieve the employees and the data you need for every employee. It will have better performance for different reasons:
A single connection to the database.
A single query instead of lot of queries to the database (a single I/O operation).
If your rdbms allows it, the query will be optimized for future requests (a single query instead of multiple queries).
I would prefer to go with the second option. For this, I tend to use a method that executes any SQL select statement and return a ResultSet. I'll post a basic example (note, the provided code can be improved depending on your needs), this method could be in your SqlDbEngine class:
public ResultSet executeSQL(Connection con, String sql, List<Object> arguments) {
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = con.prepareStatement(sql);
if (arguments != null) {
int i = 1;
for(Object o : arguments) {
pstmt.setObject(i++, o);
}
}
//method to execute insert, update, delete statements...
rs = pstmt.execute();
} catch(SQLException e) {
//handle the error...
}
return rs;
}
And this other method to handle all the query operation
public List<Employee> getAllEmployee() {
Connection con = null;
ResultSet rs = null;
List<Employee> lstEmployee = new ArrayList<Employee>();
try {
con = mySqlDbConnection.getConnection();
//write the sql to retrieve all the data
//I'm assuming these can be your columns, it's up to you
//this can be written using JOINs...
String sql = "SELECT E.EMPLOYEE_ID, E.EMPLOYEE_NAME, P.PROJECT_NAME, R.REPORT_NAME FROM EMPLOYEE E, PROJECT P, REPORT R WHERE E.EMPLOYEE_ID = P.EMPLOYEE_ID AND E.EMPLOYEE_ID = R.EMPLOYEE_ID";
//I guess you don't need parameters for this...
rs = SqlDbEngine.executeSQL(con, sql, null);
if (rs != null) {
Employee e;
int employeeId = -1, lastEmployeeId = -1;
while (rs.next()) {
//you need to make sure to create a new employee only when
//reading a new employee id
employeeId = rs.getInt("EMPLOYEE_ID");
if (lastEmployeeId != employeeId) {
e = new Employee();
lastEmployeeId = employeeId;
lstEmployee.add(e);
}
Project p = new Project();
Report r = new Report();
//fill values of p...
//fill values of r...
//you can fill the values taking advantage of the column name in the resultset
//at last, link the project and report to the employee
e.getProjects().add(p);
e.getReports().add(r);
}
}
} catch (Exception e) {
//handle the error...
} finally {
try {
if (rs != null) {
Statement stmt = rs.getStatement();
rs.close();
stmt.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
//handle the error...
}
}
return lstEmployee;
}
Note that the second way can be harder to code but it will give you the best performance. It's up to you to improve the provided methods, some advices:
Create a class that receives a ResultSet and builds a Project instance using the columns name of the ResultSet (similar for Report and Employee).
Create a method that handles the ResultSet and its Statement close.
As a best practice, never use select * from mytable, it's preferable to write the needed columns.
If I understand correctly, your code first loads all EmployeeReport rows and then filters them according to getEmployeeNumber(). You can let your database do this by modifying your SQL query.
Since you didn't show your SQL queries (I assume they're in GET_ALL_REPORT_DETAILS), I'll just make a guess... Try executing SQL like:
select *
from employee_reporting
where employeeNumber = ?
If you put this in a PreparedStatement, and then set the parameter value, your database will only return the data you need. For example:
PreparedStatement pstmt = con.prepareStatement(GET_ALL_REPORT_DETAILS);
pstmt.setInt(1, employee.getEmployeeNumber());
That should return only the EmployeeReport records having the desired employeeNumber. In case performance is still an issue, you could consider adding an index to your EmployeeReport table, but that's a different story...
Related
I had a very sophisticated class that performed DB queries, the problem is it wasnt using try-with-resource statements so i had to .close() manually. To be safer, I tried to re-design it with try-with-resource. My question is if these resources will close properly given how I'm referencing them outside the objects containing those resources. For example, this class DBQuery i use to create queries and resources related to those queries
public class DBQuery {
private String _query;
private PreparedStatement _stmt;
private ResultSet _rs;
// constructor
public DBQuery (String query) {
_query = query;
}
public PreparedStatement execPreparedStatement() throws SQLException {
_stmt = DB.getCon().prepareStatement(_query, Statement.RETURN_GENERATED_KEYS);
return _stmt;
}
public ResultSet getRecordSet() throws SQLException {
_rs = _stmt.executeQuery();
return _rs;
}
public void setInt(int paramNum, int setVal) throws SQLException {
_stmt.setInt(paramNum, setVal);
}
public void setString(int paramNum, String setVal) throws SQLException {
_stmt.setString(paramNum, setVal);
}
}
Then this would be example usage of the class. loadActiveCompany given a companyId retrieves the company from the database and creates some objects. My question is two-fold:
will the resources close properly when loadActiveCompany completes.
is there any problem with how I'm using the try-catch blocks.
Thank you
// loads the active company into the view
public void loadActiveCompany(int companyId) {
boolean loadFailed = false;
// we are passed the company id
_activeCompany.setCompanyId(companyId);
DBQuery qComps = new DBQuery("SELECT comp_name FROM comps WHERE id=?");
try ( PreparedStatement stmtComps = qComps.execPreparedStatement() ) {
DB.getCon().rollback();
qComps.setInt(1, companyId);
try ( ResultSet rsComps = qComps.getRecordSet() ) {
rsComps.next();
String companyName = rsComps.getString("comp_name");
_activeCompany.setCompanyName(companyName);
}
} catch (SQLException e) {
System.out.println("Couldn't find company!");
loadFailed = true;
} finally {
if (loadFailed)
return;
}
DBQuery qGroups = new DBQuery("SELECT id, group_name FROM comps_groups WHERE comp_id=? ORDER BY sort_order ASC");
try ( PreparedStatement stmtGroups = qGroups.execPreparedStatement() ) {
qGroups.setInt(1, companyId);
try ( ResultSet rsGroups = qGroups.getRecordSet() ) {
while (rsGroups.next()) {
int groupId = rsGroups.getInt("id");
Group thisGroup = new Group();
thisGroup.setGroupId(groupId);
thisGroup.setGroupName(rsGroups.getString("group_name"));
DBQuery qAnchors = new DBQuery("SELECT id, anchor_name, anchor_type FROM comps_groups_anchors WHERE group_id=? ORDER BY sort_order ASC");
try ( PreparedStatement stmtAnchors = qAnchors.execPreparedStatement() ) {
qAnchors.setInt(1, groupId);
try ( ResultSet rsAnchors = qAnchors.getRecordSet() ) {
while (rsAnchors.next()) {
int anchorId = rsAnchors.getInt("id");
Anchor thisAnchor = new Anchor();
thisAnchor.setAnchorId(anchorId);
thisAnchor.setAnchorName(qAnchors.getRS().getString("anchor_name"));
thisAnchor.setAnchorType(qAnchors.getRS().getInt("anchor_type"));
thisGroup.addGroupAnchor(thisAnchor);
}
}
}
_activeCompany.getCompanyGroups().add(thisGroup);
}
}
DB.getCon().commit();
} catch (SQLException e) {
System.out.println("Could not load company!");
loadFailed = true;
} finally {
if (loadFailed)
return;
}
// print out the active company
_activeCompany.printStatus();
}
Short answers:
Yes, the PreparedStatements are all closed.
Not directly problems, but easier ways.
Here my BUTs:
The name execPreparedStatement is totally misleading, as it is not (in DB terms) executing anything, just creating the PreparedStatement. A better name would be createPreparedStatement or - lol - preparePreparedStatement
Why do you call DB.getCon().rollback();? I do not think this will lead to someplace good...
the way you use DBQuery at the moment, it will only bring pain. Basicall this is just a container that saves additional infos (_stmt + _rs) which makes it SEVERELY state and sequence dependent, prone to hit you with lots of NPEs
so the actions you call on DBQuery you could simply also call on the PreparedStatement, reducing complexity and taking away a few pitfalls
so either completely remove DBQuery
or remodel the DBQuery
to be Closeable/AutoCloseable,
add some checks to the other functions,
create the PreparedStatement right away (CTOR, query string as CTOR parameter),
keep it private, do not expose it
use it inside getRecordSet,
do not store any other references unless you REALLY need them
and in the close method close the PS,
Your loadFailed = true; and if (loadFailed) return; seems overly convoluted and error-prone. Why not directly call return; right where you currently have loadFailed = true; lines?
I would - personal preference - put those 2 whole try-catch blocks into their own methods, signaling failure with a boolean or something => more methods with each less code and better variable scope (for example no re-use of loadFailed, but better re-usability of the two methods)
You actually do NOT need the inner try-resource on the ResultSets, but it's good if you (can) keep em. Just be careful there, as closing a ResultSet might have an impact on its creator PreparedStatement. So if you test it (in a situation where you re-use the PreparedStatement, that what it's actually made for) and get a 'closed' Exception when reusing the PreparedStatement, then you remove the try-resource blocks around the ResultSets.
I am writing a Program which tracks the movement of an RFID Tag as it moves between RFID censors. This is mimicking the movement of a wallet between rooms. Each censor will be a door to a different room.
I have a SQLite database which holds the Name of the Card, the Location, and the name of the Tag to differentiate between different cards.
I need an ArrayList which tells me what Tags are currently on the database.
I have a DAO method that displays all the values in a database and stores it in an ArrayList as shown:
SELECT * FROM Wallets;
[name: Henry, location: 0, tag: 5c00ce6df0, name: jim, location: 0, tag: wallet1]
I am trying to write another DAO method that will just display the tags like this:
SELECT Tag FROM Wallets;
[5c00ce6df0, wallet1]
Once i have these values saved in an ArrayList i intend to pass them into another DAO method which takes the tags and returns all the relevant information, like this:
SELECT * FROM Wallets WHERE Tag = 'wallet1';
name: jim, location: 0, tag: wallet1
Here is the code for my getAllWallets() DAO:
public ArrayList<Wallet> getAllWallets() throws SQLException{
Connection dbConnection = null;
Statement statement = null;
ResultSet resultset = null;
String query = "SELECT * FROM Wallets;";
ArrayList<Wallet> list = new ArrayList<>();
try {
dbConnection = getDBConnection();
statement = dbConnection.createStatement();
System.out.println(query);
// execute SQL query
resultset = statement.executeQuery(query);
while (resultset.next()) {
Wallet w = new Wallet(query, 0, query);
w.setName(resultset.getString("Name"));
w.setLocation(resultset.getInt("Location"));
w.setTag(resultset.getString("Tag"));
list.add(w);
}
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
finally
{
resultset.close();
statement.close();
dbConnection.close();
}
return list;
}
Here is what i have tried so far for my getWalletTag() DAO:
public ArrayList<String> getWalletTag() throws SQLException {
Connection dbConnection = null;
ResultSet resultset = null;
Statement statement = null;
ArrayList<String> list = new ArrayList<String>();
String query = "SELECT Tag FROM Wallets;";
try {
dbConnection = getDBConnection();
statement = dbConnection.createStatement();
// execute SQL query
System.out.println(query);
resultset = statement.executeQuery(query);
String tag = tag.toString(); // i know this is totally wrong but i got stuck
list.add(tag);
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
finally
{
if (resultset != null) {
resultset.close();
}
if (statement != null) {
statement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
return list;
}
What i am struggling with is how to get all the values of the Tag column into an array list. Is there an easy way to do this? Any help will be greatly appreciated. Thanks.
In an "ideal universe", you would probably want to use an ORM (like Hibernate), and a framework (like Spring Boot).
Either or both might be "overkill" for your application.
If your goal is to get a "list of tags", then the code you've got looks OK:
Connect to the database.
Make a query.
Copy the resultset into a list of Java objects, one at a time.
Close the DB connection when you're done.
If your goal is to "optimally" find a specific tag (without making another DB query), then perhaps you should use a Java Map or Set instead of an ArrayList.
Should you wish to consider Spring Boot and Sqlite, here are a couple of tutorials:
https://www.baeldung.com/spring-boot-sqlite
http://code-flow-hjbello.blogspot.com/2017/07/using-sqlite-with-jdbc.html
This part of your code seems good:
public ArrayList<Wallet> getAllWallets() throws SQLException{
resultset = statement.executeQuery(query);
while (resultset.next()) {
Wallet w = new Wallet(query, 0, query);
w.setName(resultset.getString("Name"));
w.setLocation(resultset.getInt("Location"));
w.setTag(resultset.getString("Tag"));
list.add(w);
}
...
So one of two choices:
Either forget about getWalletTag(), and just use your wallets to identify tags, or
Use the same query, just save the "tag" column into your array list (instead of anything else).
Option 2:
public ArrayList<String> getWalletTag() throws SQLException {
String query = "SELECT Tag FROM Wallets;";
...
resultset = statement.executeQuery(query);
List<String> tags = new ArrayList<String>();
while (resultset.next()) {
tags.add(resultset.getString("Tag"));
...
... or ...
...
Set<String> tags = new HashSet<String>();
while (resultset.next()) {
tags.add(resultset.getString("Tag"));
I'm building a webcrawler and I'm looking for the best way to handle my requests and connection between my threads and the database (MySql).
I've 2 types of threads :
Fetchers : They crawl websites. They produce url and add they into 2 tables : table_url and table_file. They select from table_url
to continue the crawl. And update table_url to set visited=1 when they
have read a url. Or visited=-1 when they are reading it. They can
delete row.
Downloaders : They download files. They select from table_file. They update table_file to change the Downloaded column. They never
insert anything.
Right now I'm working with this :
I've a pool of connection based on c3p0.
Every target (website) have thoses variables :
private Connection connection_downloader;
private Connection connection_fetcher;
I create both connection only once when I instanciate a website. Then every thread will use thoses connections based on their target.
Every thread have thoses variables :
private Statement statement;
private ResultSet resultSet;
Before every Query I open a SqlStatement :
public static Statement openSqlStatement(Connection connection){
try {
return connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
And after every Query I close sql statement and resultSet with :
public static void closeSqlStatement(ResultSet resultSet, Statement statement){
if (resultSet != null) try { resultSet.close(); } catch (SQLException e) {e.printStackTrace();}
if (statement != null) try { statement.close(); } catch (SQLException e) {e.printStackTrace();}
}
Right now my Select queries only work with one select (I never have to select more than one for now but this will change soon) and is defined like this :
public static String sqlSelect(String Query, Connection connection, Statement statement, ResultSet resultSet){
String result = null;
try {
resultSet = statement.executeQuery(Query);
resultSet.next();
result = resultSet.toString();
} catch (SQLException e) {
e.printStackTrace();
}
closeSqlStatement(resultSet, statement);
return result;
}
And Insert, Delete and Update queries use this function :
public static int sqlExec(String Query, Connection connection, Statement statement){
int ResultSet = -1;
try {
ResultSet = statement.executeUpdate(Query);
} catch (SQLException e) {
e.printStackTrace();
}
closeSqlStatement(resultSet, statement);
return ResultSet;
}
My question is simple : can this be improved to be faster ? And I'm concerned about mutual exclusion to prevent a thread to update a link while another is doing it.
I believe your design is flawed. Having one connection assigned full-time for one website will severly limit your overall workload.
As you already have setup a connection pool, it's perfectly okay to fetch before you use (and return afterwards).
Just the same, try-with-catch for closing all your ResultSets and Statements after will make code more readable - and using PreparedStatement instead of Statement would not hurt as well.
One Example (using a static dataSource() call to access your pool):
public static String sqlSelect(String id) throws SQLException {
try(Connection con = dataSource().getConnection();
PreparedStatement ps = con.prepareStatement("SELECT row FROM table WHERE key = ?")) {
ps.setString(1, id);
try(ResultSet resultSet = ps.executeQuery()) {
if(rs.next()) {
return rs.getString(1);
} else {
throw new SQLException("Nothing found");
}
}
} catch (SQLException e) {
e.printStackTrace();
throw e;
}
}
Following the same pattern I suggest you create methods for all the different Insert/Update/Selects your application uses as well - all using the connection only for the short time inside the DB logic.
I can not see a real advantage to have all the Database stuff in your webcrawler threads.
Why don't you use a static class with the sqlSelect and sqlExec method, but without the Connection and ResultSet parameters. Both connection objects are static as well. Make sure the connection objects are valid befor using them.
try {
Connection lig = DriverManager.getConnection(
"jdbc:mysql://localhost/gym", "root", "0000");
PreparedStatement inst = lig
.prepareStatement("SELECT * FROM produtos_has_historico WHERE Produtos_idProdutos AND Historico_idHistorico");
ResultSet a = inst.executeQuery();
while (a.next()){
DefaultListModel model = new DefaultListModel();
model.addElement(a);
}
} catch( Exception e ){}
In this Select, I get the id historic and the id product, but I wanted to get also the name and price of the products that are in my other table "products" to add to my Jlist, can I use two selects ? Thank you.
It's not a problem to have two selects in a single try-catch block.
If you want to handle the generated exception the same way regardless of where it occurred, then I don't see a problem with enclosing multiple statements in the same try block.
Yes, it's fine to put multiple calls that may fail into a single try/catch block — in fact, that's a big part of why we have try/catch blocks (and exception handling in general), to move the code that handles exceptional things (failures) out of the way of the main code, so it's easier to understand the main code.
Compare these bits of pseudo-code
result = doThis();
if (result != success) {
handleTheFailure(result)
} else {
result = doThat();
if (result != success) {
handleThefailure(result);
} else {
result = doTheOther();
if (result != success) {
handleTheFailure(result);
}
}
}
vs.
try {
doThis();
doThat();
doTheOther();
}
catch (failure) {
handleFailure(failure);
}
More in the Java tutorial on exceptions.
No problem. I also recommend you use try-with-resources instead and be more specific about the exceptions you want to catch:
try (Connection conn = DriverManager.getConnection("...")) {
PreparedStatement ps1 = conn.prepareStatement("...");
try (ResultSet rs1 = ps1.executeQuery()) {
/* Parse first result */
}
PreparedStatement ps2 = conn.prepareStatement("...");
try (ResultSet rs2 = ps2.executeQuery()) {
/* Parse second result */
}
} catch (SQLException ex) {
for (Throwable t : ex) {
t.printStackTrace();
}
}
Instead of going for two selects - you can write a SQL query having a join in it .
You will be able to perform the join if you have common columns in your tables.
Then you can write a query using join and then the same code you can use to perform the operations and you can fetch all the values that you want.
For SQL joins basic - you can visit this site -
http://www.w3schools.com/sql/sql_join.asp
I've started creating a toDoList and I like to create a "DataMapper" to fire queries to my Database.
I created this Datamapper to handle things for me but I don't know if my way of thinking is correct in this case. In my Datamapper I have created only 1 method that has to execute the queries and several methods that know what query to fire (to minimalize the open and close methods).
For example I have this:
public Object insertItem(String value) {
this.value = value;
String insertQuery = "INSERT INTO toDoList(item,datum) " + "VALUES ('" + value + "', CURDATE())";
return this.executeQuery(insertQuery);
}
public Object removeItem(int id) {
this.itemId = id;
String deleteQuery = "DELETE FROM test WHERE id ='" + itemId + "'";
return this.executeQuery(deleteQuery);
}
private ResultSet executeQuery(String query) {
this.query = query;
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
con = db.connectToAndQueryDatabase(database, user, password);
st = con.createStatement();
st.executeUpdate(query);
}
catch (SQLException e1) {
e1.printStackTrace();
}
finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e2) { /* ignored */}
}
if (st != null) {
try {
st.close();
} catch (SQLException e2) { /* ignored */}
}
if (con != null) {
try {
con.close();
} catch (SQLException e2) { /* ignored */}
}
System.out.println("connection closed");
}
return rs;
}
So now I don't know if it's correct to return a ResultSet like this. I tought of doing something like
public ArrayList<ToDoListModel> getModel() {
return null;
}
To insert every record returned in a ArrayList. But I feel like I'm stuck a little bit. Can someone lead me to a right way with an example or something?
It depends on the way the application works. If you have a lot of databases hits in a short time it would be better to bundle them and use the same database connection for all querys to reduce the overhead of the connection establishment and cleaning.
If you only have single querys in lager intervals you could do it this way.
You should also consider if you want to seperate the database layer and the user interface (if existing).
In this case you should not pass the ResultSet up to the user interface but wrap the data in an independent container and pass this through your application.
If I understand your problem correctly!, you need to pass a list of ToDoListModel objects
to insert into the DB using the insertItem method.
How you pass your object to insert items does not actually matter, but what you need to consider is how concurrent this DataMapper works, if it can be accessed by multiple threads at a time, you will end up creating multiple db connections which is little expensive.Your code actually works without any issue in sequential access.
So you can add a synchronized block to connection creation and make DataMapper class singleton.
Ok in that case what you can do is, create a ArrayList of hashmap first. which contains Key, Value as Column name and Column value. After that you can create your model.
public List convertResultSetToArrayList(ResultSet rs) throws SQLException{
ResultSetMetaData mdata = rs.getMetaData();
int columns = mdata.getColumnCount();
ArrayList list = new ArrayList();
while (rs.next()){
HashMap row = new HashMap(columns);
for(int i=1; i<=columns; ++i){
row.put(md.getColumnName(i),rs.getObject(i));
}
list.add(row);
}
return list;
}