JDBC学习笔记—对时间类型的处理

    public static void main(String[] args) {


        getTime();
    }

    public static void saveTime(){
        //定义oracle数据库的驱动的类
        String driverClass = "oracle.jdbc.OracleDriver";
        //定义连接的oracle的url
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
        //用户名
        String username = "scott";
        //密码
        String password = "tiger";

        //定义Connection连接
        Connection conn = null;
        //定义数据库的sql执行对象
        PreparedStatement ps = null;
        //定义要执行的sql
        String addSql = "insert into  time_test values(?,?,?)";


        try {

            //注册数据库的驱动程序
            Class.forName(driverClass);
            //获得数据库的连接
            conn = DriverManager.getConnection(url, username, password);
            //创建sql执行对象
            ps = conn.prepareStatement(addSql);

            java.util.Date date = new java.util.Date();

            ps.setDate(1, new Date(date.getTime()));
            ps.setTime(2, new Time(date.getTime()));
            ps.setTimestamp(3, new Timestamp(date.getTime()));

            int count = ps.executeUpdate();
            System.out.println("数据相应条数:"+count);

        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                if(ps != null){
                    ps.close();
                }
                if(conn != null){
                    conn.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void getTime(){
        //定义oracle数据库的驱动的类
        String driverClass = "oracle.jdbc.OracleDriver";
        //定义连接的oracle的url
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
        //用户名
        String username = "scott";
        //密码
        String password = "tiger";

        //定义Connection连接
        Connection conn = null;
        //定义数据库的sql执行对象
        PreparedStatement ps = null;

        //定义结果集对象
        ResultSet rs = null;
        //定义要执行的sql
        String sql = "select * from time_test";


        try {

            //注册数据库的驱动程序
            Class.forName(driverClass);
            //获得数据库的连接
            conn = DriverManager.getConnection(url, username, password);
            //创建sql执行对象
            ps = conn.prepareStatement(sql);

            rs = ps.executeQuery();
            /*while(rs.next()){
                Date date = rs.getDate(1);
                Time time = rs.getTime(2);
                Timestamp timestamp = rs.getTimestamp(3);
                System.out.println("日期:"+date);
                System.out.println("时间:"+time);
                System.out.println("时间戳:"+timestamp);
            }*/

            /*while(rs.next()){
                java.util.Date date = rs.getDate(1);
                java.util.Date time = rs.getTime(2);
                java.util.Date timestamp = rs.getTimestamp(3);
                System.out.println("日期:"+date);
                System.out.println("时间:"+time);
                System.out.println("时间戳:"+timestamp);
            }*/
            while(rs.next()){
                java.util.Date date = rs.getDate(1);
                java.util.Date time = rs.getTimestamp(2);
                java.util.Date timestamp = rs.getTimestamp(3);
                System.out.println("日期:"+date);
                System.out.println("时间:"+time);
                System.out.println("时间戳:"+timestamp);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                if(ps != null){
                    ps.close();
                }
                if(conn != null){
                    conn.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值