Java 如何使用 SQL 在数据库中插入当前日期和时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3350893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:31:12  来源:igfitidea点击:

How to insert current date and time in a database using SQL?

javasql

提问by lakshmi

I used the following code, but the DateTime field in SQL is represented as:

我使用了以下代码,但 SQL 中的 DateTime 字段表示为:

2005-04-08 00:00:00

I want to have the time too. what should I change?

我也想有时间 我应该改变什么?

Here is my code below:

这是我的代码如下:

// Get the system date and time.
java.util.Date utilDate = new Date();
// Convert it to java.sql.Date
java.sql.Date date = new java.sql.Date(utilDate.getTime());
...
...
...
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setDate(1, date);

采纳答案by perdian

Try using setTimestampinstead of setDate, since a timestamp is the prefered format for data andtime.

尝试使用setTimestamp而不是setDate,因为时间戳是数据时间的首选格式。

回答by Reddy

Try using setDate(utilDate) instead of trying to set SQL date. In SQL, date and time are different data types.

尝试使用 setDate(utilDate) 而不是尝试设置 SQL 日期。在 SQL 中,日期和时间是不同的数据类型。

回答by Truong Ha

Try this:

尝试这个:

Connection con;
Statement stmt;
con = DriverManager.getConnection(url, username, password);
stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO MYTABLE(time) " +
"VALUES ('" + new java.sql.Date(System.CurrentTimeMillis())"');");

回答by Toto

Use the sql function now().

使用 sql 函数 now()。

INSERT INTO table date_field VALUES(NOW());

回答by rainisic

Actually, the date in database is a string like "yyyy-mm-dd".

实际上,数据库中的日期是一个类似“yyyy-mm-dd”的字符串。

Why not to try:

为什么不试试:

String date = "2010-07-28";

String date = "2010-07-28";

stmt.executeUpdate("INSERT INTO MYTABLE(time) " + s);

stmt.executeUpdate("INSERT INTO MYTABLE(time) " + s);

It's just for the date which you want.

这只是为了你想要的日期。

If you want insert now, use the command like:

如果要立即插入,请使用如下命令:

INSERT INTO mytable(now());

插入 mytable(now());

回答by shanmugam

you can set Timestamp import java.sql.Timestamp;

你可以设置 Timestamp import java.sql.Timestamp;

stmt.setTimestamp(1, new Timestamp(System.currentTimeMillis()));

instead of

代替

stmt.setDate(1, date);

o/p will be: 2014-05-16 08:34:52.977

o/p 将是:2014-05-16 08:34:52.977

set your DB --column as Timestamp.

将您的 DB --column 设置为时间戳。

回答by Brayan Escobedo

I did as follows:

我做了如下:

con = new BDConesion(); //Conexion with de DB
Connection cn = con.conexion()
PreparedStatement st = null;
st = cn.prepareStatement("INSERT INTO TABLE (Id,Fecha,contendido) VALUES(?,?,?)");
st.setString(1,String.valueOf(key)); //convert the int to string
st.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
st.setString(3,Cont);//Cont it is a varible string
st.executeUpdate();

I hope you serve

我希望你服务

回答by a_horse_with_no_name

I wouldn't bother retrieving the current time in Java, just let the database do the job:

我不会费心在 Java 中检索当前时间,只需让数据库完成这项工作:

String sql = "insert into some_table (some_column, timestamp_column) values (?, current_timestamp)";
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setInt(1, 42);
stmt.executeUpdate();