Professional Documents
Culture Documents
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.
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());
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;
}
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());
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"));
} 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;
try {
model.setRowCount(0);
int cp;
sb.append((char) cp);
}
//JSONArray array = new JSONArray(sb.toString());
System.out.println(json.get("itemCount"));
row++;
if (row == 0) {
} catch (IOException e) {
9. Right click DELETE button> Select Events> Action > actionperformed and add the following
codes:
if (selectedRow == -1) {
return;
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;
try {
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
//conn.setRequestProperty("User-Agent", agent);
conn.setRequestProperty("Content-Type", type);
out.write(query.getBytes());
result += inputLine;
};
in.close();
System.out.println(result);
if (result.contains("Employee deleted.")) {
refresh();
} else {
Toolkit.getDefaultToolkit().beep();
//int rc = conn.getResponseCode();
//String rm = conn.getResponseMessage();
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.disconnect();
10. Run your project, right-click on the project name and select run.