Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

GET DATE in JDBC

This example shows how to retrieve date from existing table of MySql database. Date functions can be used in the SELECT statement or in the WHERE clause of a Query.

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

GET DATE in JDBC

  

This example shows how to retrieve date from existing table of MySql database. Date functions can be used in the SELECT statement or in the WHERE clause of a Query. Various date functions of Date are-

ADDDATE()
This function is used for add date to any other date.

ADDTIME()
This function is used for add time.

CURDATE(), CURRENTDATE(), CURRENTDATE
These three are synonyms, are used for display the current date.

CURRENTTIME(), CURRENTTIME, CURTIME()
These are same used for current time.

DATE_ADD()
This is used for adding two dates.

DATEDIFF()
This is used for subtraction of two dates.

DAY(), DAYOFMONTH()
It shows the day of month from 1 to 31.

DAYNAME()
It shows the name of the weekday.

DAYOFWEEK()
It returns the index of weekdays.

DAYOFYEAR()
It returns the day of year from 1 to 366.

LASTDAY()
It returns the last day of the month.

LOCALTIME(),LOCALTIMESTAMP(),NOW()
It returns the current date and time.

MONTH()
It returns the month from the date has gone.

MONTHNAME()
It returns the name of the month.

SECOND()
It returns the seconds from 0 to 59.

STR_TO_DATE()
It converts a string to a date.

SYSDATE()
It returns the time of execution the function.

TIMEDIFF()
It returns the diffrence of time.

WEEK()
It returns the week number.

WEEKDAY()
It returns the weekday index.

WEEKOFYEAR()
It returns the week of year from 1 to 53.

YEAR()
It returns the year.

YEARWEEK()
It returns the year and week.

GetDate.java

import java.sql.*;
import java.io.*;
public class GetDate {
  public static void main(String a[])
  {
 Connection con = null;
 String url = "jdbc:mysql://lacalhost:3306/";
 String dbName = "vineej";
 String driver = "com.mysql.jdbc.Driver";
 String userName = "vineej";
 String password = "no";
 try {
  Class.forName(driver).newInstance();
  con = DriverManager.getConnection
  (url+dbName,userName,password);

  Statement st = con.createStatement();
  ResultSet rs= st.executeQuery("select Dates 
  from Student where Name ='Ravi'"
);
  while(rs.next()) {
  String data = rs.getString(1);
  System.out.println(data);
  }
  System.out.println("Results");
  st.close();
  }
  catch( Exception e ) {
  System.out.println(e.getMessage());
  e.printStackTrace();
  }
 }
}


Description of the Program
This program is concern with retrieve the date from existing table in the database. Student is the of a table and ravi is the name of a student. Dates is the field in table from where we can retrieve the date.When the query successfully executed it will shows "Results".

Description of code

1.
First in the program we import the java.sql and java.io packages then define the GetDate class.

import java.sql.*;
import java.io.*;

2.Create a connection with MySql database

con = DriverManager.getConnection(url+dbName,userName,password);

3.Create statement and then execute query with the SELECT statement.

Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select Dates from Student where Name ='Ravi");

4.Finally got the result by the getString() method.

while( rs.next() ) {
String data = rs.getString(1);
System.out.println( data ); 
}

5. After all connection should be closed.

con.close();

6. printStackTrace() method
The method is used to show error messages. If the connection is not connected then it throws the exception and print the message.

Download Source Code:

Advertisements

Ads
Share on Google+Share on Google+

GET DATE in JDBC

Posted on: July 17, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: GET DATE in JDBC  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads