Advertisement
Guest User

Untitled

a guest
Jan 6th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.07 KB | None | 0 0
  1. package pepapadua.whatslearn1;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.os.AsyncTask;
  5. import android.os.Bundle;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.ProgressBar;
  12. import android.widget.Toast;
  13.  
  14. import java.sql.Connection;
  15. import java.sql.DriverManager;
  16. import java.sql.ResultSet;
  17. import java.sql.SQLException;
  18. import java.sql.Statement;
  19. import android.os.StrictMode;
  20.  
  21. /**
  22.  * Created by pepap on 1/7/2018.
  23.          */
  24.  
  25. public class LoginActivity extends AppCompatActivity {
  26.  
  27.     Connection con;
  28.     Button login = findViewById(R.id.email_sign_in_button);
  29.     EditText cislo_tridy = findViewById(R.id.classid);
  30.     EditText uz_jmeno = findViewById(R.id.user_name);
  31.     EditText pass = findViewById(R.id.password);
  32.     ProgressBar prgbar = findViewById(R.id.login_progress);
  33.  
  34.     String ip = "db.mysql-01.gsp-europe.net";
  35.     String dn = "sql_4564";
  36.     String psw = "wCYjYvVCyB57nocZ3wYNPs6f00d2h9A";
  37.     String table = "wl_users";
  38.  
  39.     @Override
  40.     protected void onCreate(Bundle savedInstanceState){
  41.         super.onCreate(savedInstanceState);
  42.         setContentView(R.layout.activity_login);
  43.         prgbar.setVisibility(View.GONE);
  44.         login.setOnClickListener(new View.OnClickListener(){
  45.         @Override
  46.         public void onClick (View v){
  47.             CheckLogin checkLogin = new CheckLogin();// this is the Asynctask, which is used to process in background to reduce load on app process
  48.             checkLogin.execute("");
  49.         }
  50.     });
  51.     //End Setting up the function when button login is clicked
  52.  
  53.  
  54. }
  55.     public class CheckLogin extends AsyncTask<String,String,String> {
  56.         String z = "";
  57.         Boolean isSuccess = false;
  58.  
  59.         @Override
  60.         protected void onPreExecute() {
  61.            prgbar.setVisibility(View.VISIBLE);
  62.         }
  63.  
  64.         @Override
  65.         protected void onPostExecute(String r) {
  66.             prgbar.setVisibility(View.GONE);
  67.             Toast.makeText(LoginActivity.this, r, Toast.LENGTH_SHORT).show();
  68.             if(isSuccess) {
  69.                 Toast.makeText(LoginActivity.this , "Uspesne prihlasen" , Toast.LENGTH_LONG).show();
  70.                 //finish();
  71.             }
  72.         }
  73.         @Override
  74.         protected String doInBackground(String... params) {
  75.             String classka = cislo_tridy.getText().toString();
  76.             String usernam = uz_jmeno.getText().toString();
  77.             String passwordd = pass.getText().toString();
  78.             if(usernam.trim().equals("")|| passwordd.trim().equals("")|| classka.trim().equals(""))
  79.                 z = "Vlozte prosim cislo tridy, uzivatelske id a heslo";
  80.             else {
  81.                 try {
  82.                     con = connectionclass(dn, psw, dn, ip);        // Connect to database
  83.                     if (con == null) {
  84.                         z = "Nelze pripojit k serveru!";
  85.                     }
  86.                     else {
  87.                         // Change below query according to your own database.
  88.                         String query = "SELECT * FROM `wl_users` WHERE `user_name` " + uz_jmeno.toString() + " AND `password " + pass.toString() + " AND `class_number` "+cislo_tridy.toString();
  89.                         Statement stmt = con.createStatement();
  90.                         ResultSet rs = stmt.executeQuery(query);
  91.                         if(rs.next())
  92.                         {
  93.                             z = "Uspesne prihlasen";
  94.                             isSuccess=true;
  95.                             con.close();
  96.                         }
  97.                         else
  98.                         {
  99.                             z = "Spatne udaje!";
  100.                             isSuccess = false;
  101.                         }
  102.                     }
  103.                 }
  104.                 catch (Exception ex)
  105.                 {
  106.                     isSuccess = false;
  107.                     z = ex.getMessage();
  108.                 }
  109.             }
  110.             return z;
  111.         }
  112.     }
  113.     @SuppressLint("NewApi")
  114.     public Connection connectionclass(String user, String password, String database, String server)
  115.     {
  116.         StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  117.         StrictMode.setThreadPolicy(policy);
  118.         Connection connection = null;
  119.         String ConnectionURL = null;
  120.         try
  121.         {
  122.             Class.forName("net.sourceforge.jtds.jdbc.Driver");
  123.             ConnectionURL = "jdbc:jtds:sqlserver://" + server + database + ";user=" + user+ ";password=" + password + ";";
  124.             connection = DriverManager.getConnection(ConnectionURL);
  125.         }
  126.         catch (SQLException se)
  127.         {
  128.             Log.e("error here 1 : ", se.getMessage());
  129.         }
  130.         catch (ClassNotFoundException e)
  131.         {
  132.             Log.e("error here 2 : ", e.getMessage());
  133.         }
  134.         catch (Exception e)
  135.         {
  136.             Log.e("error here 3 : ", e.getMessage());
  137.         }
  138.         return connection;
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement