0

for the past few days i've been trying to solve an issue i'm having with my Android code where i execute two different threads, the first one that executes when i start the app works perfectly and there are no errors with the query. However the second thread that executes when I click the submit button is not executing the SQL query.

Here's the code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    submitButton = (Button) findViewById(R.id.submitButton);

    submitButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,"This works",Toast.LENGTH_LONG).show();

            service = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            checkPermission();

            criteria = new Criteria();

            String provider = service.getBestProvider(criteria, false);

            if (provider != null & !provider.equals("")) {
                Location location = service.getLastKnownLocation(provider);
                service.requestLocationUpdates(provider, 2000, 1, MainActivity.this);

                if (location != null) {
                    onLocationChanged(location);
                } else {
                    Toast.makeText(MainActivity.this, "location not found", Toast.LENGTH_LONG).show();
                }

            } else {
                Toast.makeText(MainActivity.this, "Provider is null", Toast.LENGTH_LONG).show();
            }

            selectionEvent = eventSpinner.getSelectedItem().toString();

            insertThread.start();

        }
    });

    sqlThread.start();
}

Thread sqlThread = new Thread(){
    public void run(){

        alertHandler = new Handler(Looper.getMainLooper());

        try{

            Class.forName("org.postgresql.Driver");

            String url = "localhost:5432";
            Connection conn = DriverManager.getConnection(url,"arodriguez","alejandro2017");

            String statementSQL = "SELECT * FROM incidencias";
            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery(statementSQL);
            while(resultSet.next()){
                tiposIncidencias.add(resultSet.getString(2));
                naturalezaIncidencias.add(resultSet.getString(3));
            }

            alertHandler.post(new Runnable() {
                @Override
                public void run() {
                    addDefaultItemsOnTypeSpinner();
                    addDefaultItemsOnEventSpinner();
                }
            });

            String statementSQL2 = "INSERT INTO arodriguez.prueba (description,x,y) VALUES ('"+selectionEvent+"',"+latitude+","+longitude+")";
            Statement s = null;
            s = conn.createStatement();
            s.executeUpdate(statementSQL2);

            conn.close();

        } catch (SQLException se){
            Toast.makeText(MainActivity.this,se.toString(),Toast.LENGTH_LONG).show();

        } catch (ClassNotFoundException e){
            Toast.makeText(MainActivity.this,e.toString(),Toast.LENGTH_LONG).show();
        } catch (Exception ex){
            Log.d("Error",ex.toString());
        }
        return;
    }
};  

Thread insertThread = new Thread(){

    public void run(){

        alertHandler = new Handler(Looper.getMainLooper());

        try{

            alertHandler.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(MainActivity.this,selectionEvent,Toast.LENGTH_LONG).show();
                }
            });

            Class.forName("org.postgresql.Driver");

            String url = "jdbc:postgresql://localhost:5432/sde";
            Connection conn = DriverManager.getConnection(url,"arodriguez","alejandro2017");

            String statementSQL = "INSERT INTO arodriguez.prueba (description,x,y) VALUES ('"+selectionEvent+"',"+latitude+","+longitude+")";
            Statement statement = null;
            statement = conn.createStatement();
            statement.executeUpdate(statementSQL);

            conn.close();

        }catch (SQLException se){
            Log.d("Error",se.toString());
        } catch (ClassNotFoundException e){
            Log.d("Error",e.toString());
        } catch (Exception ex){
            Log.d("Error",ex.toString());
        }
        return;
    }
};

For some reason if I add the insert query in the first thread it does works:

     String statementSQL = "INSERT INTO arodriguez.prueba (description,x,y) VALUES ('"+selectionEvent+"',"+latitude+","+longitude+")";
     Statement statement = null;
     statement = conn.createStatement();
     statement.executeUpdate(statementSQL);

Am I doing something wrong?

alexUne
  • 19
  • 5
  • Have any logs appeared? Have you checked if the thread started but query were not executed, or thread did not even started? – Przemysław Różycki Mar 21 '17 at 20:13
  • Nope, logs are not showing in the logcat either. The thread did started, i used a Toast maketext within a handler to check if it started or not. The only thing that is not working is the query – alexUne Mar 21 '17 at 20:23
  • As you are using JDBC - please read: http://stackoverflow.com/questions/15853367/jdbc-vs-web-service-for-android – Morrison Chang Mar 21 '17 at 20:23
  • Yup, I am using JDBC. – alexUne Mar 21 '17 at 20:26
  • I found a solution, i created a void function and called the insert thread in there, seems like you can only have one thread on a function – alexUne Mar 22 '17 at 15:45

0 Answers0