• jdbc 4.0


    1.存储MySQL数据库的date、time、timestamp、datetime以及year类型数据

     1 package com.rong.jielong;
     2 
     3 import java.sql.Connection;
     4 import java.sql.Date;
     5 import java.sql.DriverManager;
     6 import java.sql.PreparedStatement;
     7 import java.sql.Time;
     8 import java.sql.Timestamp;
     9 import java.util.Calendar;
    10 import java.util.Properties;
    11 
    12 public class Test7 {
    13 
    14     /**
    15      * @author 容杰龙
    16      */
    17     public static void main(String[] args) {
    18         try {
    19             Class.forName("com.mysql.jdbc.Driver");
    20             String url="jdbc:mysql://localhost:3306/rjl";
    21             Properties info=new Properties();
    22             info.setProperty("user", "rjl");
    23             info.setProperty("password", "123");
    24             Connection conn = DriverManager.getConnection(url, info);
    25             String sql="insert into time values(?,?,?,?,?)";
    26             //java.util.Date包含的信息有    年-月-日   时:分:秒
    27             PreparedStatement ps = conn.prepareStatement(sql);
    28             //数据库的date类型   2017-11-04 =======java.sql包的Date
    29             ps.setDate(1, new Date(new java.util.Date().getTime()));
    30             
    31             //数据库的time类型   21:22:19=======java.sql包的Time
    32             ps.setTime(2, new Time(new java.util.Date().getTime()));
    33             
    34             //数据库的timestamp类型 2017-11-04 21:22:19======java.sql包的Timestamp
    35             ps.setTimestamp(3, new Timestamp(new java.util.Date().getTime()));
    36             
    37             //数据库的datatime类型  2017-11-04 21:22:19======java.sql包的Timestamp
    38             ps.setTimestamp(4, new Timestamp(new java.util.Date().getTime()));
    39             
    40             //数据库的year类型  2017========java的无符号整数
    41             ps.setInt(5, Calendar.getInstance().get(Calendar.YEAR));
    42             
    43             int count = ps.executeUpdate();
    44             if (count>0) {
    45                 System.out.println("添加成功!");
    46             }else{
    47                 System.out.println("添加失败!");
    48             }
    49             
    50             ps.close();
    51             conn.close();
    52         } catch (Exception e) {
    53             e.printStackTrace();
    54         }
    55     }
    56 
    57 }

    2.读取数据库各种时间

     1 package com.rong.jielong;
     2 
     3 import java.sql.Connection;
     4 import java.sql.Date;
     5 import java.sql.DriverManager;
     6 import java.sql.PreparedStatement;
     7 import java.sql.ResultSet;
     8 import java.sql.Time;
     9 import java.sql.Timestamp;
    10 
    11 public class Test8 {
    12 
    13     /**
    14      * @author 容杰龙
    15      */
    16     public static void main(String[] args) {
    17         try {
    18             Class.forName("com.mysql.jdbc.Driver");
    19             String url = "jdbc:mysql://127.0.0.1:3306/rjl";
    20             String user = "rjl";
    21             String password = "123";
    22             Connection conn = DriverManager.getConnection(url, user, password);
    23             // 取第一条数据
    24             String sql = "select * from time limit 0,1";
    25             PreparedStatement ps = conn.prepareStatement(sql);
    26             ResultSet rs = ps.executeQuery();
    27             while (rs.next()) {
    28                 // 从查询的结果集中获取时间对象
    29                 Date date = rs.getDate("date");
    30                 Time time = rs.getTime("time");
    31                 Timestamp timestamp = rs.getTimestamp("timestamp");
    32                 Timestamp datetime = rs.getTimestamp("datetime");
    33 
    34                 int year = rs.getInt("year");
    35 
    36                 System.out.println(date + "###" + time + "###" + timestamp
    37                         + "###" + datetime + "###" + year);
    38                 // 输出2017-11-04###21:22:19###2017-11-04 21:22:19.0###2017-11-04
    39                 // 21:22:19.0###2017
    40             }
    41         } catch (Exception e) {
    42             e.printStackTrace();
    43         }
    44 
    45     }
    46 
    47 }
  • 相关阅读:
    Git配置SSH访问GitHub
    vue 관련
    node
    关于CheckBox和EditText在ListView里多布局的处理
    百度地图定位
    java常用简单正则表达式写法
    Android二维码开源项目zxing编译
    Andrew XUtils的session获得和cookieStore使用
    常用易忘知识点
    替换Fragment 报错 The specified child already has a parent. You must call removeView()
  • 原文地址:https://www.cnblogs.com/57rongjielong/p/7784800.html
Copyright © 2020-2023  润新知