Results 1 to 7 of 7
- 06-21-2010, 03:14 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
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...
But in the lineJava 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"); ... ...I can't compare those 2 variables... how to compare a string with the values received from the database? (resultset object)Java Code:if (actualsituation.equals(str)){
- 06-21-2010, 03:19 PM #2
You have to open the resultSet or check whether it is not empty:
And you should use preparedStatements to avoid SQL injection, read this tutorial: http://java.sun.com/docs/books/tutorial/jdbc/index.htmlJava 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");Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 06-21-2010, 03:32 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
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!
- 06-21-2010, 03:34 PM #4
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
- 06-21-2010, 03:45 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
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 :)
- 06-21-2010, 03:48 PM #6
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
- 06-21-2010, 03:53 PM #7
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
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:
And so onJava 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
Similar Threads
-
How compare two String?
By artemff in forum New To JavaReplies: 8Last Post: 08-29-2011, 08:00 AM -
String compare issues?
By emp in forum New To JavaReplies: 3Last Post: 04-21-2009, 07:16 AM -
Comparing string using == or != (how to compare string in if else)
By fiqueudrue in forum New To JavaReplies: 6Last Post: 02-10-2009, 06:48 AM -
Compare 2 XML
By Peter in forum XMLReplies: 1Last Post: 07-05-2007, 02:58 AM -
String Compare not working
By Revelation in forum New To JavaReplies: 3Last Post: 06-30-2007, 06:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks