fengchunlin_z
fengchunlin_z
2012-11-19 22:13

java.util.Date怎样转换为java.sql.Date

  • date
  • java
  • sql




public void insert(Project project) {

Connection conn=null;

PreparedStatement stmt=null;

String sql=null;

try {

conn=ConnectionUtils.openConnection();

sql="insert into t_project(t_no,t_name,t_start_date,t_end_date) values(?,?,?,?)";

stmt=conn.prepareStatement(sql);

stmt.setString(1, project.getNo());

stmt.setString(2, project.getName());

stmt.setDate(3,(Date)project.getStartDate());//这样写不报错,但是运行时出错   1

stmt.setDate(4, (Date)project.getEndDate());//                                              2

stmt.execute();

java.lang.RuntimeException: java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date//这是运行时的错
该怎么样去1,2所对应的代码呢?
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答