java与数据库数据匹配

来源:百度知道 编辑:UC知道 时间:2024/06/18 12:20:31
在从数据库中取出数据与键盘输入时总是不相等是什么事?
程序代码如下:
String str2,comp,sql;
ResultSet result;
str2=jtfd2.getText().trim(); //从键盘输入的数据
try{
sql="use cy select * from stu where password="+
"'"+str2+"'";
result=stmt.executeQuery(sql);
if(result.next()){
comp=result.getString("password"); //数据库中的数据
System.out.println(comp);
if(str2.equals(comp)) //若相等弹出相等对话框
JOptionPane.showMessageDialog(frame,"equal");
else //不等弹出不等对话框
JOptionPane.showMessageDialog(frame,"not equal");
}
else JOptionPane.showMessageDialog(frame,"not found");
但输入一样的总弹出不等对话框,请高手指教!

把这句换成这个
comp=result.getString("password").trim();

sql="use cy select * from stu where password="+
"'"+str2+"'"; 为什么要加use cy?

if(str2.equals(comp.trim())) 这样试试吧