2015-07-10 58 views
0

幾天前我開始開發一個Android應用程序,我決定使用JDBC進行數據庫連接(數據庫在雲端),我知道使用php webservice更安全。這裏是我的問題:有時JDBC與Android連接不起作用

直到昨天我才得以建立連接並獲得查詢結果,並登錄到應用程序,但不知何故我不能夠現在就這樣做。我使用谷歌Cloud SQL的我的數據庫,一切都看起來不錯,我可以通過MySQL工作臺連接到我的數據庫,並執行相同的查詢。

有趣的是,當我嘗試登錄到應用程序使用了錯誤的密碼,我得到的登錄失敗的錯誤,這意味着我得到的結果從數據庫,但使用正確的密碼,我得到什麼這是奇怪的。

誰能猜出可能是錯的?

這裏是我的數據庫連接類

public class DBTransactions { 
// JDBC driver name and database URL 
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
static final String DB_URL = "jdbc:mysql://***:3306/"; 

// Database credentials 
static final String DB_Name = "dbname"; 
static final String USER = "user"; 
static final String PASS = "pass"; 

public static Nurse NurseLogin(String username, String pass) { 
    Connection conn = null; 
    Statement stmt = null; 
    Nurse nurse= new Nurse(); 
    try {//STEP 2: Register JDBC driver 
     Class.forName("com.mysql.jdbc.Driver"); 

     //STEP 3: Open a connection 
     System.out.println("Connecting to database..."); 
     conn = DriverManager.getConnection(DB_URL + DB_Name, USER, PASS); 

     //STEP 4: Execute a query 
     System.out.println("Creating statement..."); 
     stmt = conn.createStatement(); 
     String sql; 
     sql = "SELECT * FROM falldb.nurse WHERE username='" + username + "' AND pass='" + pass + "'"; 
     ResultSet rs = stmt.executeQuery(sql); 
     System.out.println("Query!!"); 

     //STEP 5: Extract data from result set 
     while (rs.next()) { 
      System.out.println("Data exist!!"); 
      //Retrieve by column name 
      nurse.setID(rs.getInt("idNurse")); 
      nurse.setName(rs.getString("name")); 
      nurse.setSurname(rs.getString("surname")); 
      nurse.setUsername(rs.getString("username")); 
      nurse.setPass(rs.getString("pass")); 

      //Display values 
      System.out.print("ID: " + nurse.getID()); 
      System.out.print(", First: " + nurse.getName()); 
      System.out.println(", Last: " + nurse.getSurname()); 
      System.out.println("Finished!!"); 
     } 
     //STEP 6: Clean-up environment 
     rs.close(); 
     stmt.close(); 
     conn.close(); 
    } catch (SQLException se) {//Handle errors for JDBC 
     se.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     //finally block used to close resources 
     try { 
      if (stmt != null) 
       stmt.close(); 
     } catch (SQLException se2) { 
     } 
     try { 
      if (conn != null) 
       conn.close(); 
     } catch (SQLException se) { 
      se.printStackTrace(); 
     }//end finally try 
    }//end try 
    return nurse; 
} 
} 

,這是Login.class在那裏我打電話DBTransactions.NurseLogin

public class Login extends Activity { 

Button btnLogin; 
EditText etUser; 
EditText etPass; 
TextView tvResult; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login); 

    btnLogin = (Button) findViewById(R.id.btnLogin); 
    btnLogin.setOnClickListener(clickLogin); 

    etUser = (EditText) findViewById(R.id.etUser); 
    etPass = (EditText) findViewById(R.id.etPass); 
    tvResult = (TextView) findViewById(R.id.tvResult); 
} 
public View.OnClickListener clickLogin = new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     final String u = etUser.getText().toString(); 
     final String p = etPass.getText().toString(); 

     new AsyncTask() { 
      @Override 
      protected Object doInBackground(Object[] params) { 
       return DBTransactions.NurseLogin(u, p); 
      } 
      @Override 
      protected void onPostExecute(Object o) { 

       Nurse nurse = (Nurse) o; 
       if (nurse.getName() != null) { 
        tvResult.setText("Welcome " + nurse.getName().toUpperCase()); 

        Intent i = new Intent(Login.this, PatientOperations.class); 
        i.putExtra("objects.Nurse", nurse); 
        startActivity(i); 
       } else { 
        tvResult.setText("Login Failed"); 
       } 
      } 
     }.execute(); 
    } 
}; 
} 
+0

*誰能猜出可能是錯了嗎?* ......冬天正在添加 – Selvin

+0

我們能不能從你的描述猜測你的代碼,並幫助你。發佈一些代碼以顯示您所做的事情或您面臨的問題。 –

+0

在這裏你去@AlokNair –

回答

0

後的嘗試和失敗,我終於發現是什麼原因導致的幾天問題。看來,選擇所有列從數據庫使用SELECT *一切工作正常。但是當試圖使用WHERE子句出錯時。

這工作

sql = "SELECT * FROM falldb.nurse"; 

這不

sql = "SELECT * FROM falldb.nurse WHERE username='" + username + "' AND pass='" + pass + "'"; 

的解決方案是使用WHERE子句時,而不是使用*使用列的確切名稱。我將我的代碼更改爲:

sql = "SELECT idNurse,name,surname,isAdmin FROM falldb.nurse WHERE username='" + username + "' AND pass='" + pass + "'"; 

現在我得到了結果。