This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes How to retrieve Date from JDBC using sql Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to retrieve Date from JDBC using sql " Watch "How to retrieve Date from JDBC using sql " New topic
Author

How to retrieve Date from JDBC using sql

vinoth sathiyamoorthy
Ranch Hand

Joined: Sep 30, 2008
Posts: 31
Hi friends,

I wrote a small program to store and retrieve the date from sql database.

i have insert the user selected date into database
but i cannot retrieve stored date from my table.

i used jCalendar jCal as variable name like

jCal.setSelectedDate(,,,);

and

jCal.setDate(temp);

above both are not in use.

how to retrieve date from Table? using ResultSet?

please anyone help me.

thanks in advance

with hope
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

how to retrieve date from Table? using ResultSet?


Yes. To get the data stored in database you should query the database and data will be retrieved in the form of ResultSet.

Can you please show us the code you are currently having to retrieve the data?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Abhijit Rai
Ranch Hand

Joined: Aug 07, 2008
Posts: 41
Hi,
To access any database Using JDBC follow these steps :
1.Make sure the RDBMS server is up and running correctly.
2.Find out on which port the RDBMS server is .
3.Make sure that the relevant JDBC driver (for you RDBMS ) is present and in class path.

Once these steps are followed write a Java class to access the DB. The steps to be followed in the Class are:

1.Register the JDBC driver for your RDBMS
2.Define URL of database server for your database on the localhost with the relevant port number( default :3306)
save it in a String .

3.Get a connection to the database.

4.Get a Statement object eg:

5.Execute commands on that statement object.

6.Get a ResultSet back

7. Close everything:

Below is a sample ,I am using mySQL database named javatest,



HTH


SCJP5 ,SCWCD5
vinoth sathiyamoorthy
Ranch Hand

Joined: Sep 30, 2008
Posts: 31
while(rs.next())
{


String rs1=rs.getString(2);

System.out.println("result set row 1 is "+rs1);
txtitemname.setText(rs1);
txtbatchno.setText(rs.getString(3));

//calexpirydate.setSelectedDate(yyyy,mm,dd);

txtcategory.setText(rs.getString(6));
txtrate.setText(rs.getString(7));
txtquantity.setText(rs.getString(8));
txtvendorname.setText(rs.getString(9));
}
}


above coding is used.

comment line only i want to get the date when i stored in a database.

which method used to get the date from database.

thanks with hope
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

There are few methods (with some overloaded methods as well) in ResultSet for retrieving date/time (getDate(),getTimestamp() etc..), use the appropriate one for you.
vinoth sathiyamoorthy
Ranch Hand

Joined: Sep 30, 2008
Posts: 31
I can get the date from database using this this coding Date dat=rs.getDate(4);

but i cannot set this date to calender when user wants to view means.

in textfield we can set the text using this method -> jTextField1.setText(rs.getString(1));

but in calendar how to set selected date to set ? like calcombo.set???(dat)

i want to set calendar like above jTextField1.

which method used to set the date? and also how to pass above(dat) parameter to calendar? like setText(para);?

thanks with hope
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41173
    
  45
What kind of object is "calcombo"? Do its javadocs list any setXYZ method that takes a Date or Calendar object?

Above you were talking about a "jCal.setDate" method - can't you use that?


Ping & DNS - my free Android networking tools app
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Have a look at the Java™ Tutorials section.
vinoth sathiyamoorthy
Ranch Hand

Joined: Sep 30, 2008
Posts: 31
Hi,
Here i explain what i want.

jCalendarCombo as jCal.

i have inserted date into database. using add button.

now i want to edit or view what i already inserted in a database.

i know how to get and set the corresponding items to field(e.g TextField,JComboBox etc...)

but i can't set date(from database) to this jCal variable.

i can get the Date from database and assigned into Date obj .here obj have 15-05-2009 date, retrieved from database.

but i can't use this method(setDate(,,,) or setSelectedDate(,,,)) to setDate from obj.

only manually enter or set the date (setDate(18-05-2009)). i want replacement of setDate(obj)) any other method available ?

e.g

jTextField1.setText(rs.getString(1));

i want above statement to set the date which already in obj. like jCal.setDate(obj) or jCal.setSelectedDate(obj).this parameter is not working.


can you help me?

thanks with hope
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

vinoth sathiyamoorthy wrote:i can get the Date from database and assigned into Date obj .here obj have 15-05-2009 date, retrieved from database.

but i can't use this method(setDate(,,,) or setSelectedDate(,,,)) to setDate from obj.


If you can retrieve the date correctly from the database then it sounds like this has nothing to do with JDBC.

I assume you are talking about the JCalender components, and there is a method in that for JCalendarCombo setDate(java.util.Date). So what happens when you use that method?

Just saying something doesn't work does not help others to diagnose your problem. Please provide a little more information. If you are getting the date from the database (which you have confirmed how?) then what happens when you try to assign it to the JCalendarCombo? Do you get a compile time error, and if so what? Do you get a runtime exception, and if so what? Does it appear to work but the date is not set correctly in the combo?


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
vinoth sathiyamoorthy
Ranch Hand

Joined: Sep 30, 2008
Posts: 31
Hi,

I have JDateChooser if i insert into sql database means throw sql exception

-->java.sql.SQLException: [Microsoft][SQL Native Client][SQL Server]Conversion failed when converting datetime from character string.

what is this exception? how to handle?

i have set data type in my sql table as datetime format.


please guide me.


thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to retrieve Date from JDBC using sql
 
Similar Threads
RETREIVING MAX (DATE) FROM DATABASE
formatting a SQL date in MS Access
How to handle this Date format?
Max(Date)
how to update multiple table in frame