I can not save the information to a local database!

4

I'm having trouble saving the information entered by the user in the PostgreSQL database, I've developed an application in Android Studio and I'm using an external local database with PostgreSQL to save the data, however when I run the application and I enter the given the application to work, I already looked at the code and could not find the error in the function.

Stack Trace

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.matheus.privatewalletm, PID: 3423
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4780) 
at android.view.View$PerformClick.run(View.java:19866) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Caused by: java.util.IllegalFormatConversionException: %d can't format java.lang.String arguments
at java.util.Formatter.badArgumentType(Formatter.java:1489)
at java.util.Formatter.transformFromInteger(Formatter.java:1689)
at java.util.Formatter.transform(Formatter.java:1461)
at java.util.Formatter.doFormat(Formatter.java:1081)
at java.util.Formatter.format(Formatter.java:1042)
at java.util.Formatter.format(Formatter.java:1011)
at java.lang.String.format(String.java:1803)
at java.lang.String.format(String.java:1777)
at com.example.matheus.privatewalletm.Usuario.confirmar(Usuario.java:134)
at com.example.matheus.privatewalletm.Novo.confirmar(Novo.java:61)
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:4780) 
at android.view.View$PerformClick.run(View.java:19866) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

Involved Classes

Class Novo :

public class Novo extends AppCompatActivity {

    private Usuario usuario;
    private EditText editTextNome;
    private EditText editTextData;
    private EditText editTextRg;
    private EditText editTextCpf;
    private EditText editTextDoenca;
    private EditText editTextAlergia;
    private EditText editTextProfissao;


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

        this.usuario = new Usuario();
        this.editTextNome = (EditText) findViewById(R.id.editTextNome);
        this.editTextData = (EditText) findViewById(R.id.editTextData);
        this.editTextRg = (EditText) findViewById(R.id.editTextRg);
        this.editTextCpf = (EditText) findViewById(R.id.editTextCpf);
        this.editTextDoenca = (EditText) findViewById(R.id.editTextDoenca);
        this.editTextAlergia = (EditText) findViewById(R.id.editTextAlergia);
        this.editTextProfissao = (EditText) findViewById(R.id.editTextProfissao);

        Intent intent = getIntent();
            if(intent != null){
                Bundle bundle = intent.getExtras();
                if(bundle != null){
                    this.usuario.setId(bundle.getInt("id"));
                    this.editTextNome.setText(bundle.getString("nome"));
                    this.editTextData.setText(bundle.getInt("data"));
                    this.editTextRg.setText(bundle.getString("rg"));
                    this.editTextCpf.setText(bundle.getString("cpf"));
                    this.editTextDoenca.setText(bundle.getString("doenca"));
                    this.editTextAlergia.setText(bundle.getString("alergia"));
                    this.editTextProfissao.setText(bundle.getString("profissao"));

                }
            }
    }

    public void confirmar (View view){
        this.usuario.setNome(this.editTextNome.getText().toString());
        this.usuario.setData(this.editTextData.getText().toString());
        this.usuario.setRg(this.editTextRg.getText().toString());
        this.usuario.setCpf(this.editTextCpf.getText().toString());
        this.usuario.setDoenca(this.editTextDoenca.getText().toString());
        this.usuario.setAlergia(this.editTextAlergia.getText().toString());
        this.usuario.setProfissao(this.editTextProfissao.getText().toString());
        this.usuario.confirmar();

        Toast.makeText(this,this.usuario.get_messagem(),Toast.LENGTH_LONG).show();
        if(usuario._status)
            finish();
    }

    public void cancelar (View view){
        finish();
    }
}

Class Usuario :

public class Usuario extends _default {
    private int id;
    private String nome;
    private String data;
    private String rg;
    private String cpf;
    private String doenca;
    private String alergia;
    private String profissao;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getRg() {
        return rg;
    }

    public void setRg(String rg) {
        this.rg = rg;
    }

    public String getCpf() {
        return cpf;
    }

    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    public String getDoenca() {
        return doenca;
    }

    public void setDoenca(String doenca) {
        this.doenca = doenca;
    }

    public String getAlergia() {
        return alergia;
    }

    public void setAlergia(String alergia) {
        this.alergia = alergia;
    }

    public String getProfissao() {
        return profissao;
    }

    public void setProfissao(String profissao) {
        this.profissao = profissao;
    }

