Advertisement
Guest User

Untitled

a guest
Apr 16th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. compile files('libs/jtds-1.2.7.jar')
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.os.StrictMode;
  5. import android.util.Log;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.SQLException;
  9.  
  10.  
  11. public class Connect_to_server {
  12.  
  13.  
  14. @SuppressLint("NewApi")
  15. public static Connection ConnectionHelper() {
  16. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
  17. .permitAll().build();
  18. StrictMode.setThreadPolicy(policy);
  19. Connection connection = null;
  20. String ConnectionURL = null;
  21.  
  22. String user = ***LOGIN_HERE***;
  23. String password = ***PASSWORD_HERE***;
  24. String database = ***DATABASE_NAME_HERE***;
  25. String server= ***SERVER_NAME_HERE***;
  26.  
  27. try {
  28. Class.forName("net.sourceforge.jtds.jdbc.Driver");
  29. ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";"
  30. + "databaseName=" + database + ";user=" + user
  31. + ";password=" + password + ";";
  32. connection = DriverManager.getConnection(ConnectionURL);
  33. } catch (SQLException se) {
  34. Log.e("ERRO", se.getMessage());
  35. } catch (ClassNotFoundException e) {
  36. Log.e("ERRO", e.getMessage());
  37. } catch (Exception e) {
  38. Log.e("ERRO", e.getMessage());
  39. }
  40. return connection;
  41. }
  42.  
  43. }
  44.  
  45. Connection connect;
  46. Statement st;
  47. Statement st1;
  48. ResultSet rs;
  49.  
  50.  
  51. btn_Register.setOnClickListener(new View.OnClickListener() {
  52. @Override
  53. public void onClick(View view) {
  54.  
  55.  
  56. try {
  57. connect = Connect_to_server.ConnectionHelper();
  58. st = connect.createStatement();
  59.  
  60. // TODO i Will take ID_Admin from here
  61. rs = st.executeQuery(" SELECT * FROM [dbo].[AdminUsers] WHERE Email = N'"+ET_email.getText()+"' ");
  62.  
  63. if (rs.next()){
  64.  
  65. Toast.makeText(getApplicationContext(),rs.getString("Email")+"this email in database",LENGTH_LONG).show();
  66. Intent i = new Intent(RegisterActivity.this , LoginActivity.class);
  67. startActivity(i);
  68. finish();
  69.  
  70.  
  71. }else{
  72.  
  73.  
  74. reg();
  75.  
  76.  
  77. }
  78.  
  79.  
  80.  
  81. } catch (Exception e) {
  82.  
  83. Log.e("ERRO", e.getMessage());
  84. }
  85.  
  86. }
  87. });
  88.  
  89.  
  90. private void reg(){
  91.  
  92. try {
  93. connect = Connect_to_server.ConnectionHelper();
  94. st1 = connect.createStatement();
  95.  
  96. st1.execute("INSERT INTO [dbo].[AdminUsers]" +
  97. " ([UserAdmin]" +
  98. " ,[PassAdmin]" +
  99. " ,[Address]" +
  100. " ,[Phone]" +
  101. " ,[Email])" +
  102. " VALUES" +
  103. " (N'"+ET_name.getText()+"'" +
  104. " ,N'"+ET_password.getText() +"'" +
  105. " ,N'"+ET_address.getText()+"'" +
  106. " ,N'"+ET_phone.getText()+"'" +
  107. " ,N'"+ET_email.getText()+"' )");
  108.  
  109.  
  110.  
  111. Toast.makeText(getApplicationContext(),ET_name.getText()+"welcome ",LENGTH_LONG).show();
  112.  
  113. Toast.makeText(getApplicationContext(),"u can login now",LENGTH_LONG).show();
  114. Intent i = new Intent(RegisterActivity.this , LoginActivity.class);
  115. startActivity(i);
  116. finish();
  117.  
  118.  
  119. } catch (Exception e) {
  120.  
  121. Log.e("ERRO", e.getMessage());
  122. Log.e("ERRO", e.getMessage());
  123. }
  124.  
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement