Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.annotation.SuppressLint;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.StrictMode;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.RatingBar;
- import android.widget.Spinner;
- import android.widget.Toast;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- public class Valoraraciones extends AppCompatActivity{
- Connection con;
- Spinner ruta;
- PreparedStatement stmt;
- ResultSet rs;
- Button Boton;
- String ip,db,un,passwords;
- RatingBar rb;
- EditText et;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_valoraraciones);
- getSupportActionBar().setTitle("CicloMapp");
- getSupportActionBar().setDisplayHomeAsUpEnabled(true);
- Boton=(Button)findViewById(R.id.button2);
- rb=(RatingBar) findViewById(R.id.ratingBar);
- et=(EditText) findViewById(R.id.editText3);
- ip = "mssql4.gear.host";
- db = "ciclomapp1";
- un = "ciclomapp1";
- passwords = "Mk36-9DX-580";
- ruta = (Spinner) findViewById(R.id.spinner2);
- String query = "select nombre from Rutas";
- try {
- con = connectionclass(un, passwords, db, ip);
- if (con == null) {
- Intent i=new Intent(this,MainActivity.class);
- startActivity(i);
- Toast.makeText(this, "Conexion a Internet perdida",Toast.LENGTH_SHORT).show();
- } else {
- stmt = con.prepareStatement(query);
- rs = stmt.executeQuery();
- ArrayList<String> data = new ArrayList<String>();
- while (rs.next()) {
- String id = rs.getString("nombre");
- data.add(id);
- }
- String[] array = data.toArray(new String[0]);
- ArrayAdapter NoCoreAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data);
- ruta.setAdapter(NoCoreAdapter);
- con.close();
- }
- } catch (SQLException e) {
- Intent i=new Intent(this,MainActivity.class);
- startActivity(i);
- Toast.makeText(this, "Se ha producido un error, vuelva a intentarlo mas tarde",Toast.LENGTH_SHORT).show();
- e.printStackTrace();
- }
- Boton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String descripcion=et.getText().toString();
- String mail = getIntent().getExtras().getString("correo");
- String nRuta=ruta.getSelectedItem().toString();
- float rbv=rb.getRating();
- String query="insert into valorar values(next value for seq_valorar,'"+mail+"','"+nRuta+"',"+rbv+",GETDATE(),'"+descripcion+"');";
- try {
- con = connectionclass (un, passwords, db, ip);
- PreparedStatement preparedStatement = con.prepareStatement(query);
- preparedStatement.executeUpdate();
- con.close();
- Toast.makeText(Valoraraciones.this,"Ruta valorada con Exito",Toast.LENGTH_SHORT).show();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- });
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case android.R.id.home:
- onBackPressed();
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- @SuppressLint("NewApi")
- public Connection connectionclass(String user, String password, String database, String server)
- {
- StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
- StrictMode.setThreadPolicy(policy);
- Connection connection = null;
- String ConnectionURL = null;
- try
- {
- Class.forName("net.sourceforge.jtds.jdbc.Driver");
- ConnectionURL = "jdbc:jtds:sqlserver://" + server +"/"+ database + ";user=" + user+ ";password=" + password + ";";
- connection = DriverManager.getConnection(ConnectionURL);
- }
- catch (SQLException se)
- {
- Log.e("error here 1 : ", se.getMessage());
- }
- catch (ClassNotFoundException e)
- {
- Log.e("error here 2 : ", e.getMessage());
- }
- catch (Exception e)
- {
- Log.e("error here 3 : ", e.getMessage());
- }
- return connection;
- }
- }
- DriverManager.setLoginTimeout(Segundos);
- PreparedStatement preparedstatement = con.prepareStatement(query);
- preparedstatement.setQueryTimeout(Segundos);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement