Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- EditText edtuserid,edtpass;
- Button btnlogin;
- ProgressBar pbbar;
- Connection con;
- //String un,pass,db,ip; DESKTOP-L9PBLVQSQLEXPRESS
- String ip = "192.168.0.121";
- String server="DESKTOP-L9PBLVQ\SQLEXPRESS";
- String classs = "net.sourceforge.jtds.jdbc.Driver";
- String db = "AndroidTry";
- String un = "";
- String password = "";
- @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);
- String ip = "192.168.0.121";
- String server="DESKTOP-L9PBLVQ\SQLEXPRESS";
- String classs = "net.sourceforge.jtds.jdbc.Driver";
- String db = "AndroidTry";
- String un = "";
- String password = "";
- //String url ="jdbc:sqlserver://PC01inst01;databaseName=AndroidTry;integratedSecurity=true"; localhost:4200
- 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, SecondActivity.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 {
- con = connectionclass(db, server);
- //Connection con = connectionClass.CONN();
- if (con == null) {
- z = "Error in connection with SQL server";
- } else {
- String query = "select * from Login where UserName='" + userid + "' and Password='" + password + "'";
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery(query);
- if(rs.next())
- {
- z = "Login successful";
- isSuccess=true;
- }
- else
- {
- z = "Invalid Credentials";
- isSuccess = false;
- }
- }
- }
- catch (Exception ex)
- {
- isSuccess = false;
- z = "Exceptions";
- }
- }
- return z;
- }
- }
- @SuppressLint("NewApi")
- public Connection connectionclass(String database, String server)
- {
- StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
- StrictMode.setThreadPolicy(policy);
- Connection connection=null;
- String ConnectionURL;
- try
- {
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- Class.forName("net.sourceforge.jtds.jdbc.Driver");
- ConnectionURL = "jdbc:sqlserver://"+ server+";" +"databasename="+ database+";" + "integratedSecurity=true"+";";
- connection = DriverManager.getConnection(ConnectionURL);
- }
- catch (SQLException se)
- {
- Log.e("error here 1 : ", se.getMessage());
- }
- catch (ClassNotFoundException e)
- {
- Log.e("error here 2 : ", e.getMessage());
- }
- catch (Exception e)
- {
- Log.e("error here 3 : ", e.getMessage());
- }
- return connection;
- }
- }
Add Comment
Please, Sign In to add comment