Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- ConnectionClass connectionClass;
- EditText edtuserid,edtpass;
- Button btnlogin;
- ProgressBar pbbar;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- connectionClass = new ConnectionClass();
- edtuserid = (EditText) findViewById(R.id.edtuserid);
- edtpass = (EditText) findViewById(R.id.edtpass);
- btnlogin = (Button) findViewById(R.id.btnlogin);
- pbbar = (ProgressBar) findViewById(R.id.pbbar);
- pbbar.setVisibility(View.GONE);
- btnlogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- DoLogin doLogin = new DoLogin();
- doLogin.execute("");
- }
- });
- }
- public class DoLogin extends AsyncTask<String,String,String>
- {
- String z = "";
- Boolean isSuccess = false;
- String userid = edtuserid.getText().toString();
- String password = edtpass.getText().toString();
- @Override
- protected void onPreExecute() {
- pbbar.setVisibility(View.VISIBLE);
- }
- @Override
- protected void onPostExecute(String r) {
- pbbar.setVisibility(View.GONE);
- Toast.makeText(MainActivity.this,r,Toast.LENGTH_SHORT).show();
- if(isSuccess) {
- Intent i = new Intent(MainActivity.this, attendance.class);
- startActivity(i);
- finish();
- }
- }
- @Override
- protected String doInBackground(String... params) {
- if(userid.trim().equals("")|| password.trim().equals(""))
- z = "Please enter User Id and Password";
- else
- {
- try {
- Connection con = connectionClass.CONN();
- if (con == null) {
- z = "Error in connection with The server";
- } else {
- String query = "select * from tablename where id='" + userid + "' and Password='" + password + "'";
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery(query);
- if(rs.next())
- {
- z = "Login successfull ";
- isSuccess=true;
- }
- else
- {
- z = "Invalid Credentials";
- isSuccess = false;
- }
- }
- }
- catch (Exception ex)
- {
- isSuccess = false;
- z = "Exceptions";
- }
- }
- return z;
- }
- }
- public class ConnectionClass {
- String ip = "192.168.....etc";
- String classs = "net.sourceforge.jtds.jdbc.Driver";
- String db = "dbname";
- String un = "sql server login username";
- String password = "*****";
- @SuppressLint("NewApi")
- public Connection CONN() {
- StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
- .permitAll().build();
- StrictMode.setThreadPolicy(policy);
- Connection conn = null;
- String ConnURL = null;
- try {
- Class.forName(classs);
- ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
- + "databaseName=" + db + ";user=" + un + ";password="
- + password + ";";
- conn = DriverManager.getConnection(ConnURL);
- } catch (SQLException se) {
- Log.e("ERRO", se.getMessage());
- } catch (ClassNotFoundException e) {
- Log.e("ERRO", e.getMessage());
- } catch (Exception e) {
- Log.e("ERRO", e.getMessage());
- }
- return conn;
- }
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
- <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
- <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
Add Comment
Please, Sign In to add comment