Problem to Build Create an Android Studio SQL Query Class

2

My problem today is that you have been developing an APP that has a Login, a Maintenance Products (To say more), everything with the SQL database engine

already create the login all Ok, do the procedure of a normal log with user and pass, also create the maintenance of creating products, where I take as parameters the name of the product and its respective description, I add, edit, and delete, only that it does not manage to search the data and show it to me when I access the corresponding layout, using the onCreate method of my MainMenu but that's not the case The corresponding case is that I want to know how I do that by means of a button, I look for the names that I said or they have relation to the edittext of the name, and tried to do it using the following code:

package com.sqldata.gst.appsql;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;


/**
 * Created by Administrador on 05/10/2016.
 */

public class ConClientes extends MainActivity {
    Conexion conexionSQL;
    EditText txtCdCliente, txtNomCli;
    Button btnBuscar, btnRetornar;
    ProgressBar pgrCliente;
    ListView lstClientes;
    String idCliente;
    //ResultSet rs;

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

        conexionSQL = new Conexion();
        txtCdCliente = (EditText) findViewById(R.id.txtCdCliente);
        //txtNomCli = (EditText) findViewById(R.id.txtNomCli);
        btnBuscar = (Button) findViewById(R.id.btnBuscar);
        btnRetornar = (Button) findViewById(R.id.btnRetornar);
        pgrCliente = (ProgressBar) findViewById(R.id.pgrCliente);
        lstClientes = (ListView) findViewById(R.id.lstClientes);
        pgrCliente.setVisibility(View.GONE);
        idCliente = "";

        // Evento Ejecutar Boton
        btnBuscar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectClientes selectClientes = new SelectClientes();
                selectClientes.execute(""); //Cannot resolve method 'execute(java.lang.String)
            }
        });
    }

    public class FillList extends AsyncTask<String, String, String>
    {
        String result = "";

        List<Map<String, String>> CliList = new ArrayList<Map<String, String>>();

        @Override
        protected void onPreExecute(){
            pgrCliente.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(String r){
            pgrCliente.setVisibility(View.GONE);
            Toast.makeText(ConClientes.this, r, Toast.LENGTH_SHORT).show();

            String[] from = {"A", "B", "C"};
            int[] views = {R.id.lblClienteId, R.id.lblNomCli, R.id.lblCodCli};
            final SimpleAdapter ADA = new SimpleAdapter(ConClientes.this, CliList, R.layout.lst_cliente,
                    from, views);
            lstClientes.setAdapter(ADA);

            lstClientes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    HashMap<String, Object> obj = (HashMap<String, Object>) ADA.getItem(arg2);
                    idCliente = (String) obj.get("A");
                    //String ClienName = (String) obj.get("B");
                    String ClienCod = (String) obj.get("C");
                    //txtNomCli.setText(ClienName);
                    txtCdCliente.setText(ClienCod);

                }
            });
        }

        @Override
        protected String doInBackground (String... params){
            try{
                Connection cnSQL = conexionSQL.CONN();
                if (cnSQL == null){
                    result = "Error en la Conexión SQL Server";
                }
                else{
                    String query = "select * from clientes";
                    PreparedStatement psSQL = cnSQL.prepareStatement(query);
                    ResultSet rsSQL = psSQL.executeQuery();

                    ArrayList data1 = new ArrayList();
                    while (rsSQL.next()){
                        Map<String, String> dataRec = new HashMap<String, String>();
                        dataRec.put("A", rsSQL.getString("idcliente"));
                        dataRec.put("B", rsSQL.getString("nom_cli"));
                        dataRec.put("C", rsSQL.getString("cod_cli"));
                        CliList.add(dataRec);
                    }
                    result = "Success";
                }
            } catch (Exception ex){
                result = "Error al Buscar Datos de la Tabla Clientes";
            }
            return result;
        }
    }

    public class SelectClientes extends ArrayList<String>{

        String result = "";
        Boolean isSuccess = false;

        String ClienCod = txtCdCliente.getText().toString();
        String NomCli = txtNomCli.getText().toString();

        @Override //Method does not override method from its superclass
        protected void onPreExecute(){
            pgrCliente.setVisibility(View.VISIBLE);
        }

        //@Override
        protected void onPostExecute(String r){
            pgrCliente.setVisibility(View.GONE);
            Toast.makeText(ConClientes.this, r, Toast.LENGTH_SHORT).show();
            if (isSuccess == true){
                FillList fillList = new FillList();
                fillList.execute("");
            }
        }

        @Override //Method does not override method from its superclass
        protected String doInBackground(String... params){
            if (ClienCod.trim().equals(""))
                result = "Favor de Introducir el Codigo del Cliente";
            else {
                try{
                    Connection con = conexionSQL.CONN();
                    if (con == null){
                        result = "No Hay Datos para Mostrar";
                    } else {
                        String query = "Select * from clientes where idcliente =" + idCliente;
                        PreparedStatement preparedStatement = con.prepareStatement(query);
                        preparedStatement.executeUpdate();
                        result = "Busqueda de Datos Correcta";
                        isSuccess = true;
                    }
                } catch (Exception ex){
                    isSuccess = false;
                    result = "Verifique los Datos";
                }
            }
            return result;
        }


    }
}

Well as you see that is my code that I tried to make but it has two code factors those who are commented the first is:

selectClientes.execute(""); //Cannot resolve method 'execute(java.lang.String)

and the second one in the Override

@Override //Method does not override method from its superclass

That's my little problem that I do not understand how I can do it to make the query consisting of editext = which will be the name I'll look for or the names, and the button that will execute the action of select * from, they understand me

and at the step I would like to make that when I enter the layout from the menu that loads and shows the data, without having to press anything

such as the doInBackground method that's what I want to do but I guess it's on the onCreate you will understand me If you need more details, please say, what I want is to solve this problem.

Thanks to Tdos !!

    
asked by Mblack47 06.10.2016 в 15:09
source

1 answer

2

Reviewing your code I see several details, mainly the ones you mention:

1)

  selectClientes.execute(""); //Cannot resolve method 'execute(java.lang.String)

In this case you try to execute a Asynctask , but SelectClientes is not an Asynctask

 SelectClientes selectClientes = new SelectClientes();
                selectClientes.execute(""); //Cannot resolve method 'execute(java.lang.String)

You have defined SelectClientes as a class that extends from ArrayList

public class SelectClientes extends ArrayList<String>{

should be:

public class SelectClientes extends AsyncTask<String, String, String>{

2) @Override //Method does not override method from its superclass If you correct the previous point, all these methods will be found in the parent class Asynctask, and this point would be solved.

Regarding your question:

  

search for me the names that you type or have relation to the edittext of the name

The line to get the EditText reference for the name is commented !, Remove the // :

 txtNomCli = (EditText) findViewById(R.id.txtNomCli);

Inside your Asynctask you get the text that is in the EditText:

 String NomCli = txtNomCli.getText().toString();

and you must do something similar to what you do with the client code that is to obtain the value and make a query with it.

String query = "Select * from clientes where nombre_cliente =" + NomCli;

Regarding the error:

  

The executeUpdate method must not return to result set.

Is it because you are doing an Update?, you must change to use the executeQuery() method:

String query = "Select * from clientes where idcliente =" + idCliente;
PreparedStatement preparedStatement = con.prepareStatement(query);

ResultSet rs = preparedStatement.executeQuery();
//preparedStatement.executeUpdate();
    
answered by 06.10.2016 / 22:04
source