| |

VerySource

 Forgot password?
 Register
Search
View: 1243|Reply: 8

Insert system time to oracle in java program

[Copy link]

2

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 Japan

Post time: 2020-2-12 10:00:01
| Show all posts |Read mode
Insert a piece of data into the oracle database, including the date type, but you can't get the time, you can only get the date! The procedures used in the dao program are:
pstmt.setDate (new java.sql.Date (new java.util.Date (). getTime ()));
pstmt.executeUpdate ();
The data obtained by querying in the database is:
2 127.0.0.1 2017-01-04 00: 00: 00.0
3 127.0.0.1 2017-01-04 00: 00: 00.0
4 127.0.0.1 2017-01-04 00: 00: 00.0
5 127.0.0.1 2017-01-04 00: 00: 00.0
Question: Why does the time appear as: 00:00:00? Is there any other good way to insert the system time and date into the database
Reply

Use magic Report

0

Threads

71

Posts

50.00

Credits

Newbie

Rank: 1

Credits
50.00

 China

Post time: 2020-4-8 21:45:01
| Show all posts
need

to_char (java.util.Date (). getTime (), 'YYYY-MM-DD HH24: MI: SS')

such as:
String sqlStr = "insert into" + tableName
+ "(name, age, height, add_date) values ​​(?,?,?, to_char (sysdate, 'YYYY-MM-DD HH24: MI: SS'))";
The
pstmt = con.prepareStatement (sqlStr);
pstmt.setString (1, client.getName ());
pstmt.setInt (2, client.getAge ());
pstmt.setInt (3, client.getHeight ());
//pstst.setString(4,sysdate);
The
pstmt.executeUpdate ();
Reply

Use magic Report

2

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-4-10 16:30:01
| Show all posts
Thank youwhbilly;
  From your program, convert it to a string and store it in the database. Is it built as a varchar2 type when the table is created? Using your method I compiled errors in jbuilder,
The statement I wrote is:
 pstmt.setDate (2, to_char (new java.util.Date (). getTime (), 'YYYY-MM-DD HH24: MI: SS'));
The error is as follows:
"Dao.java": unclosed character literal at line 146, column 67
"Dao.java": unclosed character literal at line 146, column 89
Please take a look, thank you
Reply

Use magic Report

2

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-4-11 08:45:02
| Show all posts
I also tried to change the attribute of the field to varchar2 type, this statement
  pstmt.setString (2, to_char (new java.util.Date (). getTime (), "YYYY-MM-DD HH24: MI: SS"));
jbuilder says "
Dao.java ": cannot find symbol; symbol: method to_char (long, java.lang.String), location: class dao.Dao at line 146, column 30
What the hell is going on! Thanks a lot!
Reply

Use magic Report

0

Threads

13

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-5-14 20:45:01
| Show all posts
1. to_char first, then to_date
Reply

Use magic Report

0

Threads

13

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-5-15 08:15:01
| Show all posts
There is such a demand that feels weird. Sysdate can be used to insert the system time.

In this case, the time of the machine where Java is located must be the same as the time of the machine where Oracle is located.
Reply

Use magic Report

0

Threads

71

Posts

50.00

Credits

Newbie

Rank: 1

Credits
50.00

 China

Post time: 2020-5-16 01:30:01
| Show all posts
It is recommended to use sysdate, not getdate ()
The following is the original code of jsp for reference

String sqlStr = "insert into" + tableName
+ "(name, age, height, add_date) values ​​(?,?,?, to_char (sysdate, 'YYYY-MM-DD HH24: MI: SS'))";

pstmt = con.prepareStatement (sqlStr);
pstmt.setString (1, client.getName ());
pstmt.setInt (2, client.getAge ());
pstmt.setInt (3, client.getHeight ());
//pstst.setString(4,sysdate);

pstmt.executeUpdate ();
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 Invalid IP Address

Post time: 2020-5-25 00:15:02
| Show all posts
You should use java.sql.Timestamp instead of java.sql.Date
Reply

Use magic Report

2

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-6-18 17:00:01
| Show all posts
Thank you for the above answer, it is better to use the upstairs method, which is timestamp, but I use order by time in sql, which is also sorted by time, which can be sorted in oracle, but how to display the page in jsp Not sorted by time? Thanks for answering!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list