NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

package app.ouzn.com.crud;



import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;



public class MainActivity extends AppCompatActivity {



//Deklarasi variable

SQLiteDatabase myDB = null;

String TableName = "Mahasiswa";

String Data = "";



TextView dtMhs;



Button bSimpan, bEdit, bHapus;

EditText tNim, tNama, tAlamat;



@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);



dtMhs = (TextView)findViewById(R.id.txtDataMhs);



bSimpan = (Button)findViewById(R.id.btnSimpan);

bEdit = (Button)findViewById(R.id.btnEdit);

bHapus = (Button)findViewById(R.id.btnHapus);



tNim = (EditText)findViewById(R.id.txtNim);

tNama = (EditText)findViewById(R.id.txtNama);

tAlamat = (EditText)findViewById(R.id.txtAlamat);



createDB();

tampilData();



bSimpan.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

simpan();

}

});

bEdit.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

edit();

}

});

bHapus.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

hapus();

}

});

}



//Method Clear TextField

public void clearField(){

tNim.setText("");

tNama.setText("");

tAlamat.setText("");

}

//Buat Method Create Database

public void createDB(){

try{

myDB = this.openOrCreateDatabase("DBMHS",MODE_PRIVATE,null);



myDB.execSQL("CREATE TABLE IF NOT EXISTS " +

TableName + "(NIM VARCHAR PRIMARY KEY, NAMA VARCHAR, ALAMAT VARCHAR);");





//myDB.execSQL("Insert Into " + TableName + " Values('126','HusnulM','Lombok');");



}catch (Exception e){

//Log.e("Error", "Error", e);

}

}



//Buat Method Tampilkan Data

public void tampilData(){

try{

Data = "";

clearField();

Cursor c = myDB.rawQuery("Select * From " + TableName, null);



int col1 = c.getColumnIndex("NIM");

int col2 = c.getColumnIndex("NAMA");

int col3 = c.getColumnIndex("ALAMAT");



c.moveToFirst();



if (c!= null){

do{

String nimMhs = c.getString(col1);

String nmMhs = c.getString(col2);

String almtMhs = c.getString(col3);

Data = Data + nimMhs + " | " + nmMhs+ " | " + almtMhs + "n";

}

while (c.moveToNext());

}

dtMhs.setText(Data);

}catch (Exception e){

dtMhs.setText(Data);

}

}



//Method Simpan Data

public void simpan(){

myDB.execSQL("Insert Into " + TableName + " Values('" + tNim.getText() + "','" + tNama.getText() + "','" + tAlamat.getText() + "');");

tampilData();

}



//Method Edit

public void edit(){

myDB.execSQL("Update " + TableName + " Set NAMA = '"+ tNama.getText() +"', ALAMAT = '"+ tAlamat.getText() +"' Where NIM = '"+ tNim.getText() +"';");

tampilData();



}



//Method Hapus

public void hapus(){

myDB.execSQL("Delete From " + TableName + " Where NIM = '" + tNim.getText() + "';");

tampilData();

}



}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.