Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- 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;
- }
- 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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement