Advertisement
Guest User

Untitled

a guest
Jan 16th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. public class DB extends _Default implements Runnable {
  2.  
  3. private Connection conn;
  4. private String host = "10.0.2.2";
  5. private String db = "airsoft_mobile";
  6. private int port = 5432 ;
  7. private String user = "postgres";
  8. private String pass = "pgadmin";
  9.  
  10.  
  11. public DB() {
  12.  
  13. super();
  14. this.conecta();
  15. this.desconecta();
  16.  
  17. }
  18.  
  19. @Override
  20. public void run() {
  21.  
  22. try{
  23.  
  24. //Realiza a Conexão com o Banco de Dados
  25. Class.forName("org.postgresql.Driver");
  26. this.conn = DriverManager.getConnection("jdbc:postgresql://10.0.2.2:5432/airsoft_mobile","postgres","pgadmin");
  27.  
  28. } catch ( Exception e ){
  29.  
  30. this._mensagem = e.getMessage();
  31. this._status = false;
  32.  
  33. }
  34. }
  35.  
  36. public void conecta(){
  37.  
  38. Thread thread = new Thread(this);
  39. thread.start();
  40.  
  41. try{
  42.  
  43. thread.join();
  44.  
  45. } catch ( Exception e ){
  46.  
  47. this._mensagem = e.getMessage();
  48. this._status = false;
  49.  
  50. }
  51.  
  52. }
  53.  
  54. private void desconecta(){
  55.  
  56. if ( this.conn != null ) {
  57.  
  58. try {
  59.  
  60. this.conn.close();
  61.  
  62. }catch (Exception e){
  63.  
  64. this._status = false;
  65. this._mensagem = e.getMessage();
  66.  
  67. } finally {
  68.  
  69. this.conn = null ;
  70.  
  71. }
  72.  
  73. }
  74.  
  75. }
  76.  
  77. public ResultSet select ( String query ) {
  78.  
  79. this.conecta();
  80. ResultSet resultSet = null;
  81. try {
  82.  
  83. resultSet = new ExecuteDB(this.conn, query).execute().get();
  84.  
  85. } catch (Exception e) {
  86.  
  87. this._mensagem = e.getMessage();
  88. this._status = false;
  89.  
  90. }
  91.  
  92. return resultSet;
  93.  
  94. }
  95.  
  96. public ResultSet execute ( String query ) {
  97.  
  98. this.conecta();
  99. ResultSet resultSet = null;
  100. try {
  101.  
  102. resultSet = new ExecuteDB(this.conn, query).execute().get();
  103.  
  104. } catch (Exception e) {
  105.  
  106. this._mensagem = e.getMessage();
  107. this._status = false;
  108.  
  109. }
  110.  
  111. return resultSet;
  112.  
  113. }
  114.  
  115. public void adicionar_usuario() {
  116.  
  117. String comando = "";
  118.  
  119. if ( this.getId() == -1 ) {
  120.  
  121. comando = String.format( "INSERT INTO usuario (nome_usuario, email_usuario, nickname_usuario, " +
  122. "senha_usuario, data_nascimento_usuario, cpf_usuario, rg_usuario, " +
  123. "tipo_usuario, telefone ) " +
  124. "VALUES ( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) " ,
  125. this.getNome(), this.getEmail(), this.getNickname(), this.getSenha(), this.getData_nasc(), this.getCpf(), this.getRg(), this.getTipo_usuario(), this.getTelefone());
  126. }
  127.  
  128. DB db = new DB();
  129. db.execute(comando);
  130. this._mensagem = db._mensagem;
  131. this._status = db._status;
  132.  
  133. }
  134.  
  135. private void adicionar(){
  136.  
  137. String nome = txtNome.getText().toString();
  138. String cpf = txtCPF.getText().toString();
  139. String email = txtEmail.getText().toString();
  140. String telefone = txtTelefone.getText().toString();
  141. String nick = txtNick.getText().toString();
  142. String tipo_usuario = spnUsuario.getSelectedItem().toString();
  143. String senha = txtSenhaCadastro.getText().toString();
  144. String confirmaSenha = txtConfirmaSenha.getText().toString();
  145.  
  146. if (! ( senha.equals(confirmaSenha) )) {
  147.  
  148. Toast.makeText(ActCadastro.this, "As senhas não correspondem. Por favor digite novamente.", Toast.LENGTH_SHORT).show();
  149. txtSenhaCadastro.setText("");
  150. txtConfirmaSenha.setText("");
  151.  
  152. }
  153.  
  154. else {
  155.  
  156. this.usuario.setNome(nome);
  157. this.usuario.setCpf(cpf);
  158. this.usuario.setEmail(email);
  159. this.usuario.setTelefone(telefone);
  160. this.usuario.setTipo_usuario(tipo_usuario);
  161. this.usuario.setNickname(nick);
  162. this.usuario.setSenha(senha);
  163.  
  164. this.usuario.adicionar_usuario();
  165.  
  166. Toast.makeText(ActCadastro.this, "Dados Cadastrados com Sucesso!", Toast.LENGTH_SHORT).show();
  167.  
  168.  
  169. if (tipo_usuario.equals("Sócio de Campo de Airsoft")) {
  170.  
  171. Intent i = new Intent();
  172. i.setClass(ActCadastro.this, ActCadastroCampo.class);
  173. i.putExtra("CPF", txtCPF.getText().toString());
  174. startActivity(i);
  175.  
  176.  
  177. }
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement