I'm doing a project in Android Studio, where a part is to register a user, I managed to make it work but after two registration tests the registration message is completed but the registration does not appear in the database and neither error message in Android Studio Debug.
Follow the code:
Class that manages the connection:
public class DB extends _Default implements Runnable {
private Connection conn;
private String host = "10.0.2.2";
private String db = "airsoft_mobile";
private int port = 5432 ;
private String user = "postgres";
private String pass = "pgadmin";
public DB() {
super();
this.conecta();
this.desconecta();
}
@Override
public void run() {
try{
//Realiza a Conexão com o Banco de Dados
Class.forName("org.postgresql.Driver");
this.conn = DriverManager.getConnection("jdbc:postgresql://10.0.2.2:5432/airsoft_mobile","postgres","pgadmin");
} catch ( Exception e ){
this._mensagem = e.getMessage();
this._status = false;
}
}
public void conecta(){
Thread thread = new Thread(this);
thread.start();
try{
thread.join();
} catch ( Exception e ){
this._mensagem = e.getMessage();
this._status = false;
}
}
private void desconecta(){
if ( this.conn != null ) {
try {
this.conn.close();
}catch (Exception e){
this._status = false;
this._mensagem = e.getMessage();
} finally {
this.conn = null ;
}
}
}
public ResultSet select ( String query ) {
this.conecta();
ResultSet resultSet = null;
try {
resultSet = new ExecuteDB(this.conn, query).execute().get();
} catch (Exception e) {
this._mensagem = e.getMessage();
this._status = false;
}
return resultSet;
}
public ResultSet execute ( String query ) {
this.conecta();
ResultSet resultSet = null;
try {
resultSet = new ExecuteDB(this.conn, query).execute().get();
} catch (Exception e) {
this._mensagem = e.getMessage();
this._status = false;
}
return resultSet;
}
}
The function responsible for adding the user that is within the class User.java:
public void adicionar_usuario() {
String comando = "";
if ( this.getId() == -1 ) {
comando = String.format( "INSERT INTO usuario (nome_usuario, email_usuario, nickname_usuario, " +
"senha_usuario, data_nascimento_usuario, cpf_usuario, rg_usuario, " +
"tipo_usuario, telefone ) " +
"VALUES ( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) " ,
this.getNome(), this.getEmail(), this.getNickname(), this.getSenha(), this.getData_nasc(), this.getCpf(), this.getRg(), this.getTipo_usuario(), this.getTelefone());
}
DB db = new DB();
db.execute(comando);
this._mensagem = db._mensagem;
this._status = db._status;
}
Responsible for collecting data to add users:
private void adicionar(){
String nome = txtNome.getText().toString();
String cpf = txtCPF.getText().toString();
String email = txtEmail.getText().toString();
String telefone = txtTelefone.getText().toString();
String nick = txtNick.getText().toString();
String tipo_usuario = spnUsuario.getSelectedItem().toString();
String senha = txtSenhaCadastro.getText().toString();
String confirmaSenha = txtConfirmaSenha.getText().toString();
if (! ( senha.equals(confirmaSenha) )) {
Toast.makeText(ActCadastro.this, "As senhas não correspondem. Por favor digite novamente.", Toast.LENGTH_SHORT).show();
txtSenhaCadastro.setText("");
txtConfirmaSenha.setText("");
}
else {
this.usuario.setNome(nome);
this.usuario.setCpf(cpf);
this.usuario.setEmail(email);
this.usuario.setTelefone(telefone);
this.usuario.setTipo_usuario(tipo_usuario);
this.usuario.setNickname(nick);
this.usuario.setSenha(senha);
this.usuario.adicionar_usuario();
Toast.makeText(ActCadastro.this, "Dados Cadastrados com Sucesso!", Toast.LENGTH_SHORT).show();
if (tipo_usuario.equals("Sócio de Campo de Airsoft")) {
Intent i = new Intent();
i.setClass(ActCadastro.this, ActCadastroCampo.class);
i.putExtra("CPF", txtCPF.getText().toString());
startActivity(i);
}
}
Thanks in advance for the help.