You are on page 1of 9

LAB 17

1. Open Netbeans application and go to File menu > New Project > Select Java > Java Application>
Click Next> Enter the name of Project “JAVAApp”> Unchecked “Create main class” > Click Finish.
2. Right click at the source packages> Select New> Select JFrame> Enter the class Name
“frmMain”> Click Finish.
3. Create a form like below.

4. Use the following variable name to each component.


a. ID = txtID
b. EMPLOYEE NAME = txtEmployeeName
c. EMAIL = txtEmail
d. AGE = txtAge
e. DESIGNATION = txtDesignation
f. REFRESH BUTTON = btnRefresh
g. EDIT BUTTON = btnEdit
h. DELETE BUTTON = btnDelete
i. TABLE = tbList
j. ADD = btnAdd
5. Add the library called simple-json.jar.
a. Right-click Libraries> Select Add JAR/Folder> select the json-simple library(to be given) >
Select Open
6. Right click ADD button> Select Events> Action > actionperformed and add the following codes:
if (btnAdd.getText().equals("ADD")) {

String employeeName = txtEmployeeName.getText().trim();


String email = txtEmail.getText().trim();
String age = txtAge.getText().trim();
String designation = txtDesignation.getText().trim();
//check if employeeName is empty
if (employeeName.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter employee name!", "ERROR",
JOptionPane.ERROR_MESSAGE);
txtEmployeeName.requestFocus();
return;
}
//check if email is empty
if (email.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter employee email!", "ERROR",
JOptionPane.ERROR_MESSAGE);
txtEmail.requestFocus();
return;
}
//check if employee age is empty
if (age.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter employee age!", "ERROR",
JOptionPane.ERROR_MESSAGE);
txtAge.requestFocus();
return;
}
//check if employee designation is empty
if (designation.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter employee designation!", "ERROR",
JOptionPane.ERROR_MESSAGE);
txtDesignation.requestFocus();
return;
}
HttpURLConnection conn = null;
String durl = "http://localhost/ITPROF7/phpapi/api/employee/create.php";

try {
URL url = new URL(durl);
//String agent = "Applet";
String query = "name=" + employeeName + "&email=" + email + "&age=" + age +
"&designation=" + designation;
//System.out.println(query);
String type = "application/x-www-form-urlencoded";
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
//conn.setRequestProperty("User-Agent", agent);
conn.setRequestProperty("Content-Type", type);
conn.setRequestProperty("Content-Length", "" + query.length());

//get the response


OutputStream out = conn.getOutputStream();
out.write(query.getBytes());
BufferedReader in = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
String inputLine, result = null;
while ((inputLine = in.readLine()) != null) {
result += inputLine;
System.out.print(inputLine + "\n");
};

in.close();
if (result.contains("success")) {
JOptionPane.showMessageDialog(this, "New Record Successfully Inserted!",
"SUCCESS", JOptionPane.INFORMATION_MESSAGE);
txtEmployeeName.setText("");
txtEmail.setText("");
txtAge.setText("");
txtDesignation.setText("");

refresh();
} else {
JSONObject json = new JSONObject(result.toString());
JOptionPane.showMessageDialog(this, json.get("message"), "SUCCESS",
JOptionPane.ERROR_MESSAGE);
}
//int rc = conn.getResponseCode();
//System.out.print("Response Code = " + rc + "\n");
//String rm = conn.getResponseMessage();
// System.out.print("Response Message = " + rm + "\n");
// txtEmployeeName.setText("");
// txtEmail.setText("");
// txtAge.setText("");
// txtDestination.setText("");

} catch (Exception e) {
e.printStackTrace();
} finally {
conn.disconnect();
}
} else {
//codes to update employee
int CONFIRM = JOptionPane.showConfirmDialog(this, "Are all information correct?",
"CONFIRM", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (CONFIRM == JOptionPane.NO_OPTION) {
return;
}

int selectedRow = tbList.getSelectedRow();

//get values from the selected row


String id = txtID.getText().trim();
String name = txtEmployeeName.getText().trim();
String email = txtEmail.getText().trim();
String age = txtAge.getText().trim();
String designation = txtDesignation.getText().trim();

HttpURLConnection conn = null;


String durl = "http://localhost/ITPROF7/phpapi/api/employee/update.php";

try {
URL url = new URL(durl);
//String agent = "Applet";
String query = "name=" + name + "&email=" + email + "&designation=" + designation +
"&age=" + age + "&id=" + id;
String type = "application/x-www-form-urlencoded";
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
//conn.setRequestProperty("User-Agent", agent);
conn.setRequestProperty("Content-Type", type);
conn.setRequestProperty("Content-Length", "" + query.length());

OutputStream out = conn.getOutputStream();


out.write(query.getBytes());
BufferedReader in = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
String inputLine, result = null;
while ((inputLine = in.readLine()) != null) {
result += inputLine;
};
in.close();
System.out.println(result);
if (result.contains("success")) {
JOptionPane.showMessageDialog(this, "Employee Info. has been updated
Successfully!", "SUCCESS", JOptionPane.INFORMATION_MESSAGE);

btnAdd.setText("ADD");
btnEdit.setText("EDIT");
btnEdit.setEnabled(true);
btnDelete.setEnabled(true);

txtID.setText("");
txtEmployeeName.setText("");
txtEmail.setText("");
txtAge.setText("");
txtDesignation.setText("");

refresh();

} else {
System.out.println(result);
JSONObject json = new JSONObject(result.toString());
JOptionPane.showMessageDialog(this, json.get("message"), "ERROR",
JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.disconnect();
}
}
7. Right click REFRESH button> Select Events> Action > actionperformed and add the following
codes:
int row = 0;
//CHANGE THIS USING YOUR API
String url = "http://localhost/ITPROF7/phpapi/api/employee/read.php";
DefaultTableModel model = (DefaultTableModel) tbList.getModel();
try {
model.setRowCount(0);
InputStream is = new URL(url).openStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is,
Charset.forName("UTF-8")));
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
//JSONArray array = new JSONArray(sb.toString());
JSONObject json = new JSONObject(sb.toString());

System.out.println(json.get("itemCount"));

JSONArray array = (JSONArray) json.get("body");

for (int i = 0; i < array.length(); i++) {


JSONObject obj4 = array.getJSONObject(i);
model.addRow(new Object[]{obj4.get("id"), obj4.get("name"), obj4.get("email"),
obj4.get("age"), obj4.get("designation"), obj4.get("created")});
row++;
}
if (row == 0) {
model.addRow(new Object[]{"", "No records found.."});
}

} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage(), "ERROR",
JOptionPane.ERROR_MESSAGE);
}
8. Right click REFRESH button> Select Events> Action > actionperformed and add the following
codes:

