Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default [SOLVED]help please! The resultset seems return the same, i need to refresh it

    Hi guys, help me please, the resultset seems return the same as the first resultset. This is a log in form that is connected to a mysql database.
    Database contained(username field): 1,2,3,4...10

    When I entered a number within 1 to 10 it should output "Found"
    When I entered a number that is not within 1 to 10 it should output "Not Found"

    Heres my code:
    Java Code:
        //code for package database
    	public void setLogInMember(String cmboxSql){
    		String mySql = cmboxSql;
    		setLogInMemberList(mySql);		
    	}
    	
    	private void setLogInMemberList(String mySql){
    		try{
    		    statement = con.createStatement();
    		    resultSet = statement.executeQuery(mySql);
    		    
    		    	if(resultSet!=null){
    		    		while(resultSet.next()){
    				    	username = resultSet.getString(1);//return the username if there is
    		    		}
    		    	}		    			    	
    			}catch(SQLException sqlex){
    				username = "";
    		}
    			finally{
    				try {
    					resultSet.close();
    					statement.close();
    				} catch (SQLException e) {
    					e.printStackTrace();
    				}
    			}
    	}
    	
    	public String getUsername(){
    		return username;
    	}
    
    	private Statement statement;
    	ResultSet resultSet;
    	private String username;
    	private String password;
    
    
    //code for package gui
    btnEnter.addActionListener(
    				new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						String username = "SELECT * FROM cooperativeDb WHERE aMeterNo = " +txtUserName.getText();
    						
    						try{
    							dbLogMem.setLogInMember(username);	
    							if(dbLogMem.getUsername().equals("")){
    								System.out.println("not found");
    							}else{
    								System.out.println("found");
    							}
    							
    						}catch(Exception e1){System.out.println("not found");}
    
    					}
    				}
    		);
    When I entered 5 at first run: "Found"
    When I entered another number this time I entered 15: "Found" //this is should "Not Found"
    I stop run program.

    When I entered 15 at first run: "Not Found"
    When I entered another number this time I entered 1: "Not Found" //this is should "Found"
    I stop run program.

    Same problem as before, it always display the result of the first entered username...
    I'd already use PreparedStatement and Statement

    Please help me guys whats wrong with my code
    Last edited by RichersooN; 04-20-2011 at 06:07 PM. Reason: typo error

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    1.) Use code tags when posting code.
    2.) Use PreparedStatement for setting query parameters and executing queries.
    3.) Put ex.printStackTrace() as the first line of ALL your catch blocks for now. This will make you see what exceptions (if any) are being thrown.
    4.) Don't mix your interface code with your data access code. Put all the DB code in a separate DAO class and test it independently of your interface code.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Please edit and repaste your code using the "Code" tags so the code is formatted and readable.

    When you catch an exception, then print out the exception, not our own message.

    Use PreparedStatements when building your SQL to help prevent SQL syntax errors.

    Edit: I see I'm a little slow with my suggestions...

  4. #4
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    I just post here the most significant piece of code with nice comment... so please be try to understand the code... I need your help.. ^_^

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by RichersooN View Post
    I just post here the most significant piece of code with nice comment... so please be try to understand the code... I need your help.. ^_^
    What does this reply have to do with the suggestions that you have been given?
    Did you read the replies that you got? Do you think those suggestions are useless?

  6. #6
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    Yes thanks to their reply, I already tried their suggestions but it seems that the problem is not solved. So I mean beg for more reply..

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What did you do and what happened?

  8. #8
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    When I entered 5 at first run: "Found"
    When I entered another number this time I entered 15: "Found" //this is should "Not Found"
    I stop run program.

    When I entered 15 at first run: "Not Found"
    When I entered another number this time I entered 1: "Not Found" //this is should "Found"
    I stop run program.

    Same problem as before, it always display the result of the first entered username...
    I'd already use PreparedStatement and Statement
    Last edited by RichersooN; 04-20-2011 at 07:19 AM. Reason: typo error

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Read the replies again and implement ALL the suggestions.

  10. #10
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    I read all of those, my theory is when we want to query data that is not on the database, the java will return the previous resultset.. How can i deal with this?

  11. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Please implement suggestions (3) and (4) in the first reply.

  12. #12
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    Yes I already implements all of those including 3 and 4.
    I use a package for gui and package for database so that it is organized.

    Heres the actually testing(output):
    //first run
    found
    SELECT * FROM cooperativeDb WHERE aMeterNo = 5
    found
    SELECT * FROM cooperativeDb WHERE aMeterNo = 15


    //second run
    not found
    SELECT * FROM cooperativeDb WHERE aMeterNo = 15
    found
    SELECT * FROM cooperativeDb WHERE aMeterNo = 5
    found
    SELECT * FROM cooperativeDb WHERE aMeterNo = 15

  13. #13
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Suggestion 4 means create a class
    Java Code:
    class CooperativeDAO {
         public static boolean exists(int aMeterNo ) {
            //some good stuff goes here
            return rs.next();
         }
        
    }

    Then test your DB class on it's own. If it gives you expected results then you are happy and you don't touch the DB code again.

    Your interface code would then simply call
    Java Code:
    CooperativeDAO.exists(txtUserName.getText());
    Last edited by r035198x; 04-20-2011 at 08:40 AM.

  14. #14
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    I know this is for beginner question, but may i know for what is static ? I mean the "static" in the public static

  15. #15
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Marks the method as a utility method. No special conditions are required in order to call it except that the class CooperativeDAO be visible to the caller. The method does not represent an action belonging to the object modelled by the class containing that method.

    Without the static, one would need an instance of a CooperativeDAO object to call it, as in

    Java Code:
    new CooperativeDAO().exists(...)
    Generally, you should not make your methods static as much as possible.

    An easier example to understand is given here:
    Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  16. #16
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    Java Code:
    Code:
    class CooperativeDAO {
         public static boolean exists(int aMeterNo ) {
            //some good stuff goes here
            return rs.next();
         }
        
    }
    Unfornately what you told me to do is also still not solving my problem. so sad

  17. #17
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    I have my plan b, I will fetch all the meterNo//from 1 to 15
    then store it in Vector, then loop thru each element of Vector to compare to the txtUsername.getText();

    Do you have any suggestion on how can I do these? please

  18. #18
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Unfornately what you told me to do is also still not solving my problem. so sad
    Did you fill the part I marked as //good stuff with ... the good stuff?
    I have my plan b, I will fetch all the meterNo//from 1 to 15
    then store it in Vector, then loop thru each element of Vector to compare to the txtUsername.getText();
    If you need a List use ArrayList not vector.
    Your plan B only works if no changes are made in the DB that affect the operation. Otherwise you will be comparing with stale data.
    Write your data access class and make sure that it works first before trying to integrate it with a gui.

  19. #19
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default


    Quote:
    Unfornately what you told me to do is also still not solving my problem. so sad
    Did you fill the part I marked as //good stuff with ... the good stuff?
    Quote:
    I have my plan b, I will fetch all the meterNo//from 1 to 15
    then store it in Vector, then loop thru each element of Vector to compare to the txtUsername.getText();
    If you need a List use ArrayList not vector.
    Your plan B only works if no changes are made in the DB that affect the operation. Otherwise you will be comparing with stale data.
    Write your data access class and make sure that it works first before trying to integrate it with a gui.
    May I know why arraylist not vector? cause I googled about java with database and they store all the .getString() into a vector

  20. #20
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    So far so good with my plan b. I can now compare all the meterNo that is exist to the meterNo that is not existed.. just a little bit this case will be closed

Page 1 of 2 12 LastLast

Similar Threads

  1. ResultSet GUI
    By razmyasdfg in forum JDBC
    Replies: 4
    Last Post: 03-15-2011, 01:51 PM
  2. How do I put a ResultSet in an Object?
    By Azuxard in forum JDBC
    Replies: 2
    Last Post: 03-31-2009, 11:40 PM
  3. Help with Resultset
    By xxAlemanxx in forum JDBC
    Replies: 6
    Last Post: 06-24-2008, 11:09 AM
  4. ResultSet to XML
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-14-2008, 09:50 AM
  5. ResultSet example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 08:59 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •