Save Values to Database Only Once in A Week - java

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

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

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.

Related

Primary key is not loaded from PostgreSQL to JTable

I'm new to PostgreSQL and Java Swing.
In PostgreSQL company database, I have a users table and it has 4 fields: user_id, username, phone, and address.
CREATE TABLE users
(
user_id serial primary key,
username VARCHAR(40) not null,
phone VARCHAR(14) not null,
address VARCHAR(50)
);
I'm trying to load all fields from users table to JTable in Java Swing using Bound (Figure 1).
Then I bind the elements from the table (Figure 2).
As you can see in Figure 2, it shows only 3 fields except user_id. I need to load this user_id field as well because I need to perform CRUD data.
How can I achieve that?
I finally got the solution.
When I import the data from the database to the form, the Model (for example; Users.java) file is generated.
The problem was I firstly generated that file and later I added the user_id column to the database, so guess what, the Model file is somehow not updated and so the user_id is not there.
Therefore, I had manually added getter and setter for the user_id field, and now ok.

SymmetricDS length for type varchar cannot exceed N

so I'm trying to use symmetricDS for replicating java h2 database to postgres. I'm using the zip file simple configuration. Here is what happen. I followed the getting started guide, download the symmetricds, and try the demo, then I tried with my own configuration with some table in the trigger. But:
If I replicate the table without varchar field in h2 it works perfectly fine.
If I have a table that has varchar field in it, it crash during creating the table.
JdbcSqlTemplate - ERROR: length for type varchar cannot exceed 10485760
Position: 161. Failed to execute: CREATE TABLE "asset"(
"db_id" BIGINT NOT NULL DEFAULT nextval('"asset_db_id_seq"'),
"id" BIGINT NOT NULL,
"account_id" BIGINT NOT NULL,
"name" VARCHAR(2147483647) NOT NULL,
"description" VARCHAR(2147483647),
"quantity" BIGINT NOT NULL,
"decimals" SMALLINT NOT NULL,
"initial_quantity" BIGINT NOT NULL,
"height" INTEGER NOT NULL,
"latest" BOOLEAN DEFAULT 'TRUE' NOT NULL,
PRIMARY KEY ("db_id")
)
indeed a clear error saying the varchar should not exceed 255, but that's how the source database is, is there anyway to force any varchar to TEXT type? or are there any other way around this? or is this a bug in symmetricds has yet to be solved?
Thanks.
I managed to go way around this by creating the table on target database manually. Here is what I did before running bin/sym.
generate query for table I want to create using dbexport by bin/dbexport --engine corp-000 --compatible=postgres --no-data table_a table_b > samples/create_asset_and_trade.sql
modify the flaw in generated query file samples/create_asset_and_trade.sql. in my case it's the length of the varchar.
after fixing that, run the generated query to fill in the target database using dbimport. bin/dbimport --engine store-001 samples/create_asset_and_trade.sql.
running bin/sym should be okay now, it'll detect that the table is already created, and skip the table creation step.
This is not the ideal way, but it should work for now.

Spring boot security - create root user on startup

My Spring Boot web app uses form authentication with spring-boot-security.
I have two tables:
User
CREATE TABLE user
(
id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
username VARCHAR(255) NOT NULL,
password CHAR(60) NOT NULL,
//...
);
Role
CREATE TABLE role
(
id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
r_name VARCHAR(45) NOT NULL
);
CREATE UNIQUE INDEX sys_role_r_name_uindex ON sys_role (r_name);
Join table
CREATE TABLE ref_user_role
(
user_id INT(11) NOT NULL,
role_id INT(11) NOT NULL,
// ...
);
In order to add more users with other roles/priviledges, one has to be of role ROLE_ROOT in sys_role table. This root user has to exist before the add-more-users action, logically.
What I have tried is that
I ran a sql script containing the root-user-insertion statement before I deployed the app. Obviously, I had to also manually generate an encrypted password for this root user.
I grammatically run an sql script to insert the root credentials. My concern is that it might not be safe to include my ROOT user credentials in the data.sql file. If I put encrypted version of the password, I have to encrypt it beforehand.
Another way I can think of is that I can create a root-creation-page ONLY the first time the app starts. I will need a secret code(only the I and the app knows) in addition to the username and password so that no other random people can create the root account.
Are these the common ways to do this?
If not, what are some of the good ways?
If you're using Spring Boot, all you have to do is to place schema.sql and data.sql scripts within resources classpath.
The schema.sql will be in charge of creating your tables and associations between them, and the data.sql will be in charge of initializing your tables with data, so you can put there your insert into tables statements.
when you upload the service these scripts will be executed automatically by Spring Boot each upload.
for further reading take a look at:
http://docs.spring.io/spring-boot/docs/1.4.2.RELEASE/reference/htmlsingle/#howto-database-initialization

Trying to add a database table to my JTable in Netbeans

As the title question states, I am attempting per the instructions of this tutorial: https://blogs.oracle.com/NetBeansSupport/entry/populating_jtable_from_mysql_database
And the message I keep receiving is: "Tables without primary keys are not supported by the Java Presence API"
The thing that is driving me crazy is that the table that I created myself using SQL does have a primary key...
Are there other(working) methods to do this, because I have looked and tried and have failed time after time again...
btw this is the SQL for those who are wondering(the server is running via localhost)
CREATE TABLE Customers
(
KL_NR INT NOT NULL AUTO_INCREMENT,
NAAM VARCHAR(30) NOT NULL,
EMAIL VARCHAR(30),
ADRES VARCHAR(30),
PLAATS VARCHAR(100),
POSTCODE VARCHAR(6),
TELEFOON VARCHAR(13),
BEDRIJFSNAAM VARCHAR(30),
PRIMARY KEY (KL_NR)
);
Just FYI I have a form setup in my application that can add rows in the database, so it's not a connection issue.
Thanks in advance.
EDIT: changed it to only 1 PK but as i stated in the comments it doesn't change anything. even when i try to import it using: the table contents and binding it.
I fixed it by making an updateTable class
String sql = "select * from customers";
preparedStatement=connect.prepareStatement(sql);
resultSet=preparedStatement.executeQuery();
Table_customer.setModel(DbUtils.resultSetToTableModel(resultSet));
//this part forces the premade pallet Jtable to morph
//into the model of my mysql server database table

How to create log activity from java to MySQL?

I want to create a log activity for each user when they login from my apps and insert into MySQL database with the structure like
id_log (int, primary key, auto increment)
username (varchar)
time (timestamp)
The problem is every pc has a different timestamp and I only know to get timestamp on a local machine, is there any way to create a log activity based on a timestamp from a PC that storing database? I ask the other and said to use log4j but I still don't get it.
You can use MySQL's NOW() function, but the TIMESTAMP datatype automatically initialises to the current time by default:
CREATE TABLE logs (
id_log SERIAL,
username VARCHAR(255),
time TIMESTAMP
);
INSERT INTO logs (username) VALUES ('eggyal');
See it on sqlfiddle.

Categories

Resources