JDBC Date Processing usage detailed

Source: Internet
Author: User
Tags datetime getdate stmt

The JDBC Date processing usage is detailed and can be used as a standard class package JDBC to connect JDBC;

import java.sql.Connection;


import java.sql.Date;


import Java.sql.ResultSet;


import java.sql.SQLException;


import Java.sql.DriverManager;


import java.sql.Statement;


import Java.sql.Time;


import Java.sql.Timestamp;


import Java.text.SimpleDateFormat;


import Java.util.Calendar;


/** *//**


*


* @author Oakertree


*


*/


public class Testjdbcdate {


public static void Main (string[] args) {


Connection con = null;


Statement stmt = null;


ResultSet rs = null;


try {


class.forname ("Com.mysql.jdbc.Driver");


//New Com.mysql.jdbc.Driver ();


con = drivermanager.getconnection ("Jdbc:mysql://localhost:3307/test", "root", "Admini");


stmt = Con.createstatement ();


rs = stmt.executequery ("SELECT * from time");


while (Rs.next ()) {


        /**//*


Date D = rs.getdate ("date");


//SimpleDateFormat SDF = new SimpleDateFormat ("YYYY year mm month DD Day");


System.out.println (Sdf.format (d));


        */


        


        /**//*


time t = rs.gettime ("Time");


SimpleDateFormat sdf = new SimpleDateFormat ("hh when mm minute ss seconds");


System.out.println (Sdf.format (t));


        */


        


Date d = rs.getdate ("datetime");


time t = rs.gettime ("datetime");


SimpleDateFormat sdfdate = new SimpleDateFormat ("YYYY year mm month DD Day");


SimpleDateFormat sdftime = new SimpleDateFormat ("hh when mm minute ss seconds");


System.out.println (Sdfdate.format (d) + Sdftime.format (t));


        


        /**//*


Timestamp dt = Rs.gettimestamp ("datetime");


SimpleDateFormat sdf = new SimpleDateFormat ("yyyy mm month DD Day hh when mm minute ss seconds");


System.out.println (Sdf.format (DT));


        */


        


//Get the month
of the date

        /**//*


Calendar C = calendar.getinstance ();


C.settime (d);


System.out.println (C.get (calendar.month));


         */


      }


} catch (ClassNotFoundException e) {


E.printstacktrace ();


catch (SQLException e) {


E.printstacktrace ();


} finally {


try {


if (rs!= null) {


Rs.close ();


rs = null;


          }


if (stmt!= null) {


Stmt.close ();


stmt = null;


          }


if (con!= null) {


Con.close ();


con = null;


          }


} catch (SQLException e) {


E.printstacktrace ();


        }


      }


  }


}


/**//*


+----------+----------+------+-----+---------+-------+


| Field | Type | Null | Key | Default | Extra |


+----------+----------+------+-----+---------+-------+


| Date | Date |   YES | |    NULL | |


| Time | Time |   YES | |    NULL | |


| datetime | datetime |   YES | |    NULL | |


+----------+----------+------+-----+---------+-------+


*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.