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);
}
});
}
}
感谢朋友的回复。上面代码中的确切问题是我们必须修剪 n 比较,否则应该在构造函数中包含代码“jf1.setText(null);”。
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();
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);
}
});
}
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题可能应该
是
== 在 Java 中不进行字符串比较;它检查对象是否相同(同一对象)
Problem is probably
should be
== doesn't do string comparison in Java; it checks if objects are identical (the same object)
切勿将 String 实例与
==
进行比较。使用string1.equals(String2)
。==
检查两个变量是否拥有相同的引用,而不是两个字符串是否包含相同的字符。Never compare String instances with
==
. Usestring1.equals(String2)
.==
checks if both variables hold the same reference, not if both strings contain the same characters.操作
A == B
是为了识别两个对象引用都指向同一个对象。操作
A.equals(B)
用于识别两个对象引用都包含相同的值。由于您的字符串是独立构造的(一个是在您编写的代码中构造的,另一个是在 JDBC 层中构造的),它们不能引用相同的对象,因此您需要使用 equals 运算符。
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.
正如 Hunter McMillen 所说,您不应该使用
==
来比较字符串。仅当两个 String 是同一个 String 对象时,使用==
才会返回 true。另一方面,如果两个字符串具有相同的内容,则 equals() 方法将返回 true。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. Theequals()
method on the other hand will return true if the two Strings have the same contents.