Error in Assigning a value got from the SQL DB

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mail2sakthi2003
    New Member
    • Sep 2007
    • 5

    #1

    Error in Assigning a value got from the SQL DB

    Hi,
    In this program, i have established the connection and it even works. i get a value from the table and checked it with the given value . if both r same, i have to assign that value to a string. But if i try assigning the value to a string variable, it goes out of the try statement to the catch statement..can u please help me what's wrong and why its not working....than k you.

    i have given the print statement inside the if condition..its working. but the variable assignment is not working.

    Code:
    try
    			{     
    				  login_name=usert.getText().trim();
    				  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
    				  String url = "jdbc:odbc:sakthi"; 
    			      con = (Connection)DriverManager.getConnection(url,"sakthi","");
    			      Statement stmt=con.createStatement();
    			      System.out.println("Connection :"+con);
    			      System.out.println("Connection is created!"+login_name);
    			      String run=("select * from logg");
    			      ResultSet rs=stmt.executeQuery(run);
    			      while(rs.next())						
    			    {   
    			    	 
    			    	 if(rs.getString("user").trim().equalsIgnoreCase(login_name.trim()))
    			    	 {
    			    	 System.out.println("hello");	 
    			    	 //uid=rs.getString("user");
    			    	 // System.out.println(uid);
    			    		 
    			         }
    			    	 else
    			    	 {
    			    		 System.out.println(" bye");
    			    	 }
    			    	
    			  	}con.close();
    			}
    			    catch(Exception s)
    			    {
    			      System.out.println("Doesn't establish the connection!");
    			      System.exit(0);
    			    }
    			
    		}
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by mail2sakthi2003
    Hi,
    In this program, i have established the connection and it even works. i get a value from the table and checked it with the given value . if both r same, i have to assign that value to a string. But if i try assigning the value to a string variable, it goes out of the try statement to the catch statement..can u please help me what's wrong and why its not working....than k you.

    i have given the print statement inside the if condition..its working. but the variable assignment is not working.

    Code:
    try
    			{     
    				  login_name=usert.getText().trim();
    				  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
    				  String url = "jdbc:odbc:sakthi"; 
    			      con = (Connection)DriverManager.getConnection(url,"sakthi","");
    			      Statement stmt=con.createStatement();
    			      System.out.println("Connection :"+con);
    			      System.out.println("Connection is created!"+login_name);
    			      String run=("select * from logg");
    			      ResultSet rs=stmt.executeQuery(run);
    			      while(rs.next())						
    			    {   
    			    	 
    			    	 if(rs.getString("user").trim().equalsIgnoreCase(login_name.trim()))
    			    	 {
    			    	 System.out.println("hello");	 
    			    	 //uid=rs.getString("user");
    			    	 // System.out.println(uid);
    			    		 
    			         }
    			    	 else
    			    	 {
    			    		 System.out.println(" bye");
    			    	 }
    			    	
    			  	}con.close();
    			}
    			    catch(Exception s)
    			    {
    			      System.out.println("Doesn't establish the connection!");
    			      System.exit(0);
    			    }
    			
    		}
    [CODE=java]if(rs.getString ("user").trim() .equalsIgnoreCa se(login_name.t rim()))[/CODE]

    Retrieves data from column user. Then inside the if block you are trying to call rs.getString("u ser") again. That according to the specs may not yield the correct results. Only call getString once for that given column. If you have done s.printStackTra ce(); (you should) in your catch block you will have seen the exception that was thrown because of that.
    And I spent the whole day yesterday pointing that out.

    Comment

    Working...