|
-
July 18th, 2010, 01:39 PM
#1
javadb problem pls help
Hey Im new to java.I'm doing a project from java.since i'm new to java i have some problem with java db data retriving.Please guys help me
I have date field in my javadb.I have to retrive data from the table via date field.
Date date = jCalendar1.getDate();
session = HibernateUtil.getSessionFactory().getCurrentSessio n();
session.beginTransaction();
List result = session.createQuery("from Presentation.Appoinment where location=\'"+date+"\'").list();
for(Object a:result){
Appoinment app=(Appoinment)result;
JOptionPane.showMessageDialog(this, app.getLocation());
}
session.getTransaction().commit();
HibernateUtil.getSessionFactory().close();
but code won't work.I have tried to insert differet colum other that date to quary then the code will work.Please help me on this guys
-
July 19th, 2010, 08:33 AM
#2
Re: javadb problem pls help
When you say that it doesn't work, do you mean that you don't get the answer you expect, or that it throws an exception?
Looking at your query, I think there could be several problems. First off, you are trying to match the column named "location" with a String that represents a date. Remember, when you concatenate strings together with a Date object, Java will call the toString() method on that date object and convert it to a String.
If indeed this "location" column holds dates instead of addresses or cities (check the data) then you will need to either convert the "location" field to characters (a string) or convert the date (which has been converted to a String) back to a date.
You can use standard SQL to convert either like:
where to_char('MMDDYYYY',location)='07192010'
Or
where location = to_date('date','MMDDYYYY')
Where the MMDDYYYY is the format that the location field is set to, or you won't get a match.
The best way in my opinion is to to_char the date in your SQL, and format your date object in Java using the SimpleDateFormat class. That way you can ensure they are both of the same exact format when comparing.
Since you are using Hibernate, you can use the built in HQL functions for converting dates to chars or visa-versa. I don't write a whole lot of custom HQL so I don't know the syntax off of the top of my head.
Last edited by ProgramThis; July 19th, 2010 at 08:36 AM.
-
July 19th, 2010, 09:26 AM
#3
Re: javadb problem pls help
First of all, I agree with ProgramThis about the "location" vs date thing. You should check that first.
In this code
Code:
List result = session.createQuery("from Presentation.Appoinment where location=\'"+date+"\'").list();
I was wondering why your are escaping the ticks (\') in your strings? It isn't necessary and could be causing problems.
Another thing you can try is to set show_sql to true in your hibernate.config.xml
Code:
<property name="hibernate.show_sql">true</property>
This will output the SQL commands as the are sent to the database. It may give you some hint as to what is going on.
As ProgramThis said, if this doesn't help, post back with the specific errors you're having.
Please use code tags around code and exception stack traces.
Last edited by ajhampson; July 19th, 2010 at 09:32 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|