开发者

string matching operation in java

开发者 https://www.devze.com 2023-03-26 08:34 出处:网络
string matching not being done properly in the code below when connected with database.Both values are same but corresponding operations are n0t performed.Can anyone help me rectifying this? Thanks in

string matching not being done properly in the code below when connected with database.Both values are same but corresponding operations are n0t performed.Can anyone help me rectifying this? Thanks in advance!

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.JFrame;
public class NewJFramefin extends javax.swing.JFrame implements ActionListener
{  

public NewJFramefin() 
{
    initComponents();
    add(jl1);
    add(jf1);
    jb1.setActionCommand("OK");
    jb1.addActionListener(this);
    add(jb1);
    jb2.setActionCommand("CANCEL");
    jb2.addActionListener(this);
    add(jb2);
    jb3.addActionListener(this);
    add(jb3);
}

public void closewindow() 
{
   System.exit(1);

}
public void actionPerformed(ActionEvent e)
{
      String find=jf1.getText();
      String ev=e.getActionCommand();
      St开发者_高级运维ring check;
      String str="jdbc:odbc:dsn1";
      try
      {
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          Connection con=DriverManager.getConnection(str);
          Statement s=con.createStatement();
          s.execute("select * from Table1");
          ResultSet res=s.getResultSet();
          if(res!=null)
          {
              while(res.next())
              {
                  check=res.getString(1);
                  System.out.println("STRING FROM DB:"+check + find);
                  if(check==find)
                  {
                       System.out.println("MEANING:"+res.getString(2));
                  }
                  if(ev.equalsIgnoreCase("OK") &&(find.equalsIgnoreCase(check)))
                  {
                      jf2.setText(res.getString(2));
                      add(jf2);

                  }
                  else if(ev.equalsIgnoreCase("CANCEL"))
                  {
                      jf2.setText(" cancelled    ");
                      add(jf2);
                      jf1.setText("    ");
                      add(jf1);
                  }
                  else if(ev.equalsIgnoreCase("EXIT"))
                  {

                      closewindow();
                  }

               }
          }


      }           
      catch(Exception ew)
      {

      }
}


public static void main(String args[]) 
{
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFramefin().setVisible(true);
        }
    });
}
}

thanks for your reply friends..the exact problem in the above code is that we have to trim n compare ,else should include the code "jf1.setText(null); " in the constructor.


Problem is probably

if(check==find)

should be

if(check.equals(find))

== doesn't do string comparison in Java; it checks if objects are identical (the same object)


Never compare String instances with ==. Use string1.equals(String2). == checks if both variables hold the same reference, not if both strings contain the same characters.


The operation A == B is to identify that two object references are both pointing to the same object.

The operation A.equals(B) is to identify that two object references both contain the same value.

Since your strings are constructed independently (one is constructed in the code you write, the other is constructed in the JDBC layer) they cannot be referencing identical objects, you need to use the equals operator.


As Hunter McMillen said, you shouldn't use == for comparing Strings. Using == on two Strings will return true only if they are the same String object. The equals() method on the other hand will return true if the two Strings have the same contents.

0

精彩评论

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

关注公众号