• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hql problem

 
yogesh totare
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi to all,

List result = session1.createQuery("select l.username , l.password from Login AS l ").list();

this is my query and now i have to take username and password from the result, then what code should i write there?


thanks in advance

yogesh
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in your login table, you'll have something like:

id userName PassWord
1 "A" 123
2 "B" 456
3 "C" 1z9

your List result contains arrays:
("A", 123), ("B", 456), etc

i guess you can code like this:

hope this helps
 
S.R.K.Vivek Raju
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi yogesh

Here is an example and think this should clear your doubt:--

The example is as follows

public MessageDataBO addOutgoingMessage(MessageDataBO msgData)
{
Session session=null;
int arr_cnt = 0;
try
{
Configuration config = new Configuration().configure();
config.addClass(MessageDataBO.class);
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();

Query query = session.createQuery("from MessageDataBO");
System.err.println("Executing HQL " + query.getQueryString());

try {
int count = 0;
List list = query.list();
if (null != list){
Iterator i=list.iterator();

String str_mess [][] = new String [list.size()][7];
while(i.hasNext())
{
msgData = ((MessageDataBO)i.next());
str_mess[arr_cnt][0] = msgData.getIdGroup();
str_mess[arr_cnt][2] = msgData.getDateTime().toString();
str_mess[arr_cnt][5] = msgData.getMessage();
arr_cnt++;
}

System.err.println("The query returned " + list.size() + " rows");
this.str_mess = str_mess;
}
else {
System.err.println("The result of the query was null");
}

} catch (Exception e1){
System.err.println("Exception " + e1.getMessage());
e1.printStackTrace();
}

}
catch(HibernateException he)
{
System.out.println("Hibernate Exception :"+he.getLocalizedMessage());
he.printStackTrace();
}

finally
{
if (session != null)
{
try
{
session.close();
}
catch (HibernateException e)
{
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
}
}
return msgData;
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic