Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pepapadua.whatslearn1;
- import android.annotation.SuppressLint;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ProgressBar;
- import android.widget.Toast;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import android.os.StrictMode;
- /**
- * Created by pepap on 1/7/2018.
- */
- public class LoginActivity extends AppCompatActivity {
- Connection con;
- Button login = findViewById(R.id.email_sign_in_button);
- EditText cislo_tridy = findViewById(R.id.classid);
- EditText uz_jmeno = findViewById(R.id.user_name);
- EditText pass = findViewById(R.id.password);
- ProgressBar prgbar = findViewById(R.id.login_progress);
- String ip = "db.mysql-01.gsp-europe.net";
- String dn = "sql_4564";
- String psw = "wCYjYvVCyB57nocZ3wYNPs6f00d2h9A";
- String table = "wl_users";
- @Override
- protected void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- prgbar.setVisibility(View.GONE);
- login.setOnClickListener(new View.OnClickListener(){
- @Override
- public void onClick (View v){
- CheckLogin checkLogin = new CheckLogin();// this is the Asynctask, which is used to process in background to reduce load on app process
- checkLogin.execute("");
- }
- });
- //End Setting up the function when button login is clicked
- }
- public class CheckLogin extends AsyncTask<String,String,String> {
- String z = "";
- Boolean isSuccess = false;
- @Override
- protected void onPreExecute() {
- prgbar.setVisibility(View.VISIBLE);
- }
- @Override
- protected void onPostExecute(String r) {
- prgbar.setVisibility(View.GONE);
- Toast.makeText(LoginActivity.this, r, Toast.LENGTH_SHORT).show();
- if(isSuccess) {
- Toast.makeText(LoginActivity.this , "Uspesne prihlasen" , Toast.LENGTH_LONG).show();
- //finish();
- }
- }
- @Override
- protected String doInBackground(String... params) {
- String classka = cislo_tridy.getText().toString();
- String usernam = uz_jmeno.getText().toString();
- String passwordd = pass.getText().toString();
- if(usernam.trim().equals("")|| passwordd.trim().equals("")|| classka.trim().equals(""))
- z = "Vlozte prosim cislo tridy, uzivatelske id a heslo";
- else {
- try {
- con = connectionclass(dn, psw, dn, ip); // Connect to database
- if (con == null) {
- z = "Nelze pripojit k serveru!";
- }
- else {
- // Change below query according to your own database.
- String query = "SELECT * FROM `wl_users` WHERE `user_name` " + uz_jmeno.toString() + " AND `password " + pass.toString() + " AND `class_number` "+cislo_tridy.toString();
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery(query);
- if(rs.next())
- {
- z = "Uspesne prihlasen";
- isSuccess=true;
- con.close();
- }
- else
- {
- z = "Spatne udaje!";
- isSuccess = false;
- }
- }
- }
- catch (Exception ex)
- {
- isSuccess = false;
- z = ex.getMessage();
- }
- }
- return z;
- }
- }
- @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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement