• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to retrieve Date from JDBC using sql

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
vinoth sathiyamoorthy
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at the Java™ Tutorials section.
 
vinoth sathiyamoorthy
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
vinoth sathiyamoorthy
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic