Note that there are some explanatory texts on larger screens.

plurals
  1. POjava/hibernate query issue
    primarykey
    data
    text
    <p>I am having problem trying to execute a SQL/HQL statement using the WHERE clause.</p> <p>The application connects and executes this method when called without errors:</p> <pre><code>public void listPlayers( ){ Session session = sessionFactory.openSession(); Transaction tx = null; try{ tx = session.beginTransaction(); List players = session.createQuery("FROM Player").list(); for (Iterator iterator = players.iterator(); iterator.hasNext();){ Player player = (Player) iterator.next(); String tmpMessage = player.getFirstName().toString(); System.out.println(tmpMessage); writer.write("First Name: "+player.getPlayerName()+", Wealth: "+player.getWealth()+"\r\n"); writer.flush(); } tx.commit(); }catch (HibernateException e) { if (tx!=null) tx.rollback(); e.printStackTrace(); }finally { session.close(); } } </code></pre> <p>BUT, when I try to do this; I get an error: player_name field not found:</p> <pre><code> public void loadPlayer(String usr){ Session session = sessionFactory.openSession(); Transaction tx = null; try{ tx = session.beginTransaction(); String hql = "SELECT * FROM Player P WHERE P.player_name = "+usr; Query query = session.createQuery(hql); List players = query.list(); for (Iterator iterator = players.iterator(); iterator.hasNext();){ Player player = (Player) iterator.next(); System.out.print(" Location: " + player.getLocation()); System.out.println(" Wealth: " + player.getWealth()); } tx.commit(); }catch (HibernateException e) { if (tx!=null) tx.rollback(); e.printStackTrace(); }finally { session.close(); } } </code></pre> <p>My mySQL database has a Table named Player, with pID, player_name, location, wealth as column values</p> <p>I am attempting to pass playername to the loadPlayer(String username) method so it can look up all information associated with the player and write back to socket. What am I doing wrong here?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload