2017-05-02 23 views
-2

我对登录页面有以下代码。如何测试我的SQL服务器连接?

我创建了以下获得在接收凭证框中输入的数据。

final String NameHere = HeldUser.getText().toString(); 
    final String PasswordHere = HeldPasssword.getText().toString(); 

以下是我的查询,它们与我的数据库中的数据进行比较。

ArryList<String> names= null; 
ArrayList<String> password = null; 
try{ 
con = MyConnectionClass(); 
String query = null; 
if(con == null){ 
z = "Check Your Internet Access!"; 
       // I have a toast RIGHT HERE to that I figured would trigger 
        the connection was not successful.. 
       Toast.makeText(Async.this, "Connection failures. Contact Administrator.", Toast.LENGTH_SHORT).show(); 

} else { 
if (Condition that I know is being triggered){ 
MyQueryDeclaredEarlier = "select * from MyTable where Username = '"+NameHere+ "'and Password = '"+PasswordHere+"'"; 
//  The above was written with the +NameHere+ and +PasswordHere+ variables 
     receive String from EditTextBoxes I have for gathering credentials 

//  The Columns (Username and Password) are correct and the tablename (Table) 
     is also, as is, in the SQL management database. (I exluded dbo.) 

Statement stmt = con.createStatement(); 
ResultSet rs = stmt.executeQuery(MyQueryDeclaredEarlier); 

while (rs.next()){ 
names.add(0,rs.getString("Username")); 
password.add(0,rs.getString("Password")); 
} 
// then I just compare the username variable (NameHere) contents to 
// the string I got from the ResultSet (rs) and added to the ListArray (names) 
at position 0. 
// I think this where the problem is. 
String FromQuery = names.get(0).toString(); 
if(NameHere.equals(FromQuery)){ 
z = "query successful" 
isSuccess = true; 
} 
else{ 
z = "Invalid Query!"; 
isSuccess = false; 
} 

伊马漏下的其余部分'}的

它使返回isSuccess为假。

是他们的任何简单的方法来测试实际的连接? 我有两个模块附加到这个项目。包括适当的依赖关系,但我不确定他们是否没有发生任何其他事情,我无法看到或Android无法为我读取。帮帮我。

+1

很明显,'NameHere.equals(FromQuery)'不评估为真。为什么不对它进行调试以查看你回来的价值?或者,是否有可能做任何日志记录? – Greg

+0

我实际上想出了一些不同的东西。欣赏建议虽然!截至目前,我正在努力让sql驱动程序集成。可能要发布一个新问题。打了一下,看看我能得到什么。谢谢! –

+1

请不要用“FIXED !!!!”回答你的问题。相反,制定一个很好的答案来解释你解决这个问题的步骤,以便将来的读者可以学习某些东西/得到他们正在寻找的帮助。 =) – Seth

回答

2

固定。我创建了一个Array列表并在开始时对其进行了最终确定。而不是玩布尔值(这是实际的问题),我验证了在从DoinBAckground方法传递的字符串postExecute。不过一直没能确认字符串变量,从文本框和字符串采用的字符是否从查询匹配聚集,因为我在与mysql-connector-java-3.0.17-ga-bin.java驱动程序的问题。作为一个模块,但似乎没有工作。付诸库和设置依赖性,编译文件:库/驱动程序名,而不是使用编译项目:驱动程序名

不知道这是否会工作,但我需要尽快测试。如果我需要更多的帮助,我会提出一个新的问题,但我想我可以单独获得这个部分。