比较java和sql的字符串 [英] compare string from java and sql

查看:142
本文介绍了比较java和sql的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我仍然得到输出否,即使a等于k?



Why I still get an output "No" even though a is equal to k?

public void CompareType(String a ) throws Exception {
		// TODO Auto-generated method stub
		String k;
		String sql="Select Type from menu ";
		DatabaseConnection db = new DatabaseConnection();
		Connection  conn =db.getConnection();
		PreparedStatement  ps = conn.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) 
        {
			k=rs.getString("Type");
		if(a==k)
		{
			System.out.println("Yes");
		}
		else
		{
			System.out.println("No");
        }
        }
		ps.close();
		rs.close();
		conn.close();
	}

推荐答案

这是一个众所周知的Java 怪异'功能',试试吧,例如

That is a well-know Java weirdness 'feature', try, for instance
public class Str
{
  public static void main(String args[])
  {
    String a = "foo";
    String b = "fo";

    b += "o";

    if ( a == b)
    {
      System.out.println("the strings are ==");
    }
    if ( a.equals(b))
    {
      System.out.println("the strings are equal(s)");
    }
  }
}





如果你想(你通常想要)比较 content 然后使用等于方法。


这篇关于比较java和sql的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