Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

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:

                         

» View all related tutorials
Related Tags: java sql mysql c database api ide jdbc data batch application io get vi exec state int bat this id

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.