Share on Google+Share on Google+

Get date data type from table

Advertisement
In this example , we will get "Date" data type from a table of "Mysql" database

GET DATE DATA TYPE FROM TABLE

In this example , we will get "Date" data type from a table of "Mysql" database and it also display current "date". And it will also display time elapsed in Milliseconds since Jan. 1, 1970.

fetchdate.java 

import java.sql.*;
import java.io.*;
import java.util.Date;
public class fetchdate {
  public static void main(String a[])
  {
     Connection con = null;
     String url = "jdbc:mysql://192.168.10.13:3306/";
     String dbName = "ankdb";
     String driver = "com.mysql.jdbc.Driver";
     String userName = "root";
     String password = "root";
     try {
            Class.forName(driver).newInstance();
            con = DriverManager.getConnection(url+dbName,userName,password);
            Statement st = con.createStatement();
       ResultSet rs= st.executeQuery("select name,DOB from student where name ='Ankit'");
            while(rs.next()) {
                String name = rs.getString(1);
        String data = rs.getString(2);
                System.out.println(name +"\t"+data);
            }
        Date date = new Date();
        System.out.println("Today's date");
        System.out.println(date);
        
        long msec = date.getTime();
        System.out.println("Milliseconds since Jan. 1, 1970 GMT= " + msec);
        st.close();
        }
        catch( Exception e ) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
     }
}

OUTPUT


C:\Program Files\Java\jdk1.6.0_18\bin>java fetchdate
Ankit 1985-06-06
Today's date
Thu Jun 03 19:09:15 IST 2010
Milliseconds since Jan. 1, 1970 GMT= 1275572355937

Download this code

Advertisements

Advertisement

Posted on: June 4, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.