Advertisement
Guest User

Untitled

a guest
May 10th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.52 KB | None | 0 0
  1. package com.example.bartosz.eagleeye;
  2.  
  3. import android.content.Intent;
  4. import android.content.pm.PackageManager;
  5. import android.net.Uri;
  6. import android.os.AsyncTask;
  7. import android.os.Bundle;
  8. import android.support.v4.app.ActivityCompat;
  9. import android.support.v4.content.ContextCompat;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.support.v7.widget.Toolbar;
  12. import android.view.Menu;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.widget.Button;
  16. import android.widget.EditText;
  17. import android.widget.TextView;
  18. import android.widget.Toast;
  19.  
  20. import java.sql.Connection;
  21. import java.sql.DriverManager;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.sql.Statement;
  25.  
  26. public class MainActivity extends AppCompatActivity {
  27.  
  28. private Toolbar mToolbar;
  29. Button button;
  30.  
  31. EditText pswdText;
  32. String pswd;
  33. int logedGlobal = 0;
  34. private static final int REQUEST_FINE_LOCATION = 0;
  35. String fullname = "";
  36.  
  37. @Override
  38. protected void onCreate(Bundle savedInstanceState) {
  39. super.onCreate(savedInstanceState);
  40. setContentView(R.layout.login_layout);
  41.  
  42. mToolbar = (Toolbar) findViewById(R.id.toolbar);
  43.  
  44. setSupportActionBar(mToolbar);
  45. getSupportActionBar().setDisplayShowHomeEnabled(true);
  46. addListenerOnButton();
  47.  
  48.  
  49. pswdText = (EditText) findViewById(R.id.passwordText);
  50. }
  51.  
  52.  
  53. @Override
  54. public boolean onCreateOptionsMenu(Menu menu) {
  55. // Inflate the menu; this adds items to the action bar if it is present.
  56. getMenuInflater().inflate(R.menu.menu_main, menu);
  57. return true;
  58. }
  59.  
  60. @Override
  61. public boolean onOptionsItemSelected(MenuItem item) {
  62. // Handle action bar item clicks here. The action bar will
  63. // automatically handle clicks on the Home/Up button, so long
  64. // as you specify a parent activity in AndroidManifest.xml.
  65. int id = item.getItemId();
  66.  
  67. //noinspection SimplifiableIfStatement
  68. if (id == R.id.action_settings) {
  69. Toast.makeText(getApplicationContext(), "SETTINGS BUTTON", Toast.LENGTH_SHORT).show();
  70. return true;
  71. }
  72. if (id == R.id.action_search) {
  73. Toast.makeText(getApplicationContext(), "SEARCH BUTTON", Toast.LENGTH_SHORT).show();
  74. }
  75. return super.onOptionsItemSelected(item);
  76. }
  77.  
  78. public void addListenerOnButton() {
  79.  
  80. button = (Button) findViewById(R.id.buttonLogin);
  81.  
  82. button.setOnClickListener(new View.OnClickListener() {
  83.  
  84. @Override
  85. public void onClick(View arg0) {
  86.  
  87. pswd = pswdText.getText().toString();
  88. new ConnectionDB2().execute();
  89.  
  90.  
  91.  
  92. }
  93.  
  94. });
  95.  
  96. }
  97. public void CheckPswd(){
  98.  
  99.  
  100. if(logedGlobal==1) {
  101. Intent myIntent = new Intent(MainActivity.this, GpsActivity.class);
  102. myIntent.putExtra("key", pswdText.getText().toString()); //Optional parameters
  103. myIntent.putExtra("name", fullname); //Optional parameters
  104. MainActivity.this.startActivity(myIntent);
  105. finish();
  106. }
  107. }
  108.  
  109. public class ConnectionDB2 extends AsyncTask<String, Integer, String> {
  110. String localpswd;
  111. String resultText;
  112. int loged = 0;
  113.  
  114. @Override
  115. protected void onPreExecute() {
  116. localpswd = pswd;
  117. }
  118.  
  119. @Override
  120. protected String doInBackground(String... f_url) {
  121. // JDBC driver name and database URL
  122. String JDBC_DRIVER = "net.sourceforge.jtds.jdbc.Driver";
  123. String DB_URL = "jdbc:jtds:sqlserver://192.168.1.129:1433;DatabaseName=Mobile";
  124.  
  125. // Database credentials
  126. String USER = "admin1";
  127. String PASS = "123";
  128.  
  129.  
  130. Connection conn = null;
  131. try {
  132. //STEP 2: Register JDBC driver
  133. Class.forName("net.sourceforge.jtds.jdbc.Driver");
  134.  
  135. //STEP 3: Open a connection
  136. System.out.println("Connecting to database...---------------------------------------------------------------");
  137. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  138.  
  139. //STEP 4: Execute a query
  140. System.out.println("Creating statement...--------------------------------------------------------------");
  141. Statement stmt = conn.createStatement();
  142. String sql;
  143. sql = "SELECT * FROM Pracownik Where id_pracownika ='" + localpswd + "'";
  144. ResultSet rs = stmt.executeQuery(sql);
  145.  
  146. //STEP 5: Extract data from result set
  147. if (!rs.isBeforeFirst()) {
  148. System.out.println("NO DATA ----------------------- pswd incorrect");
  149. resultText = "Password incorrect";
  150.  
  151. } else {
  152. while (rs.next()) {
  153.  
  154. String last = rs.getString("nazwisko");
  155. String imie = rs.getString("imie");
  156. //Display values
  157.  
  158. System.out.println(", nazwisko: " + last);
  159. resultText = "Witaj," + last;
  160. loged = 1;
  161. fullname =imie+" "+last;
  162. return last;
  163. }
  164. }
  165. //STEP 6: Clean-up environment
  166. rs.close();
  167. stmt.close();
  168. conn.close();
  169. } catch (SQLException se) {
  170. //Handle errors for JDBC
  171. se.printStackTrace();
  172. return "//Handle errors for JDBC";
  173. } catch (Exception e) {
  174. //Handle errors for Class.forName
  175. e.printStackTrace();
  176. return "//Handle errors for Class.forName";
  177. } finally {
  178. //finally block used to close resources
  179. try {
  180. if (conn != null)
  181. conn.close();
  182. } catch (SQLException se) {
  183. se.printStackTrace();
  184. }//end finally try
  185. }//end try
  186. System.out.println("Goodbye!");
  187. return "err";
  188. }
  189.  
  190. @Override
  191. protected void onProgressUpdate(Integer... progress) {
  192. }
  193.  
  194. @Override
  195. protected void onPostExecute(String result) {
  196.  
  197. logedGlobal=loged;
  198. CheckPswd();
  199. //lol
  200. Toast.makeText(getApplicationContext(), resultText, Toast.LENGTH_SHORT).show();
  201. }
  202.  
  203.  
  204. }
  205.  
  206.  
  207. private void loadPermissions(String perm, int requestCode) {
  208. if (ContextCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) {
  209. if (!ActivityCompat.shouldShowRequestPermissionRationale(this, perm)) {
  210. ActivityCompat.requestPermissions(this, new String[]{perm}, requestCode);
  211. }
  212. }
  213. }
  214.  
  215. @Override
  216. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  217. switch (requestCode) {
  218. case REQUEST_FINE_LOCATION: {
  219. // If request is cancelled, the result arrays are empty.
  220. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  221. // granted
  222. } else {
  223. // no granted
  224. }
  225. return;
  226. }
  227.  
  228. }
  229.  
  230. }
  231.  
  232.  
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement