Advertisement
Guest User

Untitled

a guest
Aug 14th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. public class MegaPermanentes extends AppCompatActivity {
  2.  
  3. //Declarando as variáveis //
  4. ConnectionClass connectionClass;
  5. EditText editName, editDocument;
  6. Button addButton, editButton, deleteButton;
  7. ProgressBar progressBar;
  8.  
  9.  
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_mega_permanentes);
  15.  
  16. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  17. StrictMode.setThreadPolicy(policy);
  18.  
  19. connectionClass = new ConnectionClass();
  20. editName = (EditText) findViewById(R.id.edtName);
  21. editDocument = (EditText) findViewById(R.id.edtDocumento);
  22. addButton = (Button) findViewById(R.id.addButton);
  23. editButton = (Button) findViewById(R.id.editButton);
  24. deleteButton = (Button) findViewById(R.id.removeButton);
  25. progressBar = (ProgressBar) findViewById(R.id.progBar);
  26. progressBar.setVisibility(View.GONE);
  27.  
  28. addButton.setOnClickListener(new View.OnClickListener(){
  29. @Override
  30. public void onClick (View v){
  31. AddInfo addPro = new AddInfo();
  32. addPro.execute("");
  33. editName.setText("");
  34. editDocument.setText("");
  35.  
  36. }
  37. });
  38. }
  39.  
  40.  
  41.  
  42. public class AddInfo extends AsyncTask<String, String, String>{
  43. String z = "";
  44. Boolean isSucess = false;
  45.  
  46. String infoName = editName.getText().toString();
  47. String infoDocu = editDocument.getText().toString();
  48. @Override
  49. protected void onPreExecute()
  50. {
  51. progressBar.setVisibility(View.VISIBLE);
  52. }
  53.  
  54. @Override
  55. protected String doInBackground(String... params) {
  56. if(infoName.trim().equals("") || infoDocu.trim().equals(""))
  57. z = "Por favor digite um nome e um documento";
  58. else{
  59. try{
  60. Connection con = connectionClass.CONN();
  61. if (con == null){
  62. z = "Erro na conexão com o Banco de Dados";
  63. }
  64. else
  65. {
  66. String query = "insert into usuarios (nome,endereco) values ('" + infoName + "','" +infoDocu + "')";
  67. PreparedStatement preparedStatement = con.prepareStatement(query);
  68. preparedStatement.executeUpdate();
  69. z = "Cadastro inserido com sucesso";
  70. isSucess = true;
  71. }
  72. }catch( Exception ex){
  73. isSucess = false;
  74. z = "Exceptions";
  75. }
  76. }
  77.  
  78. return z;
  79. }
  80. }
  81.  
  82. @SuppressLint("NewApi")
  83. public Connection connectionclass (String user, String password, String database, String server)
  84. {
  85. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  86. StrictMode.setThreadPolicy(policy);
  87. Connection connection = null;
  88. String ConnectionURL = null;
  89. try
  90. {
  91. Class.forName("net.sourceforge.jtds.jdbc.Driver");
  92. ConnectionURL = "jdbc:jtds:sqlserver://192.168.0.169/ANDROID_SQL;instance=MEGACONTROL;user=sa;password=@dm1n102030";
  93. //ConnectionURL = "jdbc:jtds:sqlserver://" + ip +"/"+ db +";""istance=MEGACONTROL""";user=" + un + ";password="+ password + ";";
  94. connection = DriverManager.getConnection(ConnectionURL);
  95.  
  96.  
  97. } catch (ClassNotFoundException e) {
  98. e.printStackTrace();
  99. Log.e("Error here 1", e.getMessage());
  100. } catch (SQLException e) {
  101. e.printStackTrace();
  102. Log.e("Error here 2", e.getMessage());
  103. }
  104.  
  105. return connection;
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement