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.
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:
|