Recent Posts

Thursday, 15 November 2018

Retrieving Date values from the database

     For Retrieving Date values from the database, we can use either simple Statement or PreparedStatement. The retrieved Date values are Stored in ResultSet in the form of "java.sql.Date" and we can get this value by using getDate() method. Once we got java.sql.Date object,we can format into our required form by using SimpleDateFormat object.

import java.sql.*;
import java.util.*;
import java.text.*;
 * @author ashok.mariyala
public class DateRetriveTest { 
   public static void main(String[] args) throws Exception { 
      Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","scott","tiger"); 
      String sqlQuery = "select * from users"; 
      PreparedStatement pst = con.prepareStatement(sqlQuery); 
      ResultSet rs = pst.executeQuery(); 
      SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
      while( { 
         String name = rs.getString(1); 
         java.sql.Date sdate=rs.getDate(2); 
         String date = sdf.format(sdate); 

Next Tutorial : Working with Large Objects - BLOB

Previous Tutorial : Inserting Date Values into Database

No comments:

Post a Comment