Tutorial Mysql Android Eclipse
Tutorial Mysql Android Eclipse
Buatlah database dengan nama db_universitas dan table mahasiswa dengan field sebagai
berikut :
Untuk beriikutnya buatlah project (folder) di htdocs dengan nama mahasiswa dan
tambahkan file-file berikut :
1. db_connect.php
<?php
2. getAllMahasiswa.php
<?php
$response = array();
if (mysqli_num_rows($result) > 0) {
$response["mahasiswa"] = array();
array_push($response["mahasiswa"], $mahasiswa);
}
$response["success"] = 1;
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "Tidak ada data yang ditemukan";
echo json_encode($response);
}
?>
3. get_mahasiswa_details.php
<?php
$response = array();
if (isset($_GET["id"]))
{
$id = $_GET['id'];
$response["success"] = 1;
$response["mahasiswa"] = array();
array_push($response["mahasiswa"], $mahasiswa);
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "Tidak ada data";
echo json_encode($response);
}
} else {
$response["success"] = 0;
$response["message"] = "Tidak ada data";
echo json_encode($response);
}
} else {
$response["success"] = 0;
$response["message"] = "Silahkan lengkapi permintaan anda";
echo json_encode($response);
}
?>
4. create_mahasiswa.php
<?php
$response = array();
$npm = $_POST['npm'];
$nama = $_POST['nama'];
$result = $connect-
>query("INSERT INTO mahasiswa(npm, nama, kelas, alamat) VALUES ('$npm
', '$nama', '$kelas', '$alamat')");
if ($result) {
$response["success"] = 1;
$response["message"] = "membuat data berhasil";
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "Sistem mendeteksi kesalahan, silahkan
coba lagi";
echo json_encode($response);
}
} else {
$response["success"] = 0;
$response["message"] = "Silahkan lengkapi aksi sebelum memulai pe
rmintaan anda";
echo json_encode($response);
}
?>
5. update_mahasiswa.php
<?php
$response = array();
$id = $_POST['id'];
$npm = $_POST['npm'];
$nama = $_POST['nama'];
$kelas = $_POST['kelas'];
$alamat = $_POST['alamat'];
$result = $connect-
>query("UPDATE mahasiswa SET npm = '$npm', nama = '$nama', kelas = '$
kelas', alamat = '$alamat' WHERE id = $id");
if ($result)
{
$response["success"] = 1;
$response["message"] = "Data anda berhasil di perbarui";
echo json_encode($response);
}
else
{
}
}
else
{
$response["success"] = 0;
$response["message"] = "Mohon kelengkapan data anda";
echo json_encode($response);
}
?>
6. delete_mahasiswa.php
<?php
$response = array();
if (isset($_POST['id']))
{
$id = $_POST['id'];
$result = $connect-
>query("DELETE FROM mahasiswa WHERE id = $id");
if ($connect->affected_rows() > 0)
{
$response["success"] = 1;
echo json_encode($response);
}
else
{
$response["success"] = 0;
$response["message"] = "Tidak ada data yang tersedia";
echo json_encode($response);
}
}
else
{
$response["success"] = 0;
$response["message"] = "Aksi tidak bisa di lakukan";
echo json_encode($response);
}
?>
Untuk mengeceknya apakah sudah benar koding yang dibuat diphp, kita bisa mengeceknya
dengan menginstall postman pada web berikut. https://www.getpostman.com/
Berikutnya konfigurasilah handphone anda dengan membuat thetering USB dan check IP
yang didapatkan pada komputer. Buka cmd pada komputer dan ketikkan ipconfig.
Buatlah Activity dan Layout baru dengan cara Klik Kanan Project – New – Other – Android
Activity – Empty Activity
Buat Activity Baru Seperti pada step sebelumnya dan pada popup menu masukkan isian
seperti berikut :
<TextView
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_above="@+id/button1"
android:layout_alignLeft="@+id/textView1"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_weight="0.27" >
</ListView>
<Button
android:id="@+id/btnTambah"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@android:id/list"
android:layout_alignParentBottom="true"
android:layout_alignRight="@android:id/list"
android:layout_marginBottom="16dp"
android:text="Tambah Data Siswa" />
</RelativeLayout>
Sehingga pada graphical layout activity_main.xml akan tampak tampilan seperti berikut :
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="Tambah Mahasiswa"
android:textSize="20dp" />
<EditText
android:id="@+id/editNPM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:ems="10"
android:hint="NPM"/>
<EditText
android:id="@+id/editNAMA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editNPM"
android:layout_alignRight="@+id/editNPM"
android:layout_below="@+id/editNPM"
android:ems="10"
android:hint="NAMA" />
<EditText
android:id="@+id/editKELAS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editNAMA"
android:layout_alignRight="@+id/editNAMA"
android:layout_below="@+id/editNAMA"
android:ems="10"
android:hint="KELAS" />
<EditText
android:id="@+id/editALAMAT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editKELAS"
<Button
android:id="@+id/btnTambahData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editALAMAT"
android:layout_alignRight="@+id/editALAMAT"
android:layout_below="@+id/editALAMAT"
android:layout_marginTop="20dp"
android:text="Tambah Data" />
<Button
android:id="@+id/btnBatalkan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnTambahData"
android:layout_alignRight="@+id/btnTambahData"
android:layout_below="@+id/btnTambahData"
android:text="Kembali" />
</RelativeLayout>
Sehingga pada graphical layout activity_tambah.xml akan tampak tampilan seperti berikut
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="Edit Data Mahasiswa"
android:textSize="20dp" />
<EditText
android:id="@+id/editEDITNPM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:ems="10"
android:hint="NPM"/>
<EditText
android:id="@+id/editEDITALAMAT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editKELAS"
android:layout_centerVertical="true"
android:ems="13"
android:hint="Alamat"
android:lines="3" />
<EditText
android:id="@+id/editEDITNAMA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editEDITNPM"
android:layout_alignRight="@+id/editEDITALAMAT"
android:layout_below="@+id/editEDITNPM"
android:ems="10"
android:hint="NAMA" />
<EditText
android:id="@+id/editEDITKELAS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editEDITNAMA"
<Button
android:id="@+id/btnEDITSIMPAN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editEDITALAMAT"
android:layout_alignRight="@+id/editEDITALAMAT"
android:layout_below="@+id/editEDITALAMAT"
android:text="S I M P A N" />
<Button
android:id="@+id/btnEDITHAPUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnEDITSIMPAN"
android:layout_alignRight="@+id/btnEDITSIMPAN"
android:layout_below="@+id/btnEDITSIMPAN"
android:text="H A P U S" />
<Button
android:id="@+id/btnEDITKEMBALI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnEDITHAPUS"
android:layout_alignRight="@+id/btnEDITHAPUS"
android:layout_below="@+id/btnEDITHAPUS"
android:text="K E M B A L I" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No\t\t\t\t\t\t\t\t\t:\t"
android:textSize="20dp"
android:textColor="@android:color/black"/>
<TextView
android:id="@+id/textID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<TableLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NISN\t\t\t\t\t\t\t\t:\t"
android:textSize="20dp"
android:textColor="@android:color/black"/>
<TextView
android:id="@+id/textNPM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="15dp"
android:textColor="@android:color/black" />
</TableRow>
</TableLayout>
<TableLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NAMA\t\t\t\t\t\t\t:\t"
android:textSize="20dp"
android:textColor="@android:color/black"/>
<TextView
android:id="@+id/textNAMA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="15dp"
android:textColor="@android:color/black" />
</TableRow>
</TableLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout android:layout_width="fill_parent"
<TextView
android:id="@+id/textT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kelas\t\t\t\t\t\t\t:\t"
android:textSize="20dp"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/textKELAS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="15dp"
android:textColor="@android:color/black"/>
</TableRow>
</TableLayout>
<TableLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alamat\t\t\t\t\t\t:\t"
android:textSize="20dp"
android:textColor="@android:color/black"/>
<TextView
android:id="@+id/textALAMAT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="15dp"
android:textColor="@android:color/black"/>
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
Berikutnya tambahkan kelas JSONParser.java pada package project. Dengan cara klik kanan
package – class – JSONParser.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public JSONParser() {
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
try {
jobj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jobj;
}
MainActivity.java
package com.uniska.androidmysql;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, TambahActivity.class);
startActivity(i);
}
});
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
});
}
map.put(TAG_ID, id);
map.put(TAG_NPM, npm);
map.put(TAG_NAMA, nama);
map.put(TAG_KELAS, kelas);
map.put(TAG_ALAMAT, alamat);
listsiswa1.add(map);
}
}
else
{
}
}
catch(JSONException e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String file_url) {
// TODO Auto-generated method stub
pDialog.dismiss();
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
ListAdapter adapter = new SimpleAdapter(MainActivity.this, listsiswa1,
R.layout.komponendata,
new String[] {TAG_ID, TAG_NPM, TAG_NAMA, TAG_KELAS, TAG_ALAMAT}, new
int[]
TambahActivity.java
package com.uniska.androidmysql;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tambah);
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new TambahSiswa().execute();
}
});
kembali = (Button) findViewById(R.id.btnBatalkan);
kembali.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent kembali = new Intent(TambahActivity.this, MainActivity.class);
startActivity(kembali);
}
});
}
class TambahSiswa extends AsyncTask<String, String, String>
{
@Override
protected void onPreExecute() {
}
}
catch(JSONException e)
{
e.printStackTrace();
EditDataActivity.java
package com.uniska.androidmysql;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_data);
Intent i = getIntent();
id = i.getStringExtra(TAG_ID);
new GetSiswaDetails().execute();
editsimpan = (Button) findViewById(R.id.btnEDITSIMPAN);
editsimpan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new UpdateSiswa().execute();
}
});
edithapus = (Button) findViewById(R.id.btnEDITHAPUS);
edithapus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(EditDataActivity.this, MainActivity.class);
startActivity(i);
}
});
}
class GetSiswaDetails extends AsyncTask<String, String, String>
{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(EditDataActivity.this);
pDialog.setMessage("Mengambil Data...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
int success;
try
{
List<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("id", id));
success = json.getInt(TAG_SUCCESS);
if(success == 1)
{
JSONArray siswaOBJB = json.getJSONArray(TAG_MAHASISWA);
editENPM.setText(siswa.getString(TAG_NPM));
editENAMA.setText(siswa.getString(TAG_NAMA));
editEKELAS.setText(siswa.getString(TAG_KELAS));
editEALAMAT.setText(siswa.getString(TAG_ALAMAT));
}
else
{
}
}
catch(JSONException e)
{
e.printStackTrace();
}
}
}).start();
return null;
}
@Override
protected void onPostExecute(String file_url) {
// TODO Auto-generated method stub
pDialog.dismiss();
}
}
class UpdateSiswa extends AsyncTask<String, String, String>
{
}
}
catch(JSONException e)
success = json.getInt(TAG_SUCCESS);
if(success == 1)
{
Intent i = new Intent(EditDataActivity.this, MainActivity.class);
startActivity(i);
}
}
catch(JSONException e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String file_url) {
// TODO Auto-generated method stub
pDialog.dismiss();
}
}
}
Selesai