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

时间:2023-02-06 15:42:54

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);

8 个解决方案

#1


11  

Try using setTimestamp instead of setDate, since a timestamp is the prefered format for data and time.

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

#2


5  

you can set Timestamp import java.sql.Timestamp;

您可以设置时间戳导入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—列设置为时间戳。

#3


2  

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())"');");

#4


1  

Use the sql function now().

使用sql函数now()。

INSERT INTO table date_field VALUES(NOW());

#5


1  

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();

#6


0  

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

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

#7


0  

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

我希望你服务

#8


-1  

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

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

Why not to try:

为什么不尝试:

String date = "2010-07-28";

字符串的日期=“2010-07-28”;

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

支撑。executeUpdate(“插入MYTABLE(时间)“+ s);

It's just for the date which you want.

只是为了你想要的日期。

If you want insert now, use the command like:

如果您现在需要插入,请使用以下命令:

INSERT INTO mytable(now());

现在插入mytable(());

#1


11  

Try using setTimestamp instead of setDate, since a timestamp is the prefered format for data and time.

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

#2


5  

you can set Timestamp import java.sql.Timestamp;

您可以设置时间戳导入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—列设置为时间戳。

#3


2  

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())"');");

#4


1  

Use the sql function now().

使用sql函数now()。

INSERT INTO table date_field VALUES(NOW());

#5


1  

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();

#6


0  

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

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

#7


0  

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

我希望你服务

#8


-1  

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

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

Why not to try:

为什么不尝试:

String date = "2010-07-28";

字符串的日期=“2010-07-28”;

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

支撑。executeUpdate(“插入MYTABLE(时间)“+ s);

It's just for the date which you want.

只是为了你想要的日期。

If you want insert now, use the command like:

如果您现在需要插入,请使用以下命令:

INSERT INTO mytable(now());

现在插入mytable(());