0

I have a situation, I want to generate a excel sheet through java program. I can generate excel sheet every time i execute the program. While i am executing program data saved to database, but I want to save data to database only once in a week. I have Two tables:

CREATE TABLE PROJECTS
(id int(10) NOT NULL AUTO_INCREMENT, 
project_name varchar(100) NOT NULL,
lastUpdated Date, PRIMARY KEY (id));

CREATE TABLE PROJECT_DATA
(id int(10) NOT NULL AUTO_INCREMENT,
project_id int(10), 
rca_field varchar(50),
 environment varchar(50), 
dateCreated Date,
 endDate Date, 
dataValue int(10), 
PRIMARY KEY (id), 
FOREIGN KEY (`project_id`) REFERENCES PROJECTS(`id`));

Can any body suggest me a way to do this in JAVA Program?

Thanks

ravikumar
  • 893
  • 1
  • 8
  • 12
Kuldeep Singh
  • 199
  • 1
  • 11
  • 2
    You don't need to write any codes in Java for that. It's more of a server administrative job. For windows: You can try using [task schedular](http://stackoverflow.com/questions/12034177/running-a-java-program-as-a-scheduled-task) to execute the java program once a week. For linux: Use [crontab](http://unix.stackexchange.com/questions/98914/how-to-use-crontab-for-a-java-file-in-linux) to execute the program once a week. – Sky May 29 '14 at 04:46
  • This is the way to do it, and most likely not inside a java program. – WestCoastProjects May 29 '14 at 04:52
  • Hi, I am sorry I can not set task schedular or cron job, because user can run program manually. I have create program so that no matter user running program, values should be saved in DB only once. – Kuldeep Singh May 29 '14 at 04:59
  • @KuldeepSingh Then it will be better for you to separate your program into 2 different modules. One to retrieve (select statement) the information. Another module to generate (insert) date to database. So user will have their own program to generate the excel by selecting the information using the data generated by the server pumped in information once a week (insert job) – Sky May 29 '14 at 05:03

2 Answers2

3

It sounds like you want to run a periodic service automatically, without requiring a human executing your application. If this is the case, you have a few main options:

On Windows, you can setup a scheduled task

On Posix, you can use cron

Using pure Java (personally recommended), you can use JavaEE's timer service. This will require an application server running, such as RedHat's JBoss, or Oracle's Glassfish: http://docs.oracle.com/javaee/6/tutorial/doc/bnboy.html

etherous
  • 709
  • 4
  • 10
  • Hi etherous, Thanks for Reply, but problem is, A person can execute job manually to generate excel sheet, but in that case values should not be saved to database they should be saved only one in DB no matter user how many time application is running manually. – Kuldeep Singh May 29 '14 at 04:57
  • @KuldeepSingh: There are ways of writing code in such a way that they are not executed manually. You can set an authentication on it. You can protect the packages. And many other ways. – Ravinder Reddy May 29 '14 at 05:13
  • [*Solution by BalusC*](http://stackoverflow.com/a/2249068/767881) is one of the adoptables. – Ravinder Reddy May 29 '14 at 05:20
  • @KuldeepSingh: If I understand, you may want to utilize a seperate database table. When a user runs the application, it sets a flag in a database record. Then, as a weekly service, that table is checked, and the records processed, and entries made in the other table – etherous May 29 '14 at 05:34
0

1-For Creating excel sheet you can call a Servlet which will generate the excel sheet.(use java POI). 2- For inserting values into DB you can schedule a cronjob which will save the data into db weekly.

chandresh
  • 141
  • 6