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

来源:互联网 发布:企业商标设计软件 编辑:程序博客网 时间:2024/05/21 08:01

 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);
    }

原创粉丝点击