Advertisement
Guest User

Untitled

a guest
Aug 6th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. package com.example.dan.tictactoe;
  2.  
  3.  
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.os.StrictMode;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.support.v7.widget.Toolbar;
  9. import android.view.View;
  10. import android.view.Menu;
  11. import android.view.MenuItem;
  12. import android.view.animation.Animation;
  13. import android.view.animation.AnimationUtils;
  14. import android.widget.Button;
  15. import android.widget.EditText;
  16. import android.widget.TextView;
  17. import android.content.Intent;
  18.  
  19.  
  20. import java.sql.Connection;
  21. import java.sql.DriverManager;
  22. import java.sql.ResultSet;
  23. import java.sql.Statement;
  24.  
  25.  
  26. public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
  27.  
  28. String dbUsername = "user_db_1423961_AndroidGame";
  29. String dbPassword = "Hellomate8";
  30. String connString = "jdbc:jtds:sqlserver://SQL2014.studentwebserver.co.uk";
  31. Statement stmt;
  32.  
  33. EditText username;
  34. EditText password;
  35. Button butLog;
  36. Button butReg;
  37.  
  38. protected void onCreate(Bundle savedInstanceState) {
  39. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  40. StrictMode.setThreadPolicy(policy);
  41. super.onCreate(savedInstanceState);
  42. setContentView(R.layout.activity_login);
  43.  
  44. username = (EditText) findViewById(R.id.editUsername);
  45. password = (EditText) findViewById(R.id.editPassword);
  46. butLog = (Button) findViewById(R.id.but_login);
  47. butReg = (Button) findViewById(R.id.but_Reg);
  48.  
  49. connectToDb();
  50.  
  51. butLog.setOnClickListener(this);
  52. checkPasswordDb();
  53. {
  54. switch (butLog.getId()) {
  55. case R.id.but_login:
  56.  
  57. break;
  58. }
  59.  
  60.  
  61. }
  62.  
  63. butReg.setOnClickListener(this) ;
  64. {
  65. switch (butReg.getId()) {
  66. case R.id.but_Reg:
  67. addUser();
  68. break;
  69. }
  70.  
  71.  
  72. }
  73.  
  74. }
  75.  
  76.  
  77. public void onClick(View v) {
  78. if (checkPasswordDb()) {
  79. Intent i = new Intent(this, MainActivity.class);
  80. startActivity(i);
  81.  
  82. }
  83.  
  84.  
  85. }
  86.  
  87.  
  88.  
  89. private void connectToDb(){
  90. checkPasswordDb();
  91. try {
  92. Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
  93. Connection DbConn = DriverManager.getConnection(connString, dbUsername, dbPassword);
  94. stmt = DbConn.createStatement();
  95. } catch (Exception e) {
  96. } }
  97.  
  98.  
  99.  
  100.  
  101. public boolean addUser()
  102.  
  103. {
  104. String sql = "INSERT INTO Users VALUES( '" + username.getText().toString() + "','" +
  105. password.getText().toString() + "')";
  106. try {
  107. stmt.executeUpdate(sql);
  108. return true;
  109. } catch (Exception e) {
  110. return false;
  111. }
  112. }
  113.  
  114. public boolean checkPasswordDb() {
  115. String sql;
  116. sql = "SELECT * FROM Users WHERE username = '" + username.getText().toString() + "'";
  117. try {
  118. ResultSet rst = stmt.executeQuery(sql);
  119. rst.next();
  120. if(rst.getString("Password").equals(password.getText().toString()))
  121. return true;
  122. else
  123. return false;
  124. } catch (Exception e) {
  125. return false;
  126. }
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement