无法连接到SQL数据库(Java,Android Studio)

时间:2019-01-27 23:23:29

标签: java android sql android-studio jdbc

我已经导入了JDBC库,将其设置为可编译,并授予了App访问Internet的权限。 当按下“屏幕”上的按钮时,它应该连接到数据库并为我提供要在列表中实现的值。

当我按下按钮时,可以看到它显示了“正在连接数据库”文本,但是大约半秒钟后,它显示了最后一个catch块中定义的“异常”。

我注释掉了onPostExecute中的三行代码,因为当我保留它们时,应用程序将崩溃。

public void retrieveData(View view) {
    GetData retrieveData = new GetData();
    retrieveData.execute();


}

private class GetData extends AsyncTask<String,String,String> {
    String msg = "";

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";

    static final String DB_URL = "jdbc:mysql://" + DbStrings.DATABASE_URL + "/" + DbStrings.DATABASE_NAME;

    @Override
    protected void onPreExecute() {
        progress.setText("Connecting to database");
    }

    @Override
    protected String doInBackground(String... strings) {

        Connection conn = null;
        Statement stmt = null;

        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, DbStrings.USERNAME, DbStrings.PASSWORD);

            stmt = conn.createStatement();
            String sql = "SELECT * FROM video"; //
            ResultSet rs = stmt.executeQuery(sql);

            while(rs.next()) {
                String name = rs.getString("title");

                buildNames += name + " ### ";

            }

            msg = "Process complete.";

            rs.close();
            conn.close();
            stmt.close();


        } catch(SQLException connError) {
            msg = "An exception was thrown for JDBC.";
            connError.printStackTrace();
        } catch (ClassNotFoundException e) {
            msg = "A Class not found exception was thrown.";
            e.printStackTrace();
        } catch (java.sql.SQLException e) {
            msg = "Exception";
            e.printStackTrace();
        } finally {

            try{
                if(stmt != null) {
                    stmt.close();
                }
            } catch (java.sql.SQLException e) {
                e.printStackTrace();
            }
            try{
                if(conn != null) {
                    conn.close();
                }
            } catch (java.sql.SQLException e) {
                e.printStackTrace();
            }

        }


        return null;
    }

    @Override
    protected void onPostExecute(String msg) {

        progress.setText(this.msg);

        //names = buildNames.substring(0,buildNames.length()-5).split(" ### ");
        //itemAdapter = new ItemAdapter(thisContext, buildNames.substring(0,buildNames.length()-5).split(" ### ")); //crashes
        //namesList.setAdapter(itemAdapter);

    }


}

}

1 个答案:

答案 0 :(得分:0)

检查堆栈跟踪后,我发现了这一点:

with recursive cte as (
      select t.*
      from table_name t
      where t.unique_id = 55544
      union all
      select t.*
      from cte join
           table_name t
           on cte.id = t.reference_id
     )
select *
from cte;

所以我知道要搜索什么; 我正在使用JDBC(3.x)的过时版本。导入5.1.46版后,它现在可以工作了:)。