i write a java code using time schdular in my program i get the data from databse and it will be updated into anothe databse the task is
1. i get the data from database for every five minutes
this program is excuted every time for every 1 minute
my java code is:
code=java
public class updateremindertable {
private Timer timer=new Timer();
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
updateremindertable manager=new updateremindertable();
manager.startTask();
}
public void data() throws Exception{
List<Rfid1> r1;
List<RfidDB> l;
List<StudentInfo1> l3;
RfidDB db;
StudentInfo si;
StudentInfo1 sb;
java.util.Date d;
List<RfidDB> l1 = new ArrayList<RfidDB>();
List<StudentInfo> l2 = new ArrayList<StudentInfo>();
l3 = new ArrayList<StudentInfo1>();
sb = new StudentInfo1();
si = new StudentInfo();
java.util.Date d2 = new Date();
Date d1 = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dateWithoutTime = sdf.parse(sdf.format(d1));
Calendar c = Calendar.getInstance();
System.out.println(c.getTime());
c.add(c.MINUTE, -5);
System.out.println(c.getTime());
d2 = c.getTime();
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
Criteria cf = session.createCriteria(RfidDB.class).add(
Restrictions.between("clock_in", d2, new Date()));
l = cf.list();
if (l.size() > 0) {
Set s0 = new HashSet();
List p0 = new ArrayList();
for (RfidDB rf : l) {
p0.add(rf.getId());
s0.addAll(p0);
p0.clear();
p0.addAll(s0);
}
Criteria cf1 = session.createCriteria(RfidDB.class);
Criterion one = Restrictions.ge("clock_in", dateWithoutTime);
Criterion two = Restrictions.in("id", p0);
Criterion complete = Restrictions.conjunction().add(one).add(two);
cf1.add(complete);
l1 = cf1.list();
Set s = new HashSet();
List pl1 = new ArrayList();
for (RfidDB sl1 : l1) {
pl1.add(sl1.getId());
s.addAll(pl1);
pl1.clear();
pl1.addAll(s);
}
l2 = session.createCriteria(StudentInfo.class)
.add(Restrictions.in("uniqueid", pl1)).list();
List pl2 = new ArrayList();
Set set = new HashSet();
for (StudentInfo si1 : l2) {
pl2.add(si1.getsNo());
set.addAll(pl2);
pl2.clear();
pl2.addAll(set);
}
l3 = session.createCriteria(StudentInfo1.class)
.add(Restrictions.in("sNo", pl2)).list();
// l3 = cf2.list();
session.close();
sf.close();
SessionFactory sf1 = HibernateUtil.getSessionFactory();
Session ses = sf1.openSession();
for (StudentInfo s2 : l2) {
java.util.Date t = null, t1 = null;
int count = 0;
Rfid1 rf;
rf = new Rfid1();
for (StudentInfo1 s3 : l3) {
if (s2.getsNo() == s3.getsNo()) {
count++;
}
}
int index = 0;
for (RfidDB s1 : l1) {
if (s2.getUniqueid().equals(s1.getId())) {
if (index == 0) {
t = s1.getClock_in();
System.out.println(t);
}
if (index >= 1) {
t1 = s1.getClock_in();
}
index++;
}
}
Transaction tx1 = ses.beginTransaction();
rf.setStatus_color("green");
rf.setUniqueid(s2.getUniqueid());
rf.setDate(t);
DateFormat df = new SimpleDateFormat("YYYY-MM-dd");
String chdate = df.format(t);
rf.setDate1(chdate);
rf.setSid(s2.getsNo());
if (t1 == null) {
rf.setClock_in(t);
Date currdate = new Date();
System.out.println(currdate);
long remtime1 = t.getTime() + (count * 2 * 60000);
if (currdate.getTime() >= remtime1) {
rf.setStatus_color("orange");
}
Date currdate1 = new Date();
long remtime2 = t.getTime() + (count * 3 * 60000);
if (currdate1.getTime() >= remtime2) {
rf.setStatus_color("red");
}
} else {
Date currdate = new Date();
long remtime1 = t.getTime() + (count * 2 * 60000);
rf.setClock_in(t);
rf.setClock_out(t1);
if (t1.getTime() >= remtime1) {
rf.setStatus_color("orange");
}
Date currdate1 = new Date();
long remtime2 = t.getTime() + (count * 3 * 60000);
if (t1.getTime() >= remtime2) {
rf.setStatus_color("red");
}
}
// long diff = (long) ((( t1.getTime()-t.getTime())*1.66667
System.out.println(rf.getClock_in());
System.out.println(rf.getClock_out());
ses.saveOrUpdate(rf);
tx1.commit();
}
ses.close();
sf1.close();
}
}
public void startTask() {
timer.schedule(new PeriodicTask(),0);
}
private class PeriodicTask extends TimerTask{
public void run() {
while(true){
// TODO Auto-generated method stub
try{
data();
Thread.sleep(60000);
timer.schedule(new PeriodicTask(),60000);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
/code
i get the exception
Exception in thread "Timer-0" java.lang.OutOfMemoryError: Java heap space
at java.lang.StringCoding$StringDecoder.decode(Unknow n Source)
at java.lang.StringCoding.decode(Unknown Source)
at java.lang.String.<init>(Unknown Source)
at java.lang.String.<init>(Unknown Source)
at com.mysql.jdbc.ResultSet.getString(ResultSet.java: 1802)
at com.mysql.jdbc.Connection.loadServerVariables(Conn ection.java:3368)
at com.mysql.jdbc.Connection.initializePropsFromServe r(Connection.java:3199)
at com.mysql.jdbc.Connection.createNewIO(Connection.j ava:1788)
at com.mysql.jdbc.Connection.<init>(Connection.java:4 40)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonReg isteringDriver.java:400)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.hibernate.connection.DriverManagerConnectionPr ovider.getConnection(DriverManagerConnectionProvid er.java:110)
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(Sc hemaUpdate.java:118)
at org.hibernate.impl.SessionFactoryImpl.<init>(Sessi onFactoryImpl.java:252)
at org.hibernate.cfg.Configuration.buildSessionFactor y(Configuration.java:1043)
at com.nitspl.HibernateUtil.getSessionFactory(Hiberna teUtil.java:20)
at com.nitspl.updateremindertable.data(updatereminder table.java:61)
at com.nitspl.updateremindertable$PeriodicTask.run(up dateremindertable.java:198)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)
any one tell me how resolve this problem