Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. import android.annotation.SuppressLint;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.os.StrictMode;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.util.Log;
  7. import android.view.MenuItem;
  8. import android.view.View;
  9. import android.widget.ArrayAdapter;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.RatingBar;
  13. import android.widget.Spinner;
  14. import android.widget.Toast;
  15.  
  16. import java.sql.Connection;
  17. import java.sql.DriverManager;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.sql.SQLException;
  21. import java.util.ArrayList;
  22.  
  23.  
  24. public class Valoraraciones extends AppCompatActivity{
  25. Connection con;
  26. Spinner ruta;
  27. PreparedStatement stmt;
  28. ResultSet rs;
  29. Button Boton;
  30. String ip,db,un,passwords;
  31. RatingBar rb;
  32. EditText et;
  33.  
  34. @Override
  35. protected void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.activity_valoraraciones);
  38. getSupportActionBar().setTitle("CicloMapp");
  39. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  40.  
  41. Boton=(Button)findViewById(R.id.button2);
  42. rb=(RatingBar) findViewById(R.id.ratingBar);
  43. et=(EditText) findViewById(R.id.editText3);
  44.  
  45. ip = "mssql4.gear.host";
  46. db = "ciclomapp1";
  47. un = "ciclomapp1";
  48. passwords = "Mk36-9DX-580";
  49. ruta = (Spinner) findViewById(R.id.spinner2);
  50.  
  51. String query = "select nombre from Rutas";
  52. try {
  53. con = connectionclass(un, passwords, db, ip);
  54. if (con == null) {
  55. Intent i=new Intent(this,MainActivity.class);
  56. startActivity(i);
  57. Toast.makeText(this, "Conexion a Internet perdida",Toast.LENGTH_SHORT).show();
  58. } else {
  59. stmt = con.prepareStatement(query);
  60. rs = stmt.executeQuery();
  61. ArrayList<String> data = new ArrayList<String>();
  62. while (rs.next()) {
  63. String id = rs.getString("nombre");
  64. data.add(id);
  65. }
  66. String[] array = data.toArray(new String[0]);
  67. ArrayAdapter NoCoreAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data);
  68. ruta.setAdapter(NoCoreAdapter);
  69. con.close();
  70. }
  71. } catch (SQLException e) {
  72. Intent i=new Intent(this,MainActivity.class);
  73. startActivity(i);
  74. Toast.makeText(this, "Se ha producido un error, vuelva a intentarlo mas tarde",Toast.LENGTH_SHORT).show();
  75. e.printStackTrace();
  76.  
  77. }
  78.  
  79. Boton.setOnClickListener(new View.OnClickListener() {
  80. @Override
  81. public void onClick(View v) {
  82. String descripcion=et.getText().toString();
  83. String mail = getIntent().getExtras().getString("correo");
  84. String nRuta=ruta.getSelectedItem().toString();
  85. float rbv=rb.getRating();
  86. String query="insert into valorar values(next value for seq_valorar,'"+mail+"','"+nRuta+"',"+rbv+",GETDATE(),'"+descripcion+"');";
  87. try {
  88. con = connectionclass (un, passwords, db, ip);
  89. PreparedStatement preparedStatement = con.prepareStatement(query);
  90. preparedStatement.executeUpdate();
  91. con.close();
  92. Toast.makeText(Valoraraciones.this,"Ruta valorada con Exito",Toast.LENGTH_SHORT).show();
  93. } catch (SQLException e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. });
  98.  
  99. }
  100.  
  101. @Override
  102. public boolean onOptionsItemSelected(MenuItem item) {
  103. switch (item.getItemId()) {
  104. case android.R.id.home:
  105. onBackPressed();
  106. return true;
  107. }
  108.  
  109. return super.onOptionsItemSelected(item);
  110. }
  111.  
  112. @SuppressLint("NewApi")
  113. public Connection connectionclass(String user, String password, String database, String server)
  114. {
  115. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  116. StrictMode.setThreadPolicy(policy);
  117. Connection connection = null;
  118. String ConnectionURL = null;
  119. try
  120. {
  121. Class.forName("net.sourceforge.jtds.jdbc.Driver");
  122. ConnectionURL = "jdbc:jtds:sqlserver://" + server +"/"+ database + ";user=" + user+ ";password=" + password + ";";
  123. connection = DriverManager.getConnection(ConnectionURL);
  124. }
  125. catch (SQLException se)
  126. {
  127. Log.e("error here 1 : ", se.getMessage());
  128. }
  129. catch (ClassNotFoundException e)
  130. {
  131. Log.e("error here 2 : ", e.getMessage());
  132. }
  133. catch (Exception e)
  134. {
  135. Log.e("error here 3 : ", e.getMessage());
  136. }
  137. return connection;
  138. }
  139. }
  140.  
  141. DriverManager.setLoginTimeout(Segundos);
  142.  
  143. PreparedStatement preparedstatement = con.prepareStatement(query);
  144. preparedstatement.setQueryTimeout(Segundos);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement