Advertisement
Guest User

Untitled

a guest
Nov 8th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. EditText username1;
  2. EditText password1;
  3. TextView status;
  4. ResultSet rs;
  5.  
  6. //private static final String url = "jdbc:mysql://10.0.2.2:3306/tesina";
  7. private static final String url = "jdbc:mysql://192.168.1.10:3306/tesina";
  8. private static final String user = "root";
  9. private static final String pass = "";
  10.  
  11. /** Called when the activity is first created. */
  12. @Override
  13. public void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. username1 = (EditText) findViewById(R.id.user);
  17. password1 = (EditText) findViewById(R.id.pass);
  18.  
  19. //status =(TextView) findViewById(R.id.login_error);
  20. Button btnLogin=(Button) findViewById(R.id.btnlogin);
  21. btnLogin.setOnClickListener(new View.OnClickListener() {
  22.  
  23. public void onClick(View view) {
  24.  
  25.  
  26. new Connessione().execute();
  27.  
  28. }
  29. });
  30. }
  31.  
  32. private class Connessione extends AsyncTask<String, String, Void> {
  33.  
  34. @Override
  35. protected Void doInBackground(String... String) {
  36. String us = username1.getText().toString();
  37.  
  38.  
  39.  
  40.  
  41. try {
  42.  
  43. Class.forName("com.mysql.jdbc.Driver");
  44. Connection con = DriverManager.getConnection(url, user, pass);
  45. Statement stmt = con.createStatement();
  46. rs = (ResultSet) stmt.executeQuery("SELECT * FROM prova WHERE nome='"+us+"'");
  47. }
  48. catch(Exception e) {
  49. e.printStackTrace();
  50.  
  51. }
  52. return null;
  53. }
  54. protected void onProgressUpdate(String... values) {
  55.  
  56. }
  57.  
  58.  
  59. protected void onPostExecute(String result) {
  60.  
  61. String p = password1.getText().toString();
  62. try {
  63.  
  64. if(rs.next())
  65. {
  66. String dbpass = rs.getString(2);
  67. if(dbpass.equals(p))
  68. {
  69.  
  70. Intent i = new Intent(MainActivity.this,classi.class);
  71. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  72. startActivity(i);
  73. finish();
  74.  
  75. }
  76. else
  77. {
  78.  
  79. Toast.makeText(getBaseContext(), "errore: password non inserita correttamente", Toast.LENGTH_LONG).show();
  80.  
  81.  
  82. }
  83. }
  84. else
  85. {
  86. Toast.makeText(getBaseContext(), "errore:username non trovato", Toast.LENGTH_LONG).show();
  87.  
  88.  
  89. }
  90. } catch (SQLException e) {
  91. // TODO Auto-generated catch block
  92. e.printStackTrace();
  93. Toast.makeText(getBaseContext(), "applicazione non collegata al database della scuola", Toast.LENGTH_LONG).show();
  94. }
  95.  
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement