加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java JDBC-插入时间类型

发布时间:2020-12-15 07:26:25 所属栏目:Java 来源:网络整理
导读:public class Demo7 { public static void main(String[] args) { Connection conn=null; PreparedStatement ps=null; try { Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","dyl123
public class Demo7 { public static void main(String[] args) { Connection conn=null; PreparedStatement ps=null; try { Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","dyl123"); ps=conn.prepareStatement("insert into t_user(username,pwd,regTime,lastLoginTime) values(?,?,?)"); ps.setObject(1,"html5"); ps.setObject(2,"12355"); //java.sql.Date,显示年月茹 //java.sql.Time,显示时分秒 //java.sql.Timestamp,显示年月日时分秒 //sql.Date()没有空构造器,需要传入一个lang类型的数值 java.sql.Date date=new java.sql.Date(System.currentTimeMillis()); ps.setDate(3,date); //时间戳,可以穿传lang类型的数值或具体年月日时分秒 java.sql.Timestamp stamp=new java.sql.Timestamp(System.currentTimeMillis()); ps.setTimestamp(4,stamp); //插入随机日期 ps.execute(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { try { if(null!=ps) { ps.close(); } }catch(SQLException e) { e.printStackTrace(); } try { if(null!=conn) { conn.close(); } }catch(SQLException e) { e.printStackTrace(); } } } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读