Database Connectivity of Android with MYSQL DATABASE , WAMP SERVER/XAMP SERVER using Eclipse or Android Studio -- --Login Page using MYSQL database
Step-1 : First of all we need to make a login screen design. for that we make on Activity_main.xml file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="18dp"
android:text="@string/Login_details"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="66dp"
android:text="@string/username"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="31dp"
android:text="@string/password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/editText2"
android:layout_marginTop="61dp"
android:text="@string/login_btn"
android:onClick="OnLogin"
/>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/button1"
android:layout_marginLeft="24dp"
android:ems="10" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/editText1"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
</RelativeLayout>
<?php
<?php
require "logindemo_mysql.php";
$user_name = $_POST["user_name"];
$user_pass = $_POST["password"];
$mysql_qry = "select * from login_details where username like '$user_name' and password like'$user_pass'";
$result = mysqli_query($conn,$mysql_qry);
if(mysqli_num_rows($result) > 0)
{
echo"login success";
}
else
{
echo"login not success";
}
?>
package com.example.logindemo_mysql;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText uname,pword;
//Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uname= (EditText) findViewById(R.id.editText1);
pword= (EditText) findViewById(R.id.editText2);
//b1= (Button)findViewById(R.id.button1);
}
public void OnLogin(View view)
{
String username= uname.getText().toString();
String password= pword.getText().toString();
String Type= "login";
BackgroundWorker backgroundworker= new BackgroundWorker(this);
backgroundworker.execute(Type,username,password);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
OUTPUT:-
Step-1 : First of all we need to make a login screen design. for that we make on Activity_main.xml file.
Activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="18dp"
android:text="@string/Login_details"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="66dp"
android:text="@string/username"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="31dp"
android:text="@string/password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/editText2"
android:layout_marginTop="61dp"
android:text="@string/login_btn"
android:onClick="OnLogin"
/>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/button1"
android:layout_marginLeft="24dp"
android:ems="10" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/editText1"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
</RelativeLayout>
- After Design the login screen we need to make database table in mysql database. for that we need to download WAMPSERVER and in wamp server, open phpmyadmin and in phpmyadmin create a databse and in database make one table as screen shows u phpmyadmin screen.
- After creating database make one php file using notepad++ software.
- Open following path and save 2 file on this location. C:\wamp\www .
- in 1st file save name logindemo_mysql.php .. in this file write following code--->>
<?php
$db_name="demologin";
$mysql_username="root";
$mysql_password="";
$server_name="localhost";
$conn= mysqli_connect($server_name,$mysql_username,$mysql_password,$db_name);
?>
- In 2nd file save name logindemo_mysql1.php .. in this file write following code --->>
require "logindemo_mysql.php";
$user_name = $_POST["user_name"];
$user_pass = $_POST["password"];
$mysql_qry = "select * from login_details where username like '$user_name' and password like'$user_pass'";
$result = mysqli_query($conn,$mysql_qry);
if(mysqli_num_rows($result) > 0)
{
echo"login success";
}
else
{
echo"login not success";
}
?>
- Now in Android Studio open mainactivity.java file and paste following code in it.
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText uname,pword;
//Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uname= (EditText) findViewById(R.id.editText1);
pword= (EditText) findViewById(R.id.editText2);
//b1= (Button)findViewById(R.id.button1);
}
public void OnLogin(View view)
{
String username= uname.getText().toString();
String password= pword.getText().toString();
String Type= "login";
BackgroundWorker backgroundworker= new BackgroundWorker(this);
backgroundworker.execute(Type,username,password);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
- After that make on Backgroundworker.java file in src folder.. in this file write following code for database connectivity. --->>
package com.example.logindemo_mysql;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
public class BackgroundWorker extends AsyncTask<String, Void,String>{
Context context;
AlertDialog alertDialog;
BackgroundWorker(Context ctx){
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String type= params[0];
String login_url="http://10.0.2.2/logindemo_mysql1.php";
if(type.equals("login"))
{
try{
String user_name= params[1];
String password= params[2];
URL url= new URL(login_url);
HttpURLConnection httpURLConnection= (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream=httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String Post_data=URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8") ;
bufferedWriter.write(Post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream=httpURLConnection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine())!= null)
{
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
}catch(MalformedURLException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
alertDialog= new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
alertDialog.setMessage(result);
alertDialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
}
2 Comments
Nice Blog, When i was read this blog i learnt new things & its truly have well stuff related to developing technology, Thank you for sharing this blog.
ReplyDeleteiPhone app training course in bangalore
iPhone job oriented course in bangalore
Thanks for sharing the useful blog about Database Connectivity in Android.
ReplyDeleteiOS Application Development Company in Coimbatore