开发者

JDBC SQL : if statement string == char.toString not working. :)

开发者 https://www.devze.com 2023-02-21 02:35 出处:网络
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

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'))
0

精彩评论

暂无评论...
验证码 换一张
取 消