    public Usuario(){
        super();
        this.id = -1;
        this.nome = "";
        this.data = "";
        this.rg = "";
        this.cpf = "";

        this.doenca = "";
        this.alergia = "";
        this.profissao = "";
    }

    public ArrayList<Usuario> getLista(){
        BD bd = new BD();
        ArrayList<Usuario> lista = new ArrayList<>();
        try {
            ResultSet resultSet = bd.select("SELECT * FROM usuario");
            if(resultSet != null ){
                while (resultSet.next()){
                    Usuario obj = new Usuario();
                    obj.setId(resultSet.getInt("id"));
                    obj.setNome(resultSet.getString("nome"));
                    obj.setData(resultSet.getString("data"));
                    obj.setRg(resultSet.getString("rg"));
                    obj.setCpf(resultSet.getString("cpf"));
                    obj.setDoenca(resultSet.getString("doenca"));
                    obj.setAlergia(resultSet.getString("alergia"));
                    obj.setProfissao(resultSet.getString("profissao"));
                    lista.add(obj);
                    obj = null;
                }
            }
        }
        catch (Exception e)
        {
            Log.d("PrivateWalletM", e.getMessage());
            this._messagem = e.getMessage();
            this._status = false;

        }
        return lista;
    }

    public void confirmar(){
        String comando = "";
        if(this.getId() == -1){
            comando = String.format("INSERT INTO Usuario(nome, data, rg, cpf, doenca, alergia, profissao) values ('%s', '%d', '%s', '%s', '%s', '%s','%s' );",
                    this.getNome(), this.getData(), this.getRg(), this.getCpf(), this.getDoenca(), this.getAlergia(), this.getProfissao());

        }
        else
        {
            comando = String.format("UPDATE Usuario SET nome ='%s', date = '%d', rg = '%s', cpf = '%s', doenca = '%s', alergia = '%s', profissao = '%s' WHERE id = %d;",
                    this.getNome(), this.getData(), this.getRg(), this.getCpf(), this.getDoenca(), this.getAlergia(), this.getProfissao(), this.getId());
        }
        BD bd = new BD();
       bd.execute(comando);
       this._messagem = bd._messagem;
        this._status = bd._status ;
    }

    public void apagar(){
        String  comando =  String.format("DELETE FROM usuario WHERE id = %d ", this.getId());

        BD bd = new BD();
        bd.execute(comando);
        this._messagem = bd._messagem;
        this._status = bd._status ;    
    }    
}

Class UsuarioAdapter :

public class UsuarioAdapter extends ArrayAdapter<Usuario> {
    private Context context;
    private ArrayList<Usuario> lista;

public UsuarioAdapter(Context context, ArrayList<Usuario> lista)
    {
        super(context,0,lista);
        this.context = context;
        this.lista = lista;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        final Usuario itemPosicao = this.lista.get(position);

        convertView = LayoutInflater.from(this.context).inflate(R.layout.item_lista,null);
        final View layout = convertView;

        TextView textViewNome = (TextView) convertView.findViewById(R.id.textViewNome);
        textViewNome.setText(itemPosicao.getNome());

        TextView textViewData = (TextView) convertView.findViewById(R.id.textViewData);
        textViewData.setText(itemPosicao.getData());

        TextView textViewRg = (TextView) convertView.findViewById(R.id.textViewRg);
        textViewRg.setText(itemPosicao.getRg());

        TextView textViewCpf = (TextView) convertView.findViewById(R.id.textViewCpf);
        textViewCpf.setText(itemPosicao.getCpf());

        TextView textViewDoenca = (TextView) convertView.findViewById(R.id.textViewDoenca);
        textViewDoenca.setText(itemPosicao.getDoenca());

        TextView textViewAlergia = (TextView) convertView.findViewById(R.id.textViewAlergia);
        textViewAlergia.setText(itemPosicao.getAlergia());

        TextView textViewProfissao = (TextView) convertView.findViewById(R.id.textViewProfissao);
        textViewProfissao.setText(itemPosicao.getProfissao());

        Button button = (Button) convertView.findViewById(R.id.buttonEditar);
        button.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){
            Intent intent = new Intent(context,Novo.class);
            intent.putExtra("nome", itemPosicao.getNome());
            intent.putExtra("data", itemPosicao.getData());
            intent.putExtra("rg", itemPosicao.getRg());
            intent.putExtra("cpf", itemPosicao.getCpf());
            intent.putExtra("doenca", itemPosicao.getDoenca());
            intent.putExtra("alergia", itemPosicao.getAlergia());
            intent.putExtra("profissao", itemPosicao.getProfissao());
            context.startActivity(intent);
           }
        });
    Button buttonDeletar = (Button) convertView.findViewById(R.id.buttonApagar);
        buttonDeletar.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                itemPosicao.apagar();
                if (itemPosicao._status)
                    layout.setVisibility(View.GONE);
                else
                    Toast.makeText(context, itemPosicao.get_messagem(), Toast.LENGTH_LONG).show();
            }
            });

        return convertView;  
    }
}

