create table tb_java8date (id int not null primary key auto_increment,t_date date, t_time time, t_datetime datetime);
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> </dependency>
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class App {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.1.100:3306/db_java8","root","root123");
PreparedStatement st = conn.prepareStatement("insert into tb_java8date (t_date,t_time,t_datetime)values(?,?,?)");
st.setObject(1, LocalDate.now());
st.setObject(2, LocalTime.now());
st.setObject(3, LocalDateTime.now());
st.execute();
st.close();
conn.close();
}
}
mysql> select * from tb_java8date; +----+------------+----------+---------------------+ | id | t_date | t_time | t_datetime | +----+------------+----------+---------------------+ | 1 | 2016-11-13 | 11:34:31 | 2016-11-13 11:34:31 | +----+------------+----------+---------------------+ 1 row in set (0.00 sec)
Exception in thread "main" com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: '\xAC\xED\x00\x05sr\x00\x0Djava.time.Ser\x95]\x84\xBA\x1B"H\xB2\x0C\x00\x00xpw\x07\x03\x00\x00\x07\xE0\x0B\x0Dx' for column 't_date' at row 1 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3845) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2447) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2594) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1901) at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1193) at com.pp.App.main(App.java:18)
create table tb_java8date (id SERIAL not null primary key,t_date date, t_time time, t_datetime timestamp);
<dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.4.1212</version> </dependency>
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class App {
public static void main( String[] args ) throws Exception {
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/pg_java8","admin","123456");
PreparedStatement st = conn.prepareStatement("insert into tb_java8date (t_date,t_time,t_datetime)values(?,?,?)");
System.out.println(st.getClass());
st.setObject(1, LocalDate.now());
st.setObject(2, LocalTime.now());
st.setObject(3, LocalDateTime.now());
st.execute();
st.close();
conn.close();
}
}
Exception in thread "main" org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of java.time.LocalDate. Use setObject() with an explicit Types value to specify the type to use. at org.postgresql.jdbc.PgPreparedStatement.setObject(PgPreparedStatement.java:1051) at com.pp.App.main(App2.java:16)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有