日期:2014-05-18  浏览次数:20582 次

数据库连接!!!!!!!!
大家帮我看看我这里有什么问题?
已经可以通过编译,实行也没问题,我实验过也能从数据库中调出数据,但是我要把String code作为参数和在数据库中调出的thiscode做比较用于判定是否一致,决定返回true还是false,但是一直返回false,(我明明给它正确的值)我也不知道问题出在哪里?大家帮我看看好吗?谢谢!

public Boolean test(String name,String code) throws SQLException
{
Connection con = null;
ResultSet rs=null;
Statement stmt;

String thiscode=null;
String where="where name=";
String sql="select code from client_code "+where+"\""+name+"\"";

try{
Class.forName("com.mysql.jdbc.Driver");
//java.sql.DriverManager.registerDriver(new com.mysql.jdbc.Driver());
String dbUrl="jdbc:mysql://localhost:3306/bookstore";
String dbUser="root";
String dbPwd="yidaimi";
con=java.sql.DriverManager.getConnection(dbUrl,dbUser,dbPwd);
con.setAutoCommit(false);
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
con.commit();
}catch(Exception e){
e.printStackTrace();
try{
con.rollback();
}catch(Exception roolback){
roolback.printStackTrace();
}

return null;
}
while(rs.next()){
thiscode=rs.getString("code"); //到这部为止我实验过,可以正确的从数据库调用数据
}
if(thiscode==code){
return true;
}else{
return false;

}

}

------解决方案--------------------
你那块代码
if(thiscode==code){ 
return true; 
}else{ 
return false; 

} 有问题,code应该是String类型的吧,你应该用thiscode.equals(code)来判断啊
如下:
if(thiscode.equals(code)){ 
return true; 
}else{ 
return false;