BD Classes

Class ExecuteBD :

public class ExecuteBD extends AsyncTask<String, Void, ResultSet>{
    private Connection connection;
    private String query;

    public ExecuteBD(Connection connection, String query) {
        this.connection = connection;
        this.query = query;
    }


    @Override
    protected ResultSet doInBackground(String... strings) {
        ResultSet resultSet = null;
        try
        {
            resultSet = connection.prepareStatement(query).executeQuery();
        }
        catch(Exception e)
        {
            Log.d("PrivateWalletM", e.getMessage());

        }finally {
            try
            {
            connection.close();
            }
            catch (Exception e)
            {
                Log.d("PrivateWalletM", e.getMessage());
            }

        }
        return resultSet;
    }
}

Class BD :

public class BD extends _default implements Runnable {
    private Connection conn;
    private String host = "192.168.0.100";
    private String db = "android";
    private int port = 2345;
    private String user = "androidUser";
    private String pass = "*android*";
    private String url = "jbdc:postgresql://%s:%d/%s";

    public BD (){
        super();
        this.url = String.format(this.url, this.host, this.port, this.db);

        this.conecta();
        this.disconecta();
    }

    @Override
    public void run() {
        try{
            Class.forName("org.postgresql.Driver");
            this.conn = DriverManager.getConnection(this.url, this.user, this.pass);
        }
        catch(Exception e)
        {
            Log.d("PrivateWalletM", e.getMessage());
            this._messagem = e.getMessage();
            this._status = false;

        }
    }
    private void conecta(){
        Thread thread = new Thread(this);
        thread.start();
        try
        {
            thread.join();
        }
        catch (Exception e)
        {
            Log.d("PrivateWalletM", e.getMessage());
            this._messagem = e.getMessage();
            this._status = false;
        }
    }

    private void disconecta(){
        if(this.conn != null ){
            try
            {
                this.conn.close();
            }
            catch (Exception e)
            {
                Log.d("PrivateWalletM", e.getMessage());
            }finally {
                this.conn = null;
            }
        }
    }
    public ResultSet select (String query){
        this.conecta();
        ResultSet resultSet = null;
        try {
            resultSet = new ExecuteBD(this.conn, query).execute().get();
        }
        catch (Exception e)
        {
            Log.d("PrivateWalletM", e.getMessage());
            this._status = false;
            this._messagem = e.getMessage();
        }
        return resultSet;
    }

    public ResultSet execute (String query){
        this.conecta();
        ResultSet resultSet = null;
        try {
            resultSet = new ExecuteBD(this.conn, query).execute().get();
        }
        catch (Exception e)
        {
            Log.d("PrivateWalletM", e.getMessage());
            this._status = false;
            this._messagem = e.getMessage();
        }
        return resultSet;
    }
}

BD table image:

    
asked by anonymous 08.09.2016 / 02:46

1 answer

5
  

Caused by: java.util.IllegalFormatConversionException: %d can not format java.lang.String arguments

In the confirmar method, in the Usuario class, when using String.format " you are using the %d format for the getData function that returns a string :

public class Usuario extends _default {
    // ...
    private String data;
    // ...

    public String getData() {
        return data;
    }

This happens on both lines of if/else :

public void confirmar(){
    String comando = "";
    if(this.getId() == -1){
        comando = String.format(
          "INSERT INTO Usuario(nome, data, ...) values ('%s', '%d', ....);",
          this.getNome(), this.getData(), ...);                ^^
    }
    else
    {
        comando = String.format(
          "UPDATE Usuario SET nome ='%s', date = '%d', ...",
          this.getNome(), this.getData(), ...);   ^^
    }
    // ....
}

Change %d to %s .

    
08.09.2016 / 03:16