Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. package br.com.agemstar.testesql;
  2.  
  3. EditText edtnome, edtend;
  4. Button carregar;
  5.  
  6.  
  7.  
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12.  
  13. edtnome = (EditText)findViewById(R.id.edtNome);
  14. edtend = (EditText)findViewById(R.id.edtEnd);
  15. carregar = (Button) findViewById(R.id.btnN1);
  16.  
  17. carregar.setOnClickListener(new View.OnClickListener(){
  18. @Override
  19. public void onClick(View v){
  20. cadastrarUsuario();
  21. }
  22. });
  23. }
  24.  
  25. public Connection conexaoBD(){
  26. Connection connection = null;
  27. try{
  28. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  29. StrictMode.setThreadPolicy(policy);
  30. Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
  31. connection = DriverManager.getConnection("jdbc:jtds:sqlserver://192.168.0.169;databaseName=ANDROID_SQL;user=sa;password=@dm1n102030;");
  32. }catch (Exception e){
  33. Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
  34. }
  35. return connection;
  36. }
  37.  
  38. public void cadastrarUsuario(){
  39. try{
  40. PreparedStatement pst = conexaoBD().prepareStatement("insert into usuarios values (?,?)");
  41. pst.setString(1,edtnome.getText().toString());
  42. pst.setString(2,edtend.getText().toString());
  43. pst.executeUpdate();
  44. Toast.makeText(getApplicationContext(),"REGISTRO CONCLUIDO",Toast.LENGTH_SHORT).show();
  45.  
  46. } catch (SQLException e) {
  47. Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement