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.
Related
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.
Example: Suppose i have created auto generated ID in database. And now in JSP page i dont have to create an ID column for it because it will be auto generated.. But when im running the program its giving error. So after creating sequence is there any extra query i have to write in JSP to specify that my ID is auto generated.??
Assume that you have
...
ID BIGINT PRIMARY KEY AUTO_INCREMENT,
...
in this case you don't need specify anything in Java, (but in case you wanted to use sequence then you need)
In case like above you can send Java object to ORM or to JDBC (with proper insert query) and DB will generate ID automatically.
Good luck
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.
I am working with Postgresql database and Java. I am trying to insert into Postgresql database using prepared statement so I am using Batch insert as well. And I am trying to insert 1000 records at a time.
And below is the exception I am getting when I am trying to insert into one of my tables in batches.
java.sql.BatchUpdateException: Batch entry 0 INSERT into data_week (name, address, version, type, size, fax, phone) values('helloooooo', '360 S', '5.0beta4', NULL, '-1', '673', '300') was aborted. Call getNextException to see the cause.
Does anyone know what does thise error means? And also, can I use batch insert for Postgresql database or not?
EDIT: Below is my schema for the table data_week
CREATE TABLE data_week
(
name character varying(256),
address character varying(256),
version character varying(256),
type integer,
size integer,
fax character varying(256),
phone integer,
created_date timestamp without time zone NOT NULL DEFAULT (now() - '7 days'::interval),
updated_date timestamp without time zone NOT NULL DEFAULT now()
)
In example: If I'm accessing the SMS database, there is a 'Date' field for message timestamp. However, say I load messages with an older timestamp into the database, how do I know that they were added after the later timestamped messages? Is there a modified or created date for table rows that I can access?
Edit and clarification:
The SMS table has the following columns: long _id, thread_id, address, person, date, protocol, read, status, type, reply_path_present, subject, body, service_center, locked,error_code, & seen.
'date' refers to the date which the message was sent, not when it was added to the database. Does the Android database (SQLite) track when data is added or modified in the database? If so, how do I access it? If it does not, and simply add/removes/updates data without logging, that's an answer too.
you can solve this problem by creating a column containing the current date and time (current means at the moment, the row was inserted):
create table mytable(_id int primary key, date datetime default current_timestamp);
this will result in a timestamp in every row that points to the moment of insertion
another solution would be simply insert the current timestamp out of the SQLiteOpenHelper like:
ContentValues values = new ContentValues(2);
values.put(COLUMN_NAME_ID, id);
values.put(COLUMN_NAME_TITLE, title);
values.put(COLUMN_NAME_CREATED_DATE, System.currentTimeMillis());
dbHelper.getWritableDatabase().insert(TABLE_NAME, null, values);
There is no date in sqlite for when the row was inserted or updated.