Advertisement
kavallo

Usar Sql en Android

Aug 1st, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1.   public Connection conexionDB(){
  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.1;databaseName=db;user=sa;password=12345");
  8.         }catch (Exception e){
  9.  
  10.         }
  11.  
  12.         return conexion;
  13.     }
  14.  
  15.  
  16.  
  17.  
  18.     public void login(View v){
  19.         try{
  20.  
  21.             txtUsuario = (EditText)findViewById(R.id.txtUsuario);
  22.             txtClave = (EditText)findViewById(R.id.txtClave);
  23.  
  24.             PreparedStatement pst=conexionDB().prepareStatement("Select * From persona Where Usuario = ? And Clave = ?");
  25.             pst.setString(1,txtUsuario.getText().toString());
  26.             pst.setString(2,txtClave.getText().toString());
  27.             ResultSet rs = pst.executeQuery();
  28.             if(rs.next()){
  29.                 Toast.makeText(getApplicationContext(), "Acceso Correcto", Toast.LENGTH_SHORT).show();
  30.                 Intent Intent = new Intent(this, PrincipalActivity.class);
  31.                 startActivity(Intent);
  32.             }else{
  33.                 Toast.makeText(getApplicationContext(), "Acceso Incorrecto", Toast.LENGTH_SHORT).show();
  34.             }
  35.  
  36.         }catch (Exception e){
  37.             Toast.makeText(getApplicationContext(), "Acceso Incorrecto", Toast.LENGTH_SHORT).show();
  38.         }
  39.     }
  40.  
  41.  
  42.  
  43.  
  44. Link :
  45. https://sourceforge.net/projects/jtds/files/latest/download?source=files
  46. https://www.youtube.com/watch?v=OOm03ezazAI
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement