5 Replies - 245 Views - Last Post: 02 August 2013 - 04:09 PM Rate Topic: -----

#1 yiann_is  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 25-December 12

Retreive data from a database, java

Posted 01 August 2013 - 02:05 PM

I have created a database, till now everything seems to work fine. The only problem is that i can t retrieve the data from a column and pass them to a method.I can print them but i can t pass them.
public  String getUser(String username){
		try{
			
			String query= "select * from userdata";
			rs = st.executeQuery(query);

			while (rs.next()){
				username=rs.getString("username");
				System.out.println(username);				
			}
			
		}catch(Exception ex){
			System.out.println(ex);
		}
		return username;
		
	}


Is This A Good Question/Topic? 0
  • +

Replies To: Retreive data from a database, java

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,672
  • Joined: 20-September 08

Re: Retreive data from a database, java

Posted 01 August 2013 - 02:34 PM

Try
http://technojeeves....-to-nested-list

In your case, nesting might not be necessary, so adapt

This post has been edited by g00se: 01 August 2013 - 02:34 PM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#3 yiann_is  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 25-December 12

Re: Retreive data from a database, java

Posted 02 August 2013 - 12:23 PM

View Postg00se, on 01 August 2013 - 02:34 PM, said:

Try
http://technojeeves....-to-nested-list

In your case, nesting might not be necessary, so adapt


i can't adapt it, or i can't understand what and how it works. Is there another way to compare the stored data (registered username)with the input data (login)
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,672
  • Joined: 20-September 08

Re: Retreive data from a database, java

Posted 02 August 2013 - 12:32 PM

Quote

Is there another way to compare the stored data (registered username)with the input data (login)
Is that your use case? If so, why are you querying for every username instead of just the one entered?
Was This Post Helpful? 1
  • +
  • -

#5 yiann_is  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 25-December 12

Re: Retreive data from a database, java

Posted 02 August 2013 - 01:13 PM

View Postg00se, on 02 August 2013 - 12:32 PM, said:

Quote

Is there another way to compare the stored data (registered username)with the input data (login)
Is that your use case? If so, why are you querying for every username instead of just the one entered?


you are right!i did like you said. Now i want to get the pass from the specific user and compare it. I thought to write it like that, but i get an error Illegal operation on empty result set
if(puname.equals(username)) {
					System.out.println(puname);
					String queryPass= "select password from userdata where username='puname'";
					rs2 = st.executeQuery(queryPass);
					String passdb=rs2.getString("password");
					System.out.println("pass"+passdb);

Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,672
  • Joined: 20-September 08

Re: Retreive data from a database, java

Posted 02 August 2013 - 04:09 PM

Quote

String queryPass= "select password from userdata where username='puname'";
Hard-coding it like that makes no sense. You need to use the username to be tested as a parameter. Use PreparedStatement
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1