10_传智播客JDBC_jdbc中的数据类型与日期问题

 java.sql.Date; java.sql.Time; java.sql.Timestamp 都是继承于 java.util.Date

 java.sql 关于时间的类型 对应于数据库的字段类型(Type)。

 这样 就会存在于 一个转换关系

 

package five.base;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import five.utils.UtilsSingle;

public class DateTest {

 /**
  * @param args
  * @throws SQLException
  */
 public static void main(String[] args) throws SQLException {

  //create(100, "name1", new Date(), 1000.58f, "sssssssss");
  read("name1");
 }

 public static void create(Integer id, String name, Date brithday,
   float money, String password) throws SQLException {
  Connection connection = null;
  PreparedStatement preparedStatement = null;
  ResultSet resultSet = null;

  UtilsSingle util = UtilsSingle.getUtilsSingleInstance();
  try {
   connection = util.getConnection();
   String sql = "insert into user (id, name, brithday, monny, password) value (?,?,?,?,?)";
   preparedStatement = connection.prepareStatement(sql);
   preparedStatement.setInt(1, id);
   preparedStatement.setString(2, name);

   // java.sql.Date; java.sql.Time; java.sql.Timestamp 都是继承于 java.util.Date
   // 所以 要将子类转成父类
   preparedStatement.setDate(3, new java.sql.Date(brithday.getTime()));

   preparedStatement.setFloat(4, money);
   preparedStatement.setString(5, password);
   int i = preparedStatement.executeUpdate();
   System.out.print("rs is " + i);
  } finally {
   util.free(connection, preparedStatement, resultSet);
  }
 }

    static void read(String name) throws SQLException{
       
     PreparedStatement ps = null;
     ResultSet rs = null;
     UtilsSingle instance = UtilsSingle.getUtilsSingleInstance();

     // 1 创建连接
        Connection connection = instance.getConnection();
        try {
         
            // 2 创建语句
         String sql = "select *from user where name = ?";
         ps = connection.prepareStatement(sql);
         // 向占位符 设置参数
         ps.setString(1, name);
         rs = ps.executeQuery();
            // 5 处理结果
            while (rs.next()) {
             // sql.Date-->util.Date
             Date brithDate = new Date(rs.getDate("brithday").getTime());
             System.out.print(brithDate);

 

             //sql.Date 有格式化输出
             brithDate = rs.getDate("brithday");
             System.out.print(brithDate);
            }
        } finally {
         instance.free(connection, ps, rs);
        }
    }

}

 

    public String toString () {
 int year = super.getYear() + 1900;
 int month = super.getMonth() + 1;
 int day = super.getDate();

        char buf[] = "2000-00-00".toCharArray();
        buf[0] = Character.forDigit(year/1000,10);
        buf[1] = Character.forDigit((year/100)%10,10);
        buf[2] = Character.forDigit((year/10)%10,10);
        buf[3] = Character.forDigit(year%10,10);
        buf[5] = Character.forDigit(month/10,10);
        buf[6] = Character.forDigit(month%10,10);
        buf[8] = Character.forDigit(day/10,10);
        buf[9] = Character.forDigit(day%10,10);
  
 return new String(buf);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值