我想從數據庫中獲取一些值,但當單擊按鈕(void ButtonClick)時,我的應用程序崩潰。嘗試使用JDBC連接到數據庫時應用程序崩潰
這是我的代碼:
public void ButtonClick() throws Exception {
getConnection();
}
public Connection getConnection() throws Exception {
try {
String username = "*******";
String password = "*******";
String url = "jdbc:mysql://http://**.***.***.***:3306/UserDB";
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = (Connection) DriverManager.getConnection(url,username,password);
Statement statement = (Statement) conn.createStatement();
String query = "SELECT * FROM TABLE 'UserDB'";
ResultSet result = statement.executeQuery(query);
while (result.next()) {
String name = result.getString("Username");
int id = result.getInt("ID");
int points = result.getInt("Points");
Toast.makeText(this, name + " " + id + " " + points, Toast.LENGTH_SHORT).show();
}
return conn;
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
return null;
}
(我不知道有什麼錯誤,因爲我的AVD不工作)
感謝您的幫助!
什麼是錯誤? –
請在此處添加代碼,而不是粘貼外部鏈接 –
Alex Mamo,我不知道錯誤是什麼。我的AVD不工作。 –