Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. public Connection conexionBD() {
  2. Connection conexion = null;
  3. try{
  4. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  5. StrictMode.setThreadPolicy(policy);
  6. Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
  7. conexion = DriverManager.getConnection("jdbc:jtds:sqlserver://192.168.0.10:1433;databasename=speedex;user=usuario;password=123;");
  8.  
  9. }
  10. catch (Exception e) {
  11. Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
  12. }
  13. return conexion;
  14. }
  15.  
  16. public boolean consultarProyecto() {
  17.  
  18. paquete = (EditText) findViewById(R.id.txtPaquete);
  19. boton = (Button) findViewById(R.id.btnBuscar);
  20.  
  21. envio = paquete.getText().toString();
  22.  
  23. boolean resp = false;
  24. try{ //Realizar consulta SELECT
  25. String sql = "SELECT es.estado, en.fechaLlegada, t.transporte, en.precioTotal FROM estado es, envio en, medioTransporte t, paquete p, grupoPaquete g WHERE es.estadoId = en.estadoId AND t.transporteId = en.transporteId AND g.paqueteId = p.paqueteId AND en.grupoId = g.grupoId AND p.paqueteId = ?;";
  26. PreparedStatement cmd = conexionBD().prepareStatement(sql);
  27. //Llenar los parametros de la clase
  28. cmd.setInt(1, Integer.parseInt(envio));
  29. //Ejecutar la consulta
  30. //pedira importar la clase ResultSet
  31. ResultSet rs = cmd.executeQuery();
  32. //Recorrer la lista de registros
  33. if(rs.next()){
  34. resp = true;
  35. //asignandole a los atributos de la clase
  36.  
  37. estado = rs.getString(1);
  38. fecha = rs.getString(2);
  39. transporte = rs.getString(3);
  40. precio = rs.getDouble(4);
  41.  
  42. }
  43. //cerrando conexion
  44. cmd.close();
  45. conexionBD().close();
  46.  
  47. Toast.makeText(getApplicationContext(), "Consulta exitosa", Toast.LENGTH_LONG).show();
  48. }
  49. catch (Exception ex){
  50. Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_LONG).show();
  51. }
  52. return resp;
  53. }
  54.  
  55. public void llamarSegundoActiviy(View view) {
  56.  
  57. consultarProyecto();
  58. System.out.println("El estado es " + estado + "/n" + "La fecha es " + fecha + "/n" + "El tipo de transporte es " + transporte + "/n" + "El precio es " + precio);
  59. Intent intent = new Intent(MainActivity.this, ScrollingActivity.class);
  60. intent.putExtra("ESTADO", estado);
  61. intent.putExtra("FECHA", fecha);
  62. intent.putExtra("TRANSPORTE", transporte);
  63. intent.putExtra("PRECIO", precio);
  64. startActivity(intent);
  65. }
  66.  
  67. public class ScrollingActivity extends AppCompatActivity {
  68. TextView estado, fecha, transporte, precio;
  69. Integer numPaquete;
  70. String estado1, fecha1, transporte1;
  71. Double precio1;
  72.  
  73. DecimalFormat formato = new DecimalFormat("$0.00");
  74.  
  75. @Override
  76. protected void onCreate(Bundle savedInstanceState) {
  77. super.onCreate(savedInstanceState);
  78. setContentView(R.layout.activity_scrolling);
  79. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  80. setSupportActionBar(toolbar);
  81.  
  82. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  83.  
  84. estado = (TextView) findViewById(R.id.txtEstado);
  85. fecha = (TextView) findViewById(R.id.txtFecha);
  86. transporte = (TextView) findViewById(R.id.txtTransporte);
  87. precio = (TextView) findViewById(R.id.txtPrecio);
  88.  
  89. Intent inten = getIntent();
  90. Bundle extras = inten.getExtras();
  91.  
  92. if(extras != null) {
  93. estado1 = extras.getString("ESTADO");
  94. estado.setText(estado1);
  95.  
  96. fecha1 = extras.getString("FECHA");
  97. fecha.setText(fecha1);
  98.  
  99. transporte1 = extras.getString("TRANSPORTE");
  100. transporte.setText(transporte1);
  101.  
  102. precio1 = extras.getDouble("ESTADO");
  103. precio.setText(String.valueOf(formato.format(precio1)));
  104. }
  105.  
  106. else{
  107. Toast.makeText(getApplicationContext(), "El numero de paquete no existe", Toast.LENGTH_LONG).show();
  108. Intent intent = new Intent(ScrollingActivity.this, MainActivity.class);
  109. startActivity(intent);
  110. }
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement