Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.bartosz.eagleeye;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.net.Uri;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.support.v4.app.ActivityCompat;
- import android.support.v4.content.ContextCompat;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class MainActivity extends AppCompatActivity {
- private Toolbar mToolbar;
- Button button;
- EditText pswdText;
- String pswd;
- int logedGlobal = 0;
- private static final int REQUEST_FINE_LOCATION = 0;
- String fullname = "";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.login_layout);
- mToolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(mToolbar);
- getSupportActionBar().setDisplayShowHomeEnabled(true);
- addListenerOnButton();
- pswdText = (EditText) findViewById(R.id.passwordText);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- Toast.makeText(getApplicationContext(), "SETTINGS BUTTON", Toast.LENGTH_SHORT).show();
- return true;
- }
- if (id == R.id.action_search) {
- Toast.makeText(getApplicationContext(), "SEARCH BUTTON", Toast.LENGTH_SHORT).show();
- }
- return super.onOptionsItemSelected(item);
- }
- public void addListenerOnButton() {
- button = (Button) findViewById(R.id.buttonLogin);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View arg0) {
- pswd = pswdText.getText().toString();
- new ConnectionDB2().execute();
- }
- });
- }
- public void CheckPswd(){
- if(logedGlobal==1) {
- Intent myIntent = new Intent(MainActivity.this, GpsActivity.class);
- myIntent.putExtra("key", pswdText.getText().toString()); //Optional parameters
- myIntent.putExtra("name", fullname); //Optional parameters
- MainActivity.this.startActivity(myIntent);
- finish();
- }
- }
- public class ConnectionDB2 extends AsyncTask<String, Integer, String> {
- String localpswd;
- String resultText;
- int loged = 0;
- @Override
- protected void onPreExecute() {
- localpswd = pswd;
- }
- @Override
- protected String doInBackground(String... f_url) {
- // JDBC driver name and database URL
- String JDBC_DRIVER = "net.sourceforge.jtds.jdbc.Driver";
- String DB_URL = "jdbc:jtds:sqlserver://192.168.1.129:1433;DatabaseName=Mobile";
- // Database credentials
- String USER = "admin1";
- String PASS = "123";
- Connection conn = null;
- try {
- //STEP 2: Register JDBC driver
- Class.forName("net.sourceforge.jtds.jdbc.Driver");
- //STEP 3: Open a connection
- System.out.println("Connecting to database...---------------------------------------------------------------");
- conn = DriverManager.getConnection(DB_URL, USER, PASS);
- //STEP 4: Execute a query
- System.out.println("Creating statement...--------------------------------------------------------------");
- Statement stmt = conn.createStatement();
- String sql;
- sql = "SELECT * FROM Pracownik Where id_pracownika ='" + localpswd + "'";
- ResultSet rs = stmt.executeQuery(sql);
- //STEP 5: Extract data from result set
- if (!rs.isBeforeFirst()) {
- System.out.println("NO DATA ----------------------- pswd incorrect");
- resultText = "Password incorrect";
- } else {
- while (rs.next()) {
- String last = rs.getString("nazwisko");
- String imie = rs.getString("imie");
- //Display values
- System.out.println(", nazwisko: " + last);
- resultText = "Witaj," + last;
- loged = 1;
- fullname =imie+" "+last;
- return last;
- }
- }
- //STEP 6: Clean-up environment
- rs.close();
- stmt.close();
- conn.close();
- } catch (SQLException se) {
- //Handle errors for JDBC
- se.printStackTrace();
- return "//Handle errors for JDBC";
- } catch (Exception e) {
- //Handle errors for Class.forName
- e.printStackTrace();
- return "//Handle errors for Class.forName";
- } finally {
- //finally block used to close resources
- try {
- if (conn != null)
- conn.close();
- } catch (SQLException se) {
- se.printStackTrace();
- }//end finally try
- }//end try
- System.out.println("Goodbye!");
- return "err";
- }
- @Override
- protected void onProgressUpdate(Integer... progress) {
- }
- @Override
- protected void onPostExecute(String result) {
- logedGlobal=loged;
- CheckPswd();
- //lol
- Toast.makeText(getApplicationContext(), resultText, Toast.LENGTH_SHORT).show();
- }
- }
- private void loadPermissions(String perm, int requestCode) {
- if (ContextCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) {
- if (!ActivityCompat.shouldShowRequestPermissionRationale(this, perm)) {
- ActivityCompat.requestPermissions(this, new String[]{perm}, requestCode);
- }
- }
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
- switch (requestCode) {
- case REQUEST_FINE_LOCATION: {
- // If request is cancelled, the result arrays are empty.
- if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- // granted
- } else {
- // no granted
- }
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement