• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to retrieve Date from JDBC using sql

 
vinoth sathiyamoorthy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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
 
Vijitha Kumara
Bartender
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • 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?
 
Abhijit Rai
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • 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: 31
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • 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: 31
  • Mark post as helpful
  • send pies
  • 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
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • 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?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50189
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the Java™ Tutorials section.
 
vinoth sathiyamoorthy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11943
212
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • 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: 31
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic