Java 尝试使用JDBC连接到DB时应用程序崩溃

Java 尝试使用JDBC连接到DB时应用程序崩溃,java,android,database,android-studio,jdbc,Java,Android,Database,Android Studio,Jdbc,我想从数据库中获取一些值,但当我单击按钮(void ButtonClick)时,我的应用程序崩溃了 这是我的密码: public void ButtonClick() throws Exception { getConnection(); } public Connection getConnection() throws Exception { try { String username = "*******"; String password

我想从数据库中获取一些值,但当我单击按钮(void ButtonClick)时,我的应用程序崩溃了

这是我的密码:

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不工作)


谢谢你的帮助

从Java 7开始,不需要使用
forName()
方法。您正在以这种方式创建一个新实例,同时尝试使用
DriverManager.getConnection()
创建一个连接

为了解决这个问题,只需使用
forName()
方法删除驱动程序的实例化

看到屏幕截图,请注意,您无法从Android本机访问MySQL数据库。实际上,您可能可以使用JDBC,但不推荐使用它。请看这个


希望有帮助。

错误是什么?请在此处添加代码,而不是粘贴外部链接Alex Mamo,我不知道错误是什么。我的AVD坏了,我只是遇到了这个问题。数据库访问的推荐方式显然是位于应用程序和数据库之间的RESTful服务。