Java时间戳到MySQL时间戳
我有这个代码,但它不工作。我收到错误消息: “com.mysql.jdbc.MysqlDataTruncation:数据截断:第1行的'Datum_zalozeni'列的datetime值“”不正确” 我的数据库是:Java时间戳到MySQL时间戳,java,mysql,timestamp,Java,Mysql,Timestamp,我有这个代码,但它不工作。我收到错误消息: “com.mysql.jdbc.MysqlDataTruncation:数据截断:第1行的'Datum_zalozeni'列的datetime值“”不正确” 我的数据库是: CREATE TABLE Ucet ( Id_Uctu Int NOT NULL auto_increment primary key, Datum_zalozeni Timestamp NOT NULL, Id_klient Int NOT NULL ) ; 错误在
CREATE TABLE Ucet
(
Id_Uctu Int NOT NULL auto_increment primary key,
Datum_zalozeni Timestamp NOT NULL,
Id_klient Int NOT NULL
)
;
错误在哪里?我认为代码是正确的。MySQL通常接受格式为“2013-12-06 14:24:34”的时间戳。如果您共享要插入到数据库中的确切值,这将非常有用
Timestamp ts = u.getDatum_zalozeni();
System.out.println(ts);
pstmt.setTimestamp(1, ts);
什么是ts?我有一个生成系统时间戳的代码。它将0设置为979。看一看
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TimestampTest
{
public static void main(String[] args)
{
try
{
System.out.println("System Date: " + generateTimestamp("yyyy-MM-dd HH:mm:ss"));
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static Timestamp generateTimestamp(String format)
{
Timestamp timestamp = null;
try
{
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
Date date = dateFormat.parse(generateDate(format));
timestamp = new Timestamp(date.getTime());
}
catch (ParseException e)
{
e.printStackTrace();
}
return timestamp;
}
public static String generateDate(String format)
{
Date date = Calendar.getInstance().getTime();
DateFormat dateFormat = new SimpleDateFormat(format);
return (dateFormat.format(date));
}
}
您可以在'INSERT'变量中发布值吗?专用静态字符串INSERT=“INSERT into Ucet values(null,,?)”;当您手动插入时间戳时,它正在工作。这是ts:2015-04-25 16:17:22.979我们可以从ts中删除979并将其保留到2015-04-25 16:17:22吗?我是说因为这是一个MysqlDataTruncation错误,但是我如何删除979呢?这是时间戳格式。它正在工作。非常感谢你。但我不知道为什么它不起作用,因为我使用手册:“插入ucetnidb.Ucet值(null,'2015-03-3114:24:25.979',1);”这就是工作。
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TimestampTest
{
public static void main(String[] args)
{
try
{
System.out.println("System Date: " + generateTimestamp("yyyy-MM-dd HH:mm:ss"));
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static Timestamp generateTimestamp(String format)
{
Timestamp timestamp = null;
try
{
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
Date date = dateFormat.parse(generateDate(format));
timestamp = new Timestamp(date.getTime());
}
catch (ParseException e)
{
e.printStackTrace();
}
return timestamp;
}
public static String generateDate(String format)
{
Date date = Calendar.getInstance().getTime();
DateFormat dateFormat = new SimpleDateFormat(format);
return (dateFormat.format(date));
}
}