0

I have imported the JDBC library, set it to be compiled, and gave the App permission to access Internet. When a button on Screen is pressed, it should connect to the database and give me the values to implement in the list.

When I press the button, I can see it giving me the "Connecting to database" text, but after about half a second it says "Exception" as defined in that last catch block.

I commented out the three lines of code in onPostExecute, because when I kept them the App would just crash.

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

    }


}

}

Jatoxo
  • 11
  • 1
  • Use Logcat to examine the stack trace: https://stackoverflow.com/q/23353173/115145. Also, please understand that JDBC is not designed for use from mobile devices, which is one of the reasons why Android does not support JDBC. – CommonsWare Jan 27 '19 at 23:29
  • This is what I found: java.sql.SQLException: Unknown initial character set index '192' received from server. Initial client character set can be forced via the 'characterEncoding' property. I saw this working in a tutorial, so it shouldn't prevent it from working – Jatoxo Jan 27 '19 at 23:37
  • You really should not be connecting directly to a database from an Android app. – Mark Rotteveel Jan 28 '19 at 10:23
  • Why not? Is it unsafe? What should I do instead? – Jatoxo Jan 29 '19 at 15:04

1 Answers1

0

After checking the stack trace I found this:

java.sql.SQLException: Unknown initial character set index '192' received from server.

And so I knew what to search for; I was using an outdated version of JDBC, (3.x). After importing Version 5.1.46, it is now working :).

Jatoxo
  • 11
  • 1