字符串匹配在與數據庫連接時以下代碼中沒有正確完成。兩個值都是相同的,但相應的操作沒有執行。任何人都可以幫我解決這個問題嗎?提前致謝!java中的字符串匹配操作
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();
String 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);
}
});
}
}
感謝您的回覆friends..the在上面的代碼確切的問題是,我們必須削減ñ比較,否則應包括代碼「jf1.setText(NULL);」在構造函數。
您不應該使用'=='比較字符串,而是使用'.equals(..)'方法 –