int row = 0;

// CHANGE THIS TO YOUR API URL

String url = "http://localhost/ITPROF7/phpapi/api/employee/read.php";

DefaultTableModel model = (DefaultTableModel) tbList.getModel();

try {

model.setRowCount(0);

InputStream is = new URL(url).openStream();

BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-


8")));

StringBuilder sb = new StringBuilder();

int cp;

while ((cp = rd.read()) != -1) {

sb.append((char) cp);

}
//JSONArray array = new JSONArray(sb.toString());

JSONObject json = new JSONObject(sb.toString());

System.out.println(json.get("itemCount"));

JSONArray array = (JSONArray) json.get("body");

for (int i = 0; i < array.length(); i++) {

JSONObject obj4 = array.getJSONObject(i);

model.addRow(new Object[]{obj4.get("id"), obj4.get("name"), obj4.get("email"),


obj4.get("age"), obj4.get("designation"), obj4.get("created")});

row++;

if (row == 0) {

model.addRow(new Object[]{"", "No records found.."});

} catch (IOException e) {

JOptionPane.showMessageDialog(this, e.getMessage(), "ERROR",


JOptionPane.ERROR_MESSAGE);

9. Right click DELETE button> Select Events> Action > actionperformed and add the following
codes:

int selectedRow = tbList.getSelectedRow();

if (selectedRow == -1) {

JOptionPane.showMessageDialog(this, "Please select employee info. to delete!", "ERROR",


JOptionPane.ERROR_MESSAGE);

return;

//get id from the selected row


String id = String.valueOf(tbList.getValueAt(selectedRow, 0));

int CONFIRM = JOptionPane.showConfirmDialog(this, "Are you sure want to delete, ID= " + id,
"CONFIRM", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

if (CONFIRM == JOptionPane.NO_OPTION) {

return;

HttpURLConnection conn = null;

String durl = "http://localhost/ITPROF7/phpapi/api/employee/delete.php";

try {

URL url = new URL(durl);

//String agent = "Applet";

String query = "id=" + id;

String type = "application/x-www-form-urlencoded";

conn = (HttpURLConnection) url.openConnection();

conn.setDoInput(true);

conn.setDoOutput(true);

conn.setRequestMethod("POST");

//conn.setRequestProperty("User-Agent", agent);

conn.setRequestProperty("Content-Type", type);

conn.setRequestProperty("Content-Length", "" + query.length());

OutputStream out = conn.getOutputStream();

out.write(query.getBytes());

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

String inputLine, result = "";

while ((inputLine = in.readLine()) != null) {

result += inputLine;
};

in.close();

System.out.println(result);

if (result.contains("Employee deleted.")) {

JOptionPane.showMessageDialog(this, "DATA DELETED SUCCESSFULLY!", "SUCCESS",


JOptionPane.INFORMATION_MESSAGE);

refresh();

} else {

Toolkit.getDefaultToolkit().beep();

JOptionPane.showMessageDialog(this, "FAILED TO DELETE. Please try again!!", "ERROR",


JOptionPane.ERROR_MESSAGE);

//int rc = conn.getResponseCode();

//System.out.print("Response Code = " + rc + "\n");

//String rm = conn.getResponseMessage();

// System.out.print("Response Message = " + rm + "\n");

} catch (Exception e) {

e.printStackTrace();

} finally {

conn.disconnect();

10. Run your project, right-click on the project name and select run.

You might also like