JDBC SQL : if statement string == char.toString not working. :)
How come my if statement if (tablename == Character.toString('S'))
is not registering as true? Both print out the be S... Is there a different why I can implement this? I could do a != but I need to add more arguments to my if statement, and my tablename needs to开发者_开发百科 stay a string.
System.out.println("Enter table:");
String line = input.readLine();
StringTokenizer tk = new StringTokenizer(line);
String tablename = tk.nextToken();
DatabaseMetaData d = conn.getMetaData();
ResultSet rm = d.getColumns(null, null, tablename, null);
System.out.println(tablename);
System.out.println(Character.toString('S');
System.out.println(tablename == Character.toString('S');
if (tablename == Character.toString('S')){
System.out.println("Woot!");
}
OUTPUT: Enter table: S S S false
Since you're comparing both objects for equality, you need to use:
if (tablename.equals(Character.toString('S'))) {
...
Here's a nice reference on equality comparison in java:
http://leepoint.net/notes-java/data/expressions/22compareobjects.html
The == only relates to the equality of the pointer that Character.toString() returns. Need to use the below.
tablename.equals(Character.toString('s'))
精彩评论