iNko Posted December 1, 2012 Share Posted December 1, 2012 Hey, i need to take just one element from my database table. I have this table - Users. and it has ID, Login, Password and Surname. I want to take just surname from this table. Something like ("Select Surname from Users where login =") Im trying to use this, but it gives me errors saying this ("Unkown column 'Admin' in 'where clause'"): private void whoslogedinLabel(){ String sql = "select Surname from Users where login = Admin"; try { pst = conn.prepareStatement(sql); rs = pst.executeQuery(); logininfoLabel.setText(sql); } catch (Exception e){ JOptionPane.showMessageDialog(null, e); } } logininfoLabel - is the label where i want the Surname to be displayed Quote Link to comment https://forums.phpfreaks.com/topic/271438-how-do-i-take-just-1-value-from-database-and-put-it-in-a-label/ Share on other sites More sharing options...
Christian F. Posted December 1, 2012 Share Posted December 1, 2012 Strings in SQL queries need to be surrounded by quotes, so add a pair of single qoutes around the text "admin". Quote Link to comment https://forums.phpfreaks.com/topic/271438-how-do-i-take-just-1-value-from-database-and-put-it-in-a-label/#findComment-1396640 Share on other sites More sharing options...
iNko Posted December 1, 2012 Author Share Posted December 1, 2012 no longer getting error, but instead of the Surname it just writes the string.. ("select Surname from Users where login = 'Admin'") Quote Link to comment https://forums.phpfreaks.com/topic/271438-how-do-i-take-just-1-value-from-database-and-put-it-in-a-label/#findComment-1396644 Share on other sites More sharing options...
DavidAM Posted December 1, 2012 Share Posted December 1, 2012 logininfoLabel.setText(sql); That statement is putting the SQL statement into the label. You need to retrieve the value you selected from the results of the query, which is in rs Quote Link to comment https://forums.phpfreaks.com/topic/271438-how-do-i-take-just-1-value-from-database-and-put-it-in-a-label/#findComment-1396661 Share on other sites More sharing options...
iNko Posted December 1, 2012 Author Share Posted December 1, 2012 made it work: private void whoslogedinLabel(){ try { String sql = "SELECT vardas_pavarde FROM Vartotojas WHERE login='" + whosloggedin + "'"; pst = conn.prepareStatement(sql); //pst.setString(1, whosloggedin); rs = pst.executeQuery(sql); while (rs.next()) { logininfoLabel.setText(rs.getString("vardas_pavarde")); } } catch (Exception e){ JOptionPane.showMessageDialog(null, e); } } thx guys Quote Link to comment https://forums.phpfreaks.com/topic/271438-how-do-i-take-just-1-value-from-database-and-put-it-in-a-label/#findComment-1396663 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.