create a Student Database Program with java
Java is a language that can run anywhere and on any platform, in a variety of Java environments into technologies that are fundamentally different from others, which exist today. First, everyone can use small, secure, dynamic, cross-platform, active, and ready-to-run applets on the network right from the start. Secondly, Java is a powerful programming language, has the power of object-oriented design with a simple and easily recognizable syntax. Third, Java is a powerful set of object classes that serve programmers with clear descriptions for many common system functions, such as window creation, network usage, and input / output.
Command Listener
Command is an object used to respond to a user on an application. Often associated as softkey, button or area on the touchscreen. Very important role in event handling on J2ME. In its use, Command is defined by implementing CommandListener.
Image
Image stores an image graphic data. In high-level UI, only images that are immutable are images that can not be modified after they are created. In a low-level UI, a mutable image that is usually stored in the Image Object memory is defined in the J2ME library with the class:
javax.microedition.lcdui.Image
ImageItem
ImageItem is an object for displaying images like Image objects, only ImageItem is equipped with layout feature feature or structure where the image on the screen. The ImageItem object is defined in the J2ME library with the class:
javax.microedition.lcdui.ImageItem
StringItem
StringItem berfungsi untuk menampilkan sebuah label statis dan sebuah pesan yang berupa teks. Objek StringItem didefinisikan pada library J2ME dengan kelas:
javax.microedition.lcdui.StringItem
ChoiceGroup
Choice Group is a group that can be selected by selecting options in the group. ChoiceGroup has something in common with List. List is a sub class of Screen while ChoiceGroup is a sub class of Items.
The ChoiceGroup object is defined in the J2ME library with the class:
javax.microedition.lcdui.ChoiceGroup
STEP OF EXPERIMENT
. Opening NetBeans IDE.
. In the NetBeans 7.0.1 Home section, select> File> Select New Project
On the New Project page, Under Categories and Project select> Java ME> Mobile Application> Next.
On the New Mobile Application page, In the Name and Location field, In the Project Name field fill in the name of the project to be done, For example, List, Then select the location where the project will be saved, Last click Next.
On the New Mobile Application page, in the Device Configuration option select CLDC-1.0 for the Device Profile select MIDP-2.1 then click Finish.
In the file field on the left click on the new file section and it will appear
then categories / MIDP in the file type / Midlet, fill in the Class Project name, then click Finish.
Up to this stage The making of Simple MIDlet is almost done, just just type sintax below,
II. SOURCE CODE
import javax.microedition.rms.;
import javax.microedition.midlet.;
import javax.microedition.lcdui.;
import java.io.;
public class DataSiswa extends MIDlet implements CommandListener {
Form formTampil, formTambahData, formLihat, formEdit;
Alert alert;
List list;
RecordStore recordstore = null;
RecordEnumeration recordEnumeration = null;
private ChoiceGroup pilihangkatan, pilihkelas;
private int choiceGroupIndex;
Display display;
Ticker t;
Command cmdLihatSiswa = new Command("Data", Command.SCREEN, 1);
Command cmdTambahData = new Command("Tambah Data", Command.SCREEN, 1);
Command cmdEdit = new Command("Ubah Data", Command.SCREEN, 1);
Command cmdDelete = new Command("Hapus Data", Command.SCREEN, 1);
Command cmdExit = new Command("Keluar", Command.EXIT, 1);
Command cmdSimpan = new Command("Simpan", Command.SCREEN, 1);
Command cmdUpdate = new Command("Ubah", Command.SCREEN, 1);
Command cmdCancel = new Command("Kembali", Command.BACK, 1);
Command cmdBack = new Command("Kembali", Command.BACK, 1);
TextField txtNama = new TextField("Nama Siswa\t:", "", 30, TextField.ANY);
TextField txtNis = new TextField("No Induk Siswa\t:", "", 30, TextField.PHONENUMBER);
TextField txtRecNama, txtRecNis;
int recID, currentTask;
String keyword;
public DataSiswa() {
t = new Ticker("Program Pendataan Siswa");
formTampil = new Form(" Program Pendataan Siswa");
try {
Image img = Image.createImage("/logo.png");
ImageItem image = new ImageItem("Selamat Datang", img, Item.LAYOUT_CENTER, "");
formTampil.append(image);
} catch (Exception e) {
e.printStackTrace();
}
formTampil.addCommand(cmdLihatSiswa);
formTampil.addCommand(cmdExit);
formTampil.setCommandListener(this);
pilihangkatan = new ChoiceGroup("", Choice.POPUP);
pilihangkatan.append("2011", null);
pilihangkatan.append("2012", null);
pilihangkatan.append("2013", null);
pilihangkatan.append("2014", null);
pilihangkatan.append("2015", null);
pilihangkatan.append("2016", null);
pilihkelas = new ChoiceGroup("", Choice.POPUP);
pilihkelas.append("1A", null);
pilihkelas.append("1B", null);
pilihkelas.append("2A", null);
pilihkelas.append("2B", null);
pilihkelas.append("3A", null);
pilihkelas.append("3B", null);
formTambahData = new Form("Tambah Data Siswa");
formTambahData.addCommand(cmdBack);
formTambahData.addCommand(cmdSimpan);
formTambahData.append(txtNama);
formTambahData.append(txtNis);
formTambahData.append(new StringItem(null, "Kelas :\t"));
formTambahData.append(pilihkelas);
formTambahData.append(new StringItem(null, "Angkatan :\t"));
formTambahData.append(pilihangkatan);
formTambahData.setCommandListener(this);
}
public class RmsBanding implements RecordComparator {
private byte[] comparatorInputData = new byte[300];
private ByteArrayInputStream comparatorInputStream = null;
private DataInputStream comparatorInputDataType = null;
public int compare(byte[] record1, byte[] record2) {
int record1int, record2int;
try {
comparatorInputStream = new ByteArrayInputStream(record1);
comparatorInputDataType = new DataInputStream(comparatorInputStream);
String data1 = comparatorInputDataType.readUTF();
comparatorInputStream = new ByteArrayInputStream(record2);
comparatorInputDataType = new DataInputStream(comparatorInputStream);
String data2 = comparatorInputDataType.readUTF();
int comparison = data1.compareTo(data2);
if (comparison == 0) {
return RecordComparator.EQUIVALENT;
} else if (comparison < 0) {
return RecordComparator.PRECEDES;
} else {
return RecordComparator.FOLLOWS;
}
} catch (Exception error) {
return RecordComparator.EQUIVALENT;
}
}
public void compareClose() {
try {
if (comparatorInputStream != null) {
comparatorInputStream.close();
}
if (comparatorInputDataType != null) {
comparatorInputDataType.close();
}
} catch (Exception error) {
}
}
}
public void startApp() {
if (display == null) {
display = Display.getDisplay(this);
display.setCurrent(formTampil);
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {
try {
recordstore = RecordStore.openRecordStore("Siswa", true);
} catch (Exception error) {
alert = new Alert("Error Creating", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
if (c == cmdExit) {
destroyApp(true);
notifyDestroyed();
}
if (c == cmdBack) {
display.setCurrent(list);
}
if (c == cmdCancel) {
display.setCurrent(formTampil);
}
if (c == cmdTambahData) {
txtNama.setString("");
txtNis.setString("");
display.setCurrent(formTambahData);
}
if (c == cmdSimpan) {
try {
String outputNama = txtNama.getString();
String outputNip = txtNis.getString();
String outputBagian = pilihangkatan.getString(pilihangkatan.getSelectedIndex());
String outputJabatan = pilihkelas.getString(pilihkelas.getSelectedIndex());
byte[] outputRecord;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream outputDataStream = new DataOutputStream(outputStream);
outputDataStream.writeUTF(outputNama);
outputDataStream.writeUTF(outputNip);
outputDataStream.writeUTF(outputBagian);
outputDataStream.writeUTF(outputJabatan);
outputDataStream.flush();
outputRecord = outputStream.toByteArray();
recordstore.addRecord(outputRecord, 0, outputRecord.length);
outputStream.reset();
outputStream.close();
outputDataStream.close();
display.setCurrent(formTampil);
} catch (Exception error) {
display.setCurrent(alert);
}
}
if (c == cmdUpdate) {
try {
String outputNama = txtRecNama.getString();
String outputNip = txtRecNis.getString();
String outputBagian = pilihangkatan.getString(pilihangkatan.getSelectedIndex());
String outputJabatan = pilihkelas.getString(pilihkelas.getSelectedIndex());
byte[] outputRecord;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream outputDataStream = new DataOutputStream(outputStream);
outputDataStream.writeUTF(outputNama);
outputDataStream.writeUTF(outputNip);
outputDataStream.writeUTF(outputBagian);
outputDataStream.writeUTF(outputJabatan);
outputDataStream.flush();
outputRecord = outputStream.toByteArray();
recordstore.setRecord(recID, outputRecord, 0, outputRecord.length);
outputStream.reset();
outputStream.close();
outputDataStream.close();
display.setCurrent(formTampil);
} catch (Exception error) {
alert = new Alert("Error Writing", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
if (c == cmdEdit) {
int index = 1 + list.getSelectedIndex();
try {
formEdit = new Form("Ubah data Siswa");
formEdit.addCommand(cmdBack);
formEdit.addCommand(cmdUpdate);
formEdit.setCommandListener(this);
byte[] byteInputData = new byte[300];
String inputNama, inputNip, inputBagian = "", inputJabatan = "";
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
DataInputStream inputDataStream = new DataInputStream(inputStream);
if (currentTask == 2) {
RmsBanding comparator = new RmsBanding();
recordEnumeration = recordstore.enumerateRecords(null, comparator, false);
}
for (int i = 1; i <= recordEnumeration.numRecords(); i++) {
int datapointer = recordEnumeration.nextRecordId();
if (i == index) {
recordstore.getRecord(datapointer, byteInputData, 0);
recID = datapointer;
inputNama = inputDataStream.readUTF();
inputNip = inputDataStream.readUTF();
inputBagian = inputDataStream.readUTF();
inputJabatan = inputDataStream.readUTF();
txtRecNama = new TextField("Nama\t:", inputNama, 30, TextField.ANY);
txtRecNis = new TextField("No Induk Siswa\t:", inputNip, 30, TextField.PHONENUMBER);
formEdit.append(txtRecNama);
formEdit.append(txtRecNis);
formEdit.append(new StringItem(null, "Kelas :\t"));
formEdit.append(inputBagian);
formEdit.append(new StringItem(null, "\n"));
formEdit.append(new StringItem(null, "Angkatan :\t"));
formEdit.append(inputJabatan);
inputStream.reset();
}
}
display.setCurrent(formEdit);
inputStream.close();
} catch (Exception error) {
alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
if (c == cmdDelete) {
int index = 1 + list.getSelectedIndex();
try {
if (currentTask == 2) {
RmsBanding comparator = new RmsBanding();
recordEnumeration = recordstore.enumerateRecords(null, comparator, false);
}
for (int i = 1; i <= recordEnumeration.numRecords(); i++) {
int datapointer = recordEnumeration.nextRecordId();
if (i == index) {
recordstore.deleteRecord(datapointer);
}
}
display.setCurrent(formTampil);
} catch (Exception error) {
alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
if (c == cmdLihatSiswa) {
try {
list = new List(" Nama-Nama Siswa", List.IMPLICIT);
list.addCommand(cmdTambahData);
list.addCommand(cmdCancel);
list.setTicker(t);
list.setCommandListener(this);
currentTask = 2;
byte[] byteInputData = new byte[300];
String inputNama, inputNip = null;
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
DataInputStream inputDataStream = new DataInputStream(inputStream);
RmsBanding comparator = new RmsBanding();
recordEnumeration = recordstore.enumerateRecords(null, comparator, false);
while (recordEnumeration.hasNextElement()) {
recordstore.getRecord(recordEnumeration.nextRecordId(), byteInputData, 0);
inputNama = inputDataStream.readUTF();
inputNip = inputDataStream.readUTF();
list.append(inputNama + "", null);
inputStream.reset();
}
display.setCurrent(list);
inputStream.close();
} catch (Exception error) {
alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
if (c == List.SELECT_COMMAND) {
int index = 1 + list.getSelectedIndex();
try {
formLihat = new Form("Keterangan : ");
formLihat.addCommand(cmdBack);
formLihat.addCommand(cmdEdit);
formLihat.addCommand(cmdDelete);
formLihat.setCommandListener(this);
byte[] byteInputData = new byte[300];
String inputNama, inputNis, inputangkatan, inputkelas = null;
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
DataInputStream inputDataStream = new DataInputStream(inputStream);
if (currentTask == 2) {
RmsBanding comparator = new RmsBanding();
recordEnumeration = recordstore.enumerateRecords(null, comparator, false);
}
for (int i = 1; i <= recordEnumeration.numRecords(); i++) {
int datapointer = recordEnumeration.nextRecordId();
if (i == index) {
recordstore.getRecord(datapointer, byteInputData, 0);
recID = datapointer;
inputNama = inputDataStream.readUTF();
inputNis = inputDataStream.readUTF();
inputangkatan = inputDataStream.readUTF();
inputkelas = inputDataStream.readUTF();
StringItem stringNama = new StringItem("Nama Siswa\t: ", inputNama, Item.PLAIN);
StringItem stringNis = new StringItem("No Induk Siswa\t: ", inputNis, Item.PLAIN);
StringItem stringangkatan = new StringItem("Angkatan\t : ", inputangkatan, Item.PLAIN);
StringItem stringkelas = new StringItem("Kelas\t : ", inputkelas, Item.PLAIN);
formLihat.append(stringNama);
formLihat.append(stringNis);
formLihat.append(stringangkatan);
formLihat.append(stringkelas);
inputStream.reset();
try {
Image img = Image.createImage("/foto.png");
ImageItem image = new ImageItem("", img, Item.LAYOUT_CENTER, "");
formLihat.append(image);
} catch (Exception e) {
e.printStackTrace();
}
}
}
display.setCurrent(formLihat);
inputStream.close();
} catch (Exception error) {
alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
try {
recordstore.closeRecordStore();
} catch (Exception error) {
alert = new Alert("Error Closing", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
}
experimental results
Figure 1
Figure2
figure3
figure4
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Hey @isfar I am @utopian-io. I have just upvoted you at 7% Power!
Achievements
Suggestions
Human Curation
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x