Retrieving Date values
In this tutorial, we are going to discuss about Retrieving Date values from database using JDBC. 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 {
Class.forName("oracle.jdbc.OracleDriver");
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(rs.next()) {
String name = rs.getString(1);
java.sql.Date sdate=rs.getDate(2);
String date = sdf.format(sdate);
System.out.println(name+"..."+date);
}
con.close();
}
}
That’s all about the Retrieving Date values from database using JDBC. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!
Retrieving Date values