ANDROID Database Connection Error

Asked

Viewed 77 times

1

I created a database on localhost, and wanted to pull this database to my Android, created two classes, CONEXAODAO, and Conexaomysql:

public class conexaoDAO {

    public String tabelaDepartamento () {
        String departamentoNome = "";

        conexaoMysqlActivity conexao = new conexaoMysqlActivity();
        conexaoActivity objConexao = new conexaoActivity();

        objConexao.db_connect_string = "localhost";
        objConexao.db_name = "souprogresso";
        objConexao.db_userid = "root";
        objConexao.db_password = "";
        Connection conn = conexao.dbConnect(objConexao);
        if (conn != null) {
            try {
                   Statement statement = conn.createStatement();
                   String queryString = "SELECT nomeDepartamento, idprefeituraDepartamento FROM departamento WHERE idprefeituraDepartamento = '1'";
                   ResultSet rs;
                   rs = statement.executeQuery(queryString);
                   if (rs.next()) {
                       departamentoNome = rs.getString("nomeDepartamento");
                       System.out.println(departamentoNome);
                   }
            } catch (SQLException e) {
                departamentoNome = e.getMessage();
            }
        }
        return departamentoNome;
    }

}


public class conexaoMysqlActivity {
     @SuppressLint("NewApi")
     public Connection dbConnect(conexaoActivity objConexao) {
          StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
          StrictMode.setThreadPolicy(policy);
          Connection conn = null;
          String connectionUrl = null;

          try {
              Class.forName("com.mysql.jdbc.Driver");
              connectionUrl = "jdbc:mysql://" + 
                      objConexao.db_connect_string + ";" +
                      "databaseName=" + objConexao.db_name + ";user=" 
                      +objConexao.db_userid + 
                      ";password=" +  objConexao.db_password + ";";
              conn = DriverManager.getConnection(connectionUrl);

          } catch (SQLException se) {
              se.printStackTrace();
              System.out.println("Não foi possível se conectar com a base de dados");
          } catch (ClassNotFoundException e) {
              e.printStackTrace();
              System.out.println("Erro de Classe SQL");
          } catch (Exception se) {
              se.printStackTrace();
              System.out.println("Erro de Expressão");
          }
          return conn;
     }
}

When I try to rotate the following error happens:

08-14 17:48:30.149: E/AndroidRuntime(674): FATAL EXCEPTION: main
08-14 17:48:30.149: E/AndroidRuntime(674): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{br.com.example.secretario/br.com.example.secretario.conexaoMysqlActivity}: java.lang.ClassCastException: br.com.example.secretario.conexaoMysqlActivity cannot be cast to android.app.Activity
08-14 17:48:30.149: E/AndroidRuntime(674):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
08-14 17:48:30.149: E/AndroidRuntime(674):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-14 17:48:30.149: E/AndroidRuntime(674):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-14 17:48:30.149: E/AndroidRuntime(674):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-14 17:48:30.149: E/AndroidRuntime(674):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 17:48:30.149: E/AndroidRuntime(674):  at android.os.Looper.loop(Looper.java:137)
08-14 17:48:30.149: E/AndroidRuntime(674):  at android.app.ActivityThread.main(ActivityThread.java:4424)
08-14 17:48:30.149: E/AndroidRuntime(674):  at java.lang.reflect.Method.invokeNative(Native Method)
08-14 17:48:30.149: E/AndroidRuntime(674):  at java.lang.reflect.Method.invoke(Method.java:511)
08-14 17:48:30.149: E/AndroidRuntime(674):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-14 17:48:30.149: E/AndroidRuntime(674):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-14 17:48:30.149: E/AndroidRuntime(674):  at dalvik.system.NativeStart.main(Native Method)
08-14 17:48:30.149: E/AndroidRuntime(674): Caused by: java.lang.ClassCastException: br.com.example.secretario.conexaoMysqlActivity cannot be cast to android.app.Activity
08-14 17:48:30.149: E/AndroidRuntime(674):  at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
08-14 17:48:30.149: E/AndroidRuntime(674):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)

1 answer

-1

Had to extend Activity:

public class conexaoMysqlActivity extends Activity {  

Browser other questions tagged

You are not signed in. Login or sign up in order to post.