如何测试SQL Server连接?

时间:2017-05-02 02:51:14

标签: java sql-server android-studio jdbc azure-sql-database

我有一个登录页面的代码。

我创建了以下内容以获取在接收凭据的框中输入的数据。

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; 
} 

Imma遗漏了'}的剩余部分

它将isSuccess返回为false。

他们是否可以轻松测试实际连接? 我有两个模块附加到这个项目。包括适当的依赖项,但我不确定他们是否没有其他任何我无法看到或Android无法读取的内容。救命。

1 个答案:

答案 0 :(得分:2)

固定。我创建了一个列表数组,并在开始时完成它。我没有使用布尔值(这是实际问题),而是使用从DoinBAckground方法传递的String在postExecute中验证。仍然无法确认从文本框中获取字符的字符串变量和从查询中收集的字符串是否匹配,因为我遇到了mysql-connector-java-3.0.17-ga-bin.java的问题驱动程序。作为一个模块,但似乎没有工作。将它放入库并将依赖项设置为编译文件:libs / drivername而不是使用编译项目:drivername

不确定这是否有效,但我需要尽快进行测试。如果我需要更多帮助,我会提出一个新问题,但我想我可以独自完成这一部分。