Results 1 to 7 of 7
  1. #1
    cotarelo is offline Member
    Join Date
    Mar 2010
    Posts
    43
    Rep Power
    0

    Default compare sql resultset with a String

    Hi,

    I want to compare the resultset obtained by a sql query with a string. So what I want is when the word queried is inside the database, do a set of actions...

    Java Code:
    public void run(){
    		try{
    			input = new BufferedReader(new InputStreamReader(s.getInputStream()));
                            String url = "jdbc:mysql://localhost/mydatabase";
                            Connection con = DriverManager.getConnection(url, "user", "password");
                               
                            
    			while (true){
    				String str = input.readLine();
    				System.out.println("Receiving: " + str);
                                    System.out.println("Checking if the word received it is on the database");
                                    Statement select2 = con.createStatement();
                                    ResultSet actualsituation = select2.executeQuery("SELECT ifx FROM table WHERE ifx='"+str+"';");
    
    
                                    if (actualsituation.equals(str)){
                                        System.out.println("Lets retreive the whole file");
    ...
    ...
    But in the line
    Java Code:
                                    if (actualsituation.equals(str)){
    I can't compare those 2 variables... how to compare a string with the values received from the database? (resultset object)

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    10

    Default

    You have to open the resultSet or check whether it is not empty:
    Java Code:
     ResultSet actualsituation = select2.executeQuery("SELECT ifx FROM table WHERE ifx='"+str+"';");
    if (actualsituation.next()){ // rs.next() is false if nothing is found
        System.out.println("Lets retreive the whole file");
    And you should use preparedStatements to avoid SQL injection, read this tutorial: http://java.sun.com/docs/books/tutorial/jdbc/index.html
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    cotarelo is offline Member
    Join Date
    Mar 2010
    Posts
    43
    Rep Power
    0

    Default

    Thanks!

    Now I am thinking that I am doing it too complicated... is there any easy way to query a mysql database and receive true if the string is inside the database and false if it is not?

    Thanks again!

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    10

    Default

    You're welcome. No that is the easy way. ;-)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    cotarelo is offline Member
    Join Date
    Mar 2010
    Posts
    43
    Rep Power
    0

    Default

    Right! is the easy way!

    so exactly, if the query is true, it will go on the if bucle, if not it will jump it :)

    I did not understand it at the beggining.

    PS:Now it works but I have another problem, so I will open a new thread :)

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    10

    Default

    If it's about databases use the JDBC forum.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    cotarelo is offline Member
    Join Date
    Mar 2010
    Posts
    43
    Rep Power
    0

    Default

    Well I am not sure... I think it is related to sockets or databases, I dont know...

    The thing is that the string str is retreived from a socket, and, a few seconds after retreiving the values with jdbc and storing them, in the console I receive a infinite loop that looks like this:

    Java Code:
    Receiving: null
    Checking if the word received it is on the database
    Receiving: null
    Checking if the word received it is on the database
    Receiving: null
    Checking if the word received it is on the database
    Receiving: null
    Checking if the word received it is on the database
    Receiving: null
    Checking if the word received it is on the database
    And so on

Similar Threads

  1. Replies: 8
    Last Post: 02-22-2014, 09:06 AM
  2. How compare two String?
    By artemff in forum New To Java
    Replies: 8
    Last Post: 08-29-2011, 09:00 AM
  3. String compare issues?
    By emp in forum New To Java
    Replies: 3
    Last Post: 04-21-2009, 08:16 AM
  4. Compare 2 XML
    By Peter in forum XML
    Replies: 1
    Last Post: 07-05-2007, 03:58 AM
  5. String Compare not working
    By Revelation in forum New To Java
    Replies: 3
    Last Post: 06-30-2007, 07:43 PM

Posting Permissions

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