开发者

Retrieving records from MySQL DB returns Null <Beginner question>

Question 1.

I am using Netbeans 6.9.1 glassfish 3.1 . I am trying to retrieve some values from 开发者_开发百科the MySQL DB and save it on a List. My code as follows;

        @Override
public List<Hotel> list() {
   Query query  = getEm().createNativeQuery("select hotel.name from hotel");
   List<Hotel> hotels= query.getResultList();

   return hotels;
}

When i execute the above code i get NULL POINT exception, i have handled it anyway. There are values in the DB, and when i execute the SQL in the above code, i get the results from MYSQL. I need to any reasons why its returning NULL.

I am using JAVA persistence to save records.

Question 2.

private EntityManager em;
.... 
getEm().persist(hotel);

Without writing a SQL insert statement we could save records to a DB from the above code. Is there a way to retrieve the records as described in question 1. (To return the records as a List<Hotel> Object)


Query query = getEm().createNativeQuery("select hotel.name from hotel");

This will never return List, but it will return a List. For Exceptions, it's useful to also post the exception stacktrace.

For the second question, you don't need to retrieve them, the hotel object after the persist is the object that got persisted, add it to your list and you're done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