- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert a Date value to string in JDBC?
The toString() method of the java.sql.Date class returns the escape format: yyyy-mm-dd of the date represented by the current date object. Using this method you can convert a Date object to a String.
Date date = rs.getDate("Dispatch_Date"); date.toString());
Assume we have a table named dispatch_data 3 records as shown below:
+--------------+------------------+---------------+----------------+ | Product_Name | Name_Of_Customer | Dispatch_Date | Location | +--------------+------------------+---------------+----------------+ | KeyBoard | Amith | 1981-12-05 | Hyderabad | | Ear phones | Sumith | 1981-04-22 | Vishakhapatnam | | Mouse | Sudha | 1988-11-05 | Vijayawada | +--------------+------------------+---------------+----------------+
Following JDBC program establishes connection with the database retrieves the contents of the dispatch_data table, converts the date objects into String values using the toString() method and displays the contents of the table along with the Date values (which are converted into String format):
import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class DateToString { public static void main(String args[])throws Exception { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating a Statement object Statement stmt = con.createStatement(); //Creating Statement object stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from dispatch_data"); //Retrieving values while(rs.next()) { System.out.println("Product Name: "+rs.getString("Product_Name")); System.out.println("Name Of The Customer: "+rs.getString("Name_Of_Customer")); //Retrieving the date Date date = rs.getDate("Dispatch_Date"); //Converting Date object to String System.out.println("Date: "+date.toString()); System.out.println(); } } }
Output
Connection established...... Product Name: KeyBoard Name Of The Customer: Amith Date: 1981-12-05 Product Name: Ear phones Name Of The Customer: Sumith Date: 1981-04-22 Product Name: Mouse Name Of The Customer: Sudha Date: 1988-11-05
- Related Articles
- How to convert a String into a Date object using JDBC API?
- How to convert a timestamp object in to Date in JDBC program?
- How to convert a Date object in to Timestamp in JDBC program?
- How to convert a Python date string to a date object?
- How to insert Date value into table in JDBC?
- How to convert a string to date in MySQL?
- How to convert String to Date in java?
- How to Convert Date to String in TypeScript?
- How to convert a string in to date object in JavaScript?
- How to convert a JavaScript date object to a string?
- How to convert Date to String in Java 8?
- How to convert a boolean value to string value in JavaScript?
- Convert String to Date in Java
- How to handle date in JDBC?
- Java Program to convert a String to Date

Advertisements