chaitu chaitu
asked on
fetch the records from cache
say i have emp table
eno ename sales
1 david 1100
2 lara 200
3 james 1000
1 david 1200
2 lara 5400
4 white 890
3 james 7500
1 david 1313
eno can be duplicate
when i give empno is 1
i want to display his sales i.e 1100,1200,1313
first time i will go to database and fetch the records
but next time onwards i dont go to database; i will fetch the records from cache;
i thought doing it using hashmap or hasptable ;both those two don't allow duplicate values(empno has duplicate values);
How to solve this problem.
eno ename sales
1 david 1100
2 lara 200
3 james 1000
1 david 1200
2 lara 5400
4 white 890
3 james 7500
1 david 1313
eno can be duplicate
when i give empno is 1
i want to display his sales i.e 1100,1200,1313
first time i will go to database and fetch the records
but next time onwards i dont go to database; i will fetch the records from cache;
i thought doing it using hashmap or hasptable ;both those two don't allow duplicate values(empno has duplicate values);
How to solve this problem.
you need to store a map of lists, keyed on empno
ASKER
sorry i didnt get u
your map would contains Lists (eg. Vectors) keyed on empno.
And each list would contain all employees with that empno
And each list would contain all employees with that empno
Make a class called Employee something like:
class Employee {
private int id;
private String name;
private ArrayList sales;
blah blah
}
Then when you read the information, create objects of Employee, and you can store each one of these in a HashMap and key it on whatever you want.
class Employee {
private int id;
private String name;
private ArrayList sales;
blah blah
}
Then when you read the information, create objects of Employee, and you can store each one of these in a HashMap and key it on whatever you want.
ASKER
"you can store each one of these in a HashMap and key it on whatever you want."
if i want give key on empno hashmap doesnt allow duplicates
if i want give key on empno hashmap doesnt allow duplicates
chaituu,
design a different key rather than (or Containing) eno, for example u can decide that the ID with Name is the real non repeatable combination in the table or what ever combination but their must be a unique combination in order to store ur table into Hashtable, in this case create class called EmpKey has member fields contains Id and Name and implements, equals() and hashCode() methods
if their is no unique combination then don't use Hashtable use a Collection or List based class like Vector to store ur Table Value Object or Data Transfer Object
design a different key rather than (or Containing) eno, for example u can decide that the ID with Name is the real non repeatable combination in the table or what ever combination but their must be a unique combination in order to store ur table into Hashtable, in this case create class called EmpKey has member fields contains Id and Name and implements, equals() and hashCode() methods
if their is no unique combination then don't use Hashtable use a Collection or List based class like Vector to store ur Table Value Object or Data Transfer Object
You'll have no duplicates. Empno (eno) is the employee identifier. And those identifiers are unique.
Look, you could split your table on two databases (actually, it SHOULD be done this way):
employee table:
eno ename
1 david
2 lara
3 james
4 white
sales table:
eno sales
1 1100
1 1200
1 1313
2 200
2 5400
3 1000
3 7500
4 890
There should be defined the foreign key: sales.eno -> employee.eno.
Then you will be able to read the values from the database and create straight mapping: Hashtable<Integer, ArrayList<Integer>>
1 -> (1100, 1200, 1313)
2 -> (200, 5400)
3 -> (1000, 7500)
4 -> (890)
Look, you could split your table on two databases (actually, it SHOULD be done this way):
employee table:
eno ename
1 david
2 lara
3 james
4 white
sales table:
eno sales
1 1100
1 1200
1 1313
2 200
2 5400
3 1000
3 7500
4 890
There should be defined the foreign key: sales.eno -> employee.eno.
Then you will be able to read the values from the database and create straight mapping: Hashtable<Integer, ArrayList<Integer>>
1 -> (1100, 1200, 1313)
2 -> (200, 5400)
3 -> (1000, 7500)
4 -> (890)
ASKER
hi RMaruszewski
all the fields in one table only there is no second table;
but in the emp data
if the empno is 1 then obviously his name should be same in ename field;
for empno 1 i cant give another name naa;
so i cant take eno and ename as key;
all the fields in one table only there is no second table;
but in the emp data
if the empno is 1 then obviously his name should be same in ename field;
for empno 1 i cant give another name naa;
so i cant take eno and ename as key;
Try using EHCache. You can use this simple library for all your later caching efforts.
- http://ehcache.sourceforge.net/
- http://ehcache.sourceforge.net/
ASKER
i want to do it using Collections package only
ASKER
this is my Employee class
package example;
import java.util.*;
public class Employee
{
private int eno;
private String ename;
private java.util.ArrayList sales;
public void setENO(int eno)
{
this.eno = eno;
}
public int getENO()
{
return this.eno;
}
public void setEName(String ename)
{
this.ename =ename;
}
public String getEName()
{
return this.ename ;
}
public void setSales(ArrayList sales)
{
this.sales = sales;
}
public ArrayList getSales()
{
return this.sales;
}
}
this is Cache class
package example;
import java.sql.*;
import java.io.*;
import example.Employee;
import java.util.*;
public class Cache
{
public Cache()
{
try
{
Class.forName("oracle.jdbc .driver.Or acleDriver ");
Connection con=DriverManager.getConne ction("jdb c:oracle:t hin:@172.1 6.31.12:15 21:egl","s cott","tig er");
Statement stmt= con.createStatement();
ResultSet rs= stmt.executeQuery("SELECT ENO,ENAME,SALES FROM EMPTABLE");
Employee employee = null;
while(rs.next())
{
employee = new Employee();
employee.setENO(rs.getInt( 1));
employee.setEName(rs.getSt ring(2));
//*****How can I add sales to the particular empno in the Array List********************** *****///
}
}catch(ClassNotFoundExcept ion cnf)
{
cnf.printStackTrace();
}
catch(SQLException sql)
{
sql.printStackTrace();
}
}
public static void main(String[] args)
{
Cache cache = new Cache();
}
}
in EMPTABLE eno can allow duplicates?
package example;
import java.util.*;
public class Employee
{
private int eno;
private String ename;
private java.util.ArrayList sales;
public void setENO(int eno)
{
this.eno = eno;
}
public int getENO()
{
return this.eno;
}
public void setEName(String ename)
{
this.ename =ename;
}
public String getEName()
{
return this.ename ;
}
public void setSales(ArrayList sales)
{
this.sales = sales;
}
public ArrayList getSales()
{
return this.sales;
}
}
this is Cache class
package example;
import java.sql.*;
import java.io.*;
import example.Employee;
import java.util.*;
public class Cache
{
public Cache()
{
try
{
Class.forName("oracle.jdbc
Connection con=DriverManager.getConne
Statement stmt= con.createStatement();
ResultSet rs= stmt.executeQuery("SELECT ENO,ENAME,SALES FROM EMPTABLE");
Employee employee = null;
while(rs.next())
{
employee = new Employee();
employee.setENO(rs.getInt(
employee.setEName(rs.getSt
//*****How can I add sales to the particular empno in the Array List**********************
}
}catch(ClassNotFoundExcept
{
cnf.printStackTrace();
}
catch(SQLException sql)
{
sql.printStackTrace();
}
}
public static void main(String[] args)
{
Cache cache = new Cache();
}
}
in EMPTABLE eno can allow duplicates?
ResultSet rs= stmt.executeQuery("SELECT ENO,ENAME,SALES FROM EMPTABLE ORDER BY ENO");
int lastEno = 0;
int currentEno = 0;
ArrayList employees = new ArrayList;
ArrayList employeeSales = null;
Employee employee = null;
while(rs.next())
{
currentEno = rs.getInt(1);
if (currentEno != lastEno)
{
if (employee != null)
{
employee.setESales(employe eSales);
employees.add(employee);
}
employee = new Employee();
employeeSales = new ArrayList();
employee.setENO(currentEno );
employee.setEName(rs.getSt ring(2));
lastEno = currentEno;
}
employeeSales.add(rs.getIn t(3);
}
if (employee != null)
{
employee.setESales(employe eSales);
employees.add(employee);
}
int lastEno = 0;
int currentEno = 0;
ArrayList employees = new ArrayList;
ArrayList employeeSales = null;
Employee employee = null;
while(rs.next())
{
currentEno = rs.getInt(1);
if (currentEno != lastEno)
{
if (employee != null)
{
employee.setESales(employe
employees.add(employee);
}
employee = new Employee();
employeeSales = new ArrayList();
employee.setENO(currentEno
employee.setEName(rs.getSt
lastEno = currentEno;
}
employeeSales.add(rs.getIn
}
if (employee != null)
{
employee.setESales(employe
employees.add(employee);
}
ASKER
on what key basis u add these sales ;
i can't add hashmap.put(eno,employee.g etSales()) ;
i can't add hashmap.put(eno,employee.g
hashmap.put(new Integer(eno),employee.getS ales()), or
hashmap.put(employeeName,e mployee.ge tSales())
hashmap.put(employeeName,e
ASKER
as well i can write like this but i want to display which employee has his sales...
public class Cache
{
public Cache()
{
try
{
Class.forName("oracle.jdbc .driver.Or acleDriver ");
Connection con=DriverManager.getConne ction("jdb c:oracle:t hin:@172.1 6.31.12:15 21:egl","s cott","tig er");
Statement stmt= con.createStatement();
ResultSet rs= stmt.executeQuery("SELECT ENO,ENAME,SALES FROM EMPTABLE");
int lastEno = 0;
int currentEno = 0;
ArrayList employees = new ArrayList();
ArrayList employeeSales = new ArrayList();
Employee employee = null;
while(rs.next())
{
employee = new Employee();
employee.setENO(rs.getInt( 1));
employee.setEName(rs.getSt ring(2));
employeeSales.add(new Integer(rs.getInt(3)));
employee.setSales(employee Sales);
employees .add(employee);
}
}catch(ClassNotFoundExcept ion cnf)
{
cnf.printStackTrace();
}
catch(SQLException sql)
{
sql.printStackTrace();
}
}
public static void main(String[] args)
{
Cache cache = new Cache();
}
}
public class Cache
{
public Cache()
{
try
{
Class.forName("oracle.jdbc
Connection con=DriverManager.getConne
Statement stmt= con.createStatement();
ResultSet rs= stmt.executeQuery("SELECT ENO,ENAME,SALES FROM EMPTABLE");
int lastEno = 0;
int currentEno = 0;
ArrayList employees = new ArrayList();
ArrayList employeeSales = new ArrayList();
Employee employee = null;
while(rs.next())
{
employee = new Employee();
employee.setENO(rs.getInt(
employee.setEName(rs.getSt
employeeSales.add(new Integer(rs.getInt(3)));
employee.setSales(employee
employees .add(employee);
}
}catch(ClassNotFoundExcept
{
cnf.printStackTrace();
}
catch(SQLException sql)
{
sql.printStackTrace();
}
}
public static void main(String[] args)
{
Cache cache = new Cache();
}
}
Your solution won't work.
You don't reset the "employeeSales" list.
And the second employee will get the sales of the first employee.
And the third employee will get the sales of the first and second employee.
And so on...
You also create multiple instances of the "Employee" object for one employee. It's wrong solution.
You don't reset the "employeeSales" list.
And the second employee will get the sales of the first employee.
And the third employee will get the sales of the first and second employee.
And so on...
You also create multiple instances of the "Employee" object for one employee. It's wrong solution.
Use my solution, then iterate through all the "employees", get "employee", display his identifier, name, and sales list.
Simple as that.
Simple as that.
ASKER
but as u said using this
hashmap.put(new Integer(eno),employee.getS ales()), or
hashmap.put(employeeName,e mployee.ge tSales())
hashmap doen't allow duplicates naaa.......
hashmap.put(new Integer(eno),employee.getS
hashmap.put(employeeName,e
hashmap doen't allow duplicates naaa.......
Look again at my solution. You have no duplicates there.
ASKER
WHERE SHOULD I PUT THIS LINE
hashmap.put(new Integer(currentEno),employ ee.getSale s());
hashmap.put(new Integer(currentEno),employ
ASKER
HashMap hashmap = new HashMap();
ArrayList employees = new ArrayList();
ArrayList employeeSales = null;
Employee employee = null;
while(rs.next())
{
currentEno = rs.getInt(1);
if (currentEno != lastEno)
{
if (employee != null)
{
employee.setSales(employee Sales);
employees.add(employee);
}
System.out.println("curren tEno="+cur rentEno);
employee = new Employee();
employeeSales = new ArrayList();
employee.setENO(currentEno );
employee.setEName(rs.getSt ring(2));
System.out.println("ename= "+rs.getSt ring(2));
lastEno = currentEno;
}
employeeSales.add(new Integer(rs.getInt(3)));
if (employee != null)
{
employee.setSales(employee Sales);
employees.add(employee);
}
hashmap.put(currentEno,emp loyee.getS ales());
System.out.println("getSal es="+emplo yee.getSal es());
}
here currentEno wil come as duplicates ;
ArrayList employees = new ArrayList();
ArrayList employeeSales = null;
Employee employee = null;
while(rs.next())
{
currentEno = rs.getInt(1);
if (currentEno != lastEno)
{
if (employee != null)
{
employee.setSales(employee
employees.add(employee);
}
System.out.println("curren
employee = new Employee();
employeeSales = new ArrayList();
employee.setENO(currentEno
employee.setEName(rs.getSt
System.out.println("ename=
lastEno = currentEno;
}
employeeSales.add(new Integer(rs.getInt(3)));
if (employee != null)
{
employee.setSales(employee
employees.add(employee);
}
hashmap.put(currentEno,emp
System.out.println("getSal
}
here currentEno wil come as duplicates ;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
All of the solutions rely on the fact that the employee number will be returned from the SQL statement sorted by that number. There is not guarantee that the database will return data in any order unless you include an "ORDER BY" clause in the query.
This is not necessary, just code the retrieval loop like this:
HashMap employeesMap = new HashMap();
ArrayList employeeSales = null;
Employee employee = null;
while(rs.next())
{
currentEno = new Integer(rs.getInt(1));
employee=employeesMap.get( currentEno );
if (currentEno==null) {
employee.setENO(currentEno );
employee.setEName(rs.getSt ring(2));
}
employee.addSales(rs.getIn t(3);
employeesMap.put(currentEn o, employee);
}
...
When you create a new employee, also make sure you initialize the sales array.
The addSales(int sale) method just calls add() to the salesArray member variable.
If the employee has already been retrieved once, the employee# and Name will be the same, just add the new sales record to the array.
This is not necessary, just code the retrieval loop like this:
HashMap employeesMap = new HashMap();
ArrayList employeeSales = null;
Employee employee = null;
while(rs.next())
{
currentEno = new Integer(rs.getInt(1));
employee=employeesMap.get(
if (currentEno==null) {
employee.setENO(currentEno
employee.setEName(rs.getSt
}
employee.addSales(rs.getIn
employeesMap.put(currentEn
}
...
When you create a new employee, also make sure you initialize the sales array.
The addSales(int sale) method just calls add() to the salesArray member variable.
If the employee has already been retrieved once, the employee# and Name will be the same, just add the new sales record to the array.