You are on page 1of 5

LAPORAN PRAKTIKUM

Pemograman mobile PRAKTIKUM IX:TUTORIAL PEMBUATAN Database(Part 1).

DISUSUN OLEH : Wahyu Nurfian 12.12.0062

LABORATORIUM KOMPUTER & JARINGAN SEKOLAH TINGGI MANAJEMEN INFORMATIKA DAN KOMPUTER AMIKOM PURWOKERTO JL. LET. JEND. POL SUMARTO PURWOKERTO 2013

1. Buat Project Baru 2. Desain interface baru untuk tampilan awal program 3. Buat Layout baru dengan nama create data dengan tampilan seperti berikut :

4. Buat Class baru pada folder src dengan nama createData,tuliskan coding seperti di bawah ini: package com.example.dbandroid; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class createData extends Activity implements OnClickListener {

private Button Submit; private EditText nama,merk,harga; private DBDataSource datasource; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.createdata); Submit=(Button) findViewById(R.id.btnSubmit); Submit.setOnClickListener(this); nama=(EditText) findViewById(R.id.txtnama); merk=(EditText) findViewById(R.id.txtmerk); harga=(EditText) findViewById(R.id.txtharga); datasource=new DBDataSource(this); datasource.open(); } public void onClick(View arg0){ String nm=null; String mk=null; String hrg=null; Barang barang=null; if(nama.getText()!=null&&merk.getText()!=null && harga.getText()!=null){ nm=nama.getText().toString(); mk=merk.getText().toString(); hrg=harga.getText().toString(); } switch(arg0.getId()) { case R.id.btnSubmit: barang=datasource.createBarang(nm,mk,hrg); Toast.makeText(this, "masuk Barang\n"+ "nama"+barang.getNama_barang()+ "merk"+barang.getMerk_barang()+ "harga"+barang.getHarga_barang(), Toast.LENGTH_LONG).show(); break; } } }

5. Buat class baru dengan nama dbHelper,dan tuliskan coding berikut : package com.example.dbandroid; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class dbHelper extends SQLiteOpenHelper { public static final String TABLE_NAME="data_inventory"; public static final String COLOUMN_ID="_id"; public static final String COLOUMN_NAME="nama_barang"; public static final String COLOUMN_MERK="merk_barang"; public static final String COLOUMN_HARGA="harga_barang"; private static final String db_name="inventory.db"; private static final Integer db_version=1; private static final String db_create="create table" +TABLE_NAME+"(" +COLOUMN_ID+"integer primary key autoincrement," +COLOUMN_NAME+"varchar(50) not null," +COLOUMN_MERK+"varchar(50) not null," +COLOUMN_HARGA+"varchar(50) not null);"; public dbHelper(Context sia){ super(sia,db_name,null,db_version); } public void onCreate(SQLiteDatabase db){ db.execSQL(db_create); } public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){ Log.w(dbHelper.class.getName(),"Upgrading database from"+ oldVersion+"to"+newVersion+"which will destroy all old data"); db.execSQL("Drop Table If Exists"+TABLE_NAME); onCreate(db); } } 6. Buat Class baru dengan namaDBDataSource dengan coding seperti berikut: package com.example.dbandroid; import android.content.ContentValues; import android.content.Context; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase;

public class DBDataSource { private SQLiteDatabase database; private dbHelper dbHelper; private String[] allcolomns={ dbHelper.COLOUMN_ID, dbHelper.COLOUMN_NAME, dbHelper.COLOUMN_MERK, dbHelper.COLOUMN_HARGA }; public DBDataSource(Context c){ dbHelper=new dbHelper(c); } public void open()throws SQLException{ database=dbHelper.getWritableDatabase(); } public void close(){ dbHelper.close(); } public Barang createBarang(String nm,String mk,String hrg){ ContentValues values=new ContentValues(); values.put(dbHelper.COLOUMN_NAME, nm); values.put(dbHelper.COLOUMN_MERK, mk); values.put(dbHelper.COLOUMN_HARGA, hrg); long insertId=database.insert(dbHelper.TABLE_NAME,null, values); Cursor cursor=database.query(distinct, table, columns, selection, selectionArgs, groupBy, having, orderBy, limit, cancellationSignal) } }