首页 > 代码库 > 日期的转换

日期的转换

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.util.Date;

import com.heima.jdbc.domain.Emp;

import com.heima.jdbc.utils.JdbcUtil;

public class JdbcDateDemo {

                   public static void main(String[] args) throws Exception {

                   // 创建emp对象

                   Emp emp = new Emp() ;

                   emp.setEmpNo(9527) ;

                   emp.setEname("华安") ;

                   emp.setHireDate(new Date()) ;

                  // 获取连接对象以及PreparedStatement

                   Connection conn = JdbcUtil.getConnection() ;

                   String sql = "insert into emp(empno , ename , hiredate) values (? , ? , ?)" ;

                   PreparedStatement ps = conn.prepareStatement(sql) ;

                    // 赋值

                   ps.setInt(1, emp.getEmpNo()) ;

                   ps.setString(2, emp.getEname()) ;

                   Date date = emp.getHireDate() ;

                   long time = date.getTime() ;

                   java.sql.Date date2 = new java.sql.Date(time) ;

                   ps.setDate(3, date2) ;

                   // 执行sql

                   ps.executeUpdate() ;

                    // 释放资源

                   JdbcUtil.close(conn, ps, null) ;

         }

}

import java.util.Date;

public class Emp {

         

         private int empNo ;                    // 员工编号

         private String ename ;              // 员工名称

         private String job;            // 岗位信息

         private int mgr ;                          // 经理编号

         private Date hireDate ;            // 入职日期

         private double sal ;           // 薪资

         private double comm ;              // 奖金

         private int deptNo ;          // 部门编号

 

         public int getEmpNo() {

                   return empNo;

         } 

         public void setEmpNo(int empNo) {

                   this.empNo = empNo;

         } 

         public String getEname() {

                   return ename;

         } 

         public void setEname(String ename) {

                   this.ename = ename;

         }

      public String getJob() {

                   return job;

         }

          public void setJob(String job) {

                   this.job = job;

         }

         public int getMgr() {

                   return mgr;

         }

         public void setMgr(int mgr) {

                   this.mgr = mgr;

         } 

         public Date getHireDate() {

                   return hireDate;

         }

      public void setHireDate(Date hireDate) {

                   this.hireDate = hireDate;

         }

          public double getSal() {

                   return sal;

         }

         public void setSal(double sal) {

                   this.sal = sal;

         } 

         public double getComm() {

                   return comm;

         }

public void setComm(double comm) {

                   this.comm = comm;

         }

          public int getDeptNo() {

                   return deptNo;

         }

         public void setDeptNo(int deptNo) {

                   this.deptNo = deptNo;

         }

         @Override

         public String toString() {

                   return "Emp [empNo=" + empNo + ", ename=" + ename + ", job=" + job

                                     + ", mgr=" + mgr + ", hireDate=" + hireDate + ", sal=" + sal

                                     + ", comm=" + comm + ", deptNo=" + deptNo + "]";

         }

}

 

 

 

日期的转换