การพัฒนาระบบด้วยภาษา JAVA และการออกแบบระบบเชิงวัตถุ V1
วันที่เขียน 23/2/2554 11:15:30     แก้ไขล่าสุดเมื่อ 28/3/2567 17:39:09
เปิดอ่าน: 29484 ครั้ง

เพื่อแสดงตัวอย่างการพัฒนาระบบด้วยภาษา JAVA และการออกแบบระบบเชิงวัตถุ ซึ่งทำให้การพัฒนาระบบสามารถแก้ไขได้ง่าย และเป็นการพัฒนาระบบในตระกูล OpenSource

การพัฒนาโปรแกรมด้วยภาษาจาวา และออกแบบโปรแกรมเชิงวัตถุ 

          ณัฐกฤตา ได้ไปศึกษาการพัฒนาโปรแกรมด้วยภาษาจาวา และการออกแบบโปรแกรมเชิงวัตถุ มา เนื่องจากว่าพบความแตกต่างระหว่าง Programmer รุ่นใหม่ กับรุ่นก่อน โดยเฉพาะกับนักศึกษาที่ได้เข้ามาฝึกงานที่ศูนย์ IT ส่วนใหญ่มีพื้นฐานการพัฒนามาจากภาษาจาวา และถูกปลูกฝังให้ออกแบบระบบเชิงวัตถุ ซึ่งเป็นพื้นฐานที่ใช้ในการวิเคราะห์ระบบด้วย UML ซึ่งในสมัยที่เราเรียนนั้น ยังเป็นการออกแบบด้วย DFD ER Diagram  ซึ่งเป็นสิ่งยากสำหรับ Programmer รุ่นเรา ที่จะเข้าใจ แต่พอสรุปคร่าวๆ ได้ว่า รุ่นใหม่เค้าจะคิดและวิเคราะห์ระบบให้สามารถจับต้องได้ ใครทำอะไรมีหน้าที่อะไรก็เป็นของคนนั้น อย่างเช่น แม่ค้าขายก๋วยเตี๋ยว ก็จะมีหน้าที่ในการทำก๋วยเตี๋ยว บุรุษไปรษณีย์ ก็มีหน้าที่คัดแยกและจัดส่งจดหมายสู่ปลายทาง ซึ่งทำให้การแก้ไขปรับปรุงระบบได้ง่ายขึ้น อย่างเช่น ถ้าเราพูดถึงระบบที่เกี่ยวข้องกับนักศึกษา เราก็ดูเฉพาะส่วนที่เป็นนักศึกษา ถ้าเราพูดถึงระบบที่เกี่ยวข้องกับรายวิชาก็จะดูเฉพาะส่วนนั้นๆ ซึ่งถ้าเทียบกับที่เราเคยวิเคราะห์กันมา ก็ทำเป็น Procedure หรือ Function ไว้ ซึ่งถ้า Programmer ที่พัฒนาโดยตระกูล Microsoft .Net ก็พอทำ Class เอาไว้เพื่อใช้ประโยชน์ในกรณี Reused Code รวมๆ กันไว้ใน Class หนึ่งๆ แต่ก็ไม่ได้จับแยกว่าอะไรเป็นของอะไร ยิ่งกรณีที่ Programmer ที่พัฒนาโดยใช้ภาษา PHP ยิ่งพัฒนาโปรแกรมฝั่งในหน้าเว็บเลย ทำให้ผู้ที่ต้องรับผิดชอบต่องานของ Programmer คนก่อน ไม่สามารถจับต้นชนปลายได้ถูกว่าที่มัน Error น่ะมันอยู่ตรงไหน จะไล่อ่าน Code ก็แสนจะลำบาก ยิ่งถ้าไม่ได้ทำคู่มือผู้พัฒนาระบบไว้นะ ไม่อยากจะคิดต่อเลยคะ

          เกริ่นมานิดหน่อยนะคะ ณัฐกฤตา ได้ลองพัฒนาเว็บไซด์ซึ่งพัฒนาโดย JAVA ที่มีการวิเคราะห์ระบบเชิงวัตถุประกอบในการพัฒนาเว็บไซด์ และทำการพัฒนาเว็บไซด์ โดยอิงหลักการพัฒนาแบบสถาปัตยกรรม 3 tier นั่นคือ MVC ซึ่งย่อมาจาก M : Model, V : View , C  : Controller

Model            == class manager // javaBean // POJO

        View             == หน้าแสดงผล jsp, html

Controller        ==  ส่วนที่เป็น Servlet

 

          หลักการง่ายๆ ก็คือ

  1. สร้าง Class เพื่อเก็บ attribute , Method
  2. สร้างหน้าจอ สำหรับติดต่อกับผู้ใช้ (View)
  3. สร้างหน้า Servlet สำหรับการรับค่าจากหน้าจอ กับค่าข้อมูลจากส่วนติดต่อฐานข้อมูล (Controller)
  4. สร้างหน้า Manager สำหรับเป็นส่วนติดต่อฐานข้อมูล (Model)

มีเทคนิคนิดหนึ่งก็คือ ในตอนติดต่อกับฐานข้อมูล เราจะใช้ประโยชน์จาก class ซึ่งเก็บ attribute ไว้ และ Method set, get โดยการนำข้อมูลที่ได้จากการคิวรีข้อมูล set value ให้กับ attribute แล้วจึงส่งค่า return กลับไปยัง Servlet เพื่อแสดงผลให้กับผู้ใช้ในหน้า View ต่อไป ดูจากตัวอย่างกันนะคะ

 

Default.jsp

          หลักการทำงานคือ สร้างหน้าเว็บไซด์แสดงรายการเมนูเพื่อจัดการฐานข้อมูล ประกอบด้วย เมนูเพิ่มข้อมูลพนักงานและเมนูแสดงข้อมูลพนักงาน โดยใช้ลิงค์ ซึ่งส่งค่าพารามิเตอร์สำหรับการทำงานผ่านตัวแปร act ไปยัง Servlet เพื่อประมวลผล

 

<?xml version="1.0" encoding="utf-8" ?>

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>ติดต่อฐานข้อมูล MYSQL</title>

</head>

<body>

            <h1>เมนู</h1>

            <hr></hr>

            <a href="InsertServlet?act=toadd">เพิ่มข้อมูลพนักงาน</a><br></br>

            <a href="EmployeeServlet?act=show">แสดงข้อมูลพนักงานทั้งหมด</a>      

</body>

</html>

           

 

ShowAllEmployee.jsp

          หลักการทำงาน คือ รับค่าข้อมูลจาก Servlet โดยผ่านฟังก์ชัน request.getAttritbute(“list”) โดยค่าข้อมูลที่ได้กำหนดรับค่าเป็น ArrayList ของ Object Department และแสดงผลโดยใช้ Loop For

 

<?xml version="1.0" encoding="utf-8" ?>

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Show All Employee</title>

<%@ page import="Employee.*" %>

<%@ page import="java.util.*" %>

<% ArrayList<Department> list = (ArrayList<Department>) request.getAttribute("list"); %>

</head>

<body>

            <form name="form1" action="EmployeeServlet" method="get">

    <% for(int i = 0; i<list.size(); i++) { %>

            <% Vector <Employee> emp = list.get(i).getEmps(); %>

            <% if(emp.size()>0) { %>

            <table border="2">

                        <tr>

                                    <td>แผนก</td>

                                    <td colspan="4"><%=list.get(i).getDeptname() %></td>

                        </tr>

                        <tr>

                                    <td>รหัสพนักงาน</td>

                                    <td>ชื่อ  - นามสกุล</td>

                                    <td>เงินเดือน</td>

                                    <td>แก้ไข</td>

                                    <td>ลบ</td>

                        </tr>                           

                                    <%for(int j =0 ;j<emp.size();j++){ %>

                        <tr>                            

                                    <td><%= emp.get(j).getEmpID()  %></td>

                                    <td><%= emp.get(j).getEmpName()%></td>

                                    <td><%= emp.get(j).getSalary() %></td>

                                    <td>

                                                <a href="UpdateServlet?act=select&id=<%= emp.get(j).getEmpID() %>">แก้ไข</a>

                                    </td>

                                    <td>

                                                <a href="UpdateServlet?act=del&id=<%= emp.get(j).getEmpID() %>"  onclick="return(confirm('ต้องการลบข้อมูลพนักงาน แน่ใจหรือไม่?'))">ลบ</a>

                                                </td>

                        </tr>

                                    <%} %>

            </table>  

            <%} %>            

            <br></br>

    <%} %>

    <a href="default.jsp">กลับหน้าหลัก</a><br></br>

    <a href="InsertServlet?act=toadd">เพิ่มข้อมูลพนักงาน</a><br></br>

    </form>

</body>

</html>

 

AddEmployee.jsp

          หลักการคือ สร้างรหัสพนักงานอัตโนมัติ โดยเพิ่มลำดับรหัสจากล่าสุด ประเภทแผนก ดึงข้อมูลจากฐานข้อมูลมาแสดงบนหน้าเว็บ โดยการรับข้อมูลจาก request.getAttribute(“imDept”);

 

<?xml version="1.0" encoding="utf-8" ?>

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Insert Employee</title>

<%@page import="java.util.*" %>

<%Hashtable<String, String> imDept = (Hashtable<String, String>) request.getAttribute("imDept"); %>

<%int empcode = (Integer) request.getAttribute("empcode"); %>

</head>

<body>

            <h1>เพิ่มข้อมูลพนักงาน</h1>

            <form name="form1" action="InsertServlet" method="post">

                        <fieldset>

                        <legend>เพิ่มข้อมูลพนักงาน</legend>

                                    รหัสพนักงาน : <input type="text" name="empid" value="<%= empcode %>" disabled="disabled"/><br></br>

                                    ชื่อ-นามสกุล : <input type="text" name="name"/><br></br>

                                    เงินเดือน        : <input type="text" name="salary"/><br></br>

                                    แผนก             :

                                    <% Set<String> keys = imDept.keySet(); %>

                                    <% int i = 0; %>

                                    <select name="ddlDep">

                                                <% for(String k : keys) { %>

                                                <% i++; %>

                        <option value="<%=k %>"><%=imDept.get(k) %></option>

                        <%} %>                                              

                                    </select><br></br>                             

                                    <input type="submit" value="บันทึก"/>

                                    <input type="reset" value="ยกเลิก"/><br></br>            

                                    <input type="hidden" id="act" name="act" value="add" />                       

                                    <input type="hidden" id="id" name="id" value="<%= empcode %>" />                                       

            </fieldset>

            </form>

            <a href="default.jsp">กลับหน้าหลัก</a><br></br>

    <a href="EmployeeServlet?act=show">แสดงข้อมูลพนักงานทั้งหมด</a>

</body>

</html>

 

EditEmployee.jsp

          หลักการคือ คิวรีข้อมูลจากรหัสพนักงานที่เลือก และนำข้อมูลแสดงบนหน้าเว็บสำหรับการแก้ไขข้อมูล โดยไม่อนุญาติให้แก้ไขรหัสพนักงาน 

  

<?xml version="1.0" encoding="utf-8" ?>

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Edit Employee</title>

<%@ page import="Employee.*" %>

<%@ page import="java.util.*" %>

<% Employee list = (Employee) request.getAttribute("imEmp"); %>

 

<% Hashtable<String, String> imDept = (Hashtable<String, String>) request.getAttribute("imDept"); %>

 

</head>

<body>

            <h1>แก้ไขข้อมูลพนักงาน</h1>

            <form name="form1" action="UpdateServlet" method="post">

                        <fieldset>

                        <legend>แก้ไขข้อมูลพนักงาน</legend>

                                    รหัสพนักงาน  : <input type="text" name="empid" value="<%=list.getEmpID() %>" disabled="disabled"/><br></br>

                                    ชื่อ-นามสกุล   : <input type="text" name="name" value="<%=list.getEmpName() %>"/><br></br>

                                    เงินเดือน          : <input type="text" name="salary" value="<%=list.getSalary() %>"/><br></br>

                                    แผนก             :

                                    <% Set<String> keys = imDept.keySet(); %>

                                    <% int i = 0; %>

                                    <select name="ddlDep">

                                                <% for(String k : keys) { %>

                                                <% i++; %>

                                                <%if (k.equalsIgnoreCase(Integer.toString(list.getDeptID()))) { %>

                                    <option value="<%=k %>" selected="selected"><%=imDept.get(k) %></option>

                        <%}else { %>

                                    <option value="<%=k %>"><%=imDept.get(k) %></option>

                        <%} %>

                        <%} %>                                              

                                    </select><br></br>     

                                    <input type="submit" value="บันทึก"/>

                                    <input type="reset" value="ยกเลิก"/><br></br>            

                                    <input type="hidden" id="act" name="act" value="edit" />           

                                    <input type="hidden" id="id" name="id" value="<%=list.getEmpID() %>" />

            </fieldset>

            </form>

            <a href="default.jsp">กลับหน้าหลัก</a><br></br>

    <a href="EmployeeServlet?act=show">แสดงข้อมูลพนักงานทั้งหมด</a>

</body>

</html>

 

ConnectionDB.java

          เป็น Class สำหรับการติดต่อกับฐานข้อมูล โดยในตัวอย่างใช้ MySql และติดต่อโดยผ่าน Driver

กำหนด

Attribute คือ Connection connection

Method Connection สำหรับติดต่อฐานข้อมูล

Method getConnection() สำหรับส่งค่าการกำหนดคำสั่งติดต่อฐานข้อมูล  

Method setConnection() สำหรับกำหนดคำสั่งติดต่อฐานข้อมูล ให้กับ Attribute connection

Method closeAdminDB() สำหรับการปิดการเชื่อมต่อฐานข้อมูล

 

package connect;

 

import java.sql.*;

 

public class ConnectionDB {

            private Connection connection = null;

            public Connection connectAdminDB() {

                        try {

                                    ////แบบใช้ ODBC ในเครื่อง

                                    //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

                                    //connection = DriverManager.getConnection("jdbc:odbc:AdminDB");

                                    ////แบบ copy mysql-connector-java-3.1.12-bin.jar ไว้ที่ Web-inf/lib

                                    Class.forName("com.mysql.jdbc.Driver"); //การติดต่อโดยผ่าน Driver

                                    connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/employee?characterEncoding=utf-8","root","root");

//คำสั่งต่อต่อฐานข้อมูล MySql

 

                        } catch (ClassNotFoundException e) {

                                    e.printStackTrace();

                        } catch (SQLException e) {

                                    e.printStackTrace();

                        }

                        return connection;

            }          

            public Connection getConnection() {

                        return connection;

            }

            public void setConnection(Connection connection) {

                        this.connection = connection;

            }

            public void closeAdminDB() {

                                    try {

                                                connection.close();

                                    } catch (SQLException e) {

                                                e.printStackTrace();

                                    }

            }

}

 

Department.java

          Class Department ประกอบด้วย Attribute ซึ่งจัดเก็บรหัสและชื่อแผนก

package Employee;

 

import java.util.Vector;

 

public class Department {

            private int DeptID;

            private String DeptName;

            private Vector<Employee> Emps = new Vector<Employee>();

           

 

            public int getDeptID() {

                        return DeptID;

            }

            public Vector<Employee> getEmps() {

                        return Emps;

            }

            public void addEmps(Employee emps) {

                        Emps.addElement(emps);

            }

            public void setDeptID(int i) {

                        DeptID = i;

            }

            public String getDeptName() {

                        return DeptName;

            }

            public void setDeptName(String deptName) {

                        DeptName = deptName;

            }

            public String getDeptname() {

                        return DeptName;

            }

            public void setDeptname(String deptName) {

                        DeptName = deptName;

            }

 

}

 

Employee.java

Class Employee ประกอบด้วย Attribute ซึ่งจัดเก็บรหัสพนักงาน ชื่อสกุล เงินเดือน และแผนก

 

package Employee;

 

public class Employee {

              private int EmpID;

              private String EmpName;

              private float Salary;

              private int DeptID;

             

            public int getDeptID() {

                        return DeptID;

            }

            public void setDeptID(int deptID) {

                        DeptID = deptID;

            }

            public int getEmpID() {

                        return EmpID;

            }

            public void setEmpID(int empID) {

                        EmpID = empID;

            }

            public String getEmpName() {

                        return EmpName;

            }

            public void setEmpName(String empName) {

                        EmpName = empName;

            }

            public float getSalary() {

                        return Salary;

            }

            public void setSalary(float salary) {

                        Salary = salary;

            }

           

}

 

EmployeeManager.java

          Class EmployeeManager เป็น Model สำหรับการจัดการฐานข้อมูล เก็บคำสั่งในการคิวรี ติดต่อฐานข้อมูลไว้

package Employee;

import java.io.UnsupportedEncodingException;

import java.sql.*;

import java.util.*;

import connect.ConnectionDB;

 

public class EmployeeManager {

            public EmployeeManager(){

            }

           

            public int EmployeeLast(){ //คิวรีข้อมูลเพื่อค้นหารหัสพนักงานล่าสุด

                        try{

                                    ConnectionDB condb = new ConnectionDB();

                                    Connection con = condb.connectAdminDB();

                                    int empid = 0;

                                    String sql = "SELECT EmpID FROM employee ORDER BY empID DESC LIMIT 1";   

                                    Statement stmt = con.createStatement();

                                    ResultSet rs = stmt.executeQuery(sql); //เก็บผลการ select ไว้ในตัวแปร rs

                                    while(rs.next()){

                                                empid = rs.getInt(1);

                                    }

                                    rs.close();

                                    stmt.close();

                                    con.close();

                                    return empid;

                           }

                           catch(Exception ex){

                                    System.out.println("lab7_1>>EmployeeManager>>EmployeeLast()>>" + ex.getMessage());

                                    return 0;

                           }

            }

           

            public Hashtable<String, String> getDepartment(){

//คิวรีรหัสและชื่อแผนก จากฐานข้อมูลเก็บไว้ในตัวแปรประเภท Hashtable

                        try{

                                    ConnectionDB condb = new ConnectionDB();

                                    Connection con = condb.connectAdminDB();

                                    Hashtable<String, String> imDept = new Hashtable<String, String>();                              

                                    String sql ="SELECT DeptID, DeptName FROM department ORDER BY DeptID";                             

                                    Statement stmt = con.createStatement();

                                    ResultSet rs = stmt.executeQuery(sql); //เก็บผลการ select ไว้ในตัวแปร rs

                                    while(rs.next()){

                                                imDept.put(Integer.toString(rs.getInt(1)), rs.getString(2));                                               

                                    }

                                    rs.close();

                                    stmt.close();

                                    con.close();

                                    return imDept;

                           }

                           catch(Exception ex){

                                    System.out.println("lab7_1>>EmployeeManager>>getDepartment()>>" + ex.getMessage());

                                    return null;

                           }

                        }

 

           

            public ArrayList <Department> getAllEmployee(){ //คิวรีข้อมูลพนักงานทั้งหมด

                           try{

                                    ConnectionDB condb = new ConnectionDB();

                                    Connection con = condb.connectAdminDB();

                                    ArrayList<Department> listdept = new ArrayList <Department>();

                                    String sql ="SELECT DeptID, DeptName FROM department ORDER BY DeptID";                             

                                    Statement stmt = con.createStatement();

                                    ResultSet rs = stmt.executeQuery(sql); //เก็บผลการ select ไว้ในตัวแปร rs

                                    while(rs.next()){

                                                Department dept = new Department();

                                                dept.setDeptID(rs.getInt(1));

                                                dept.setDeptname(rs.getString(2));        

                                                String sql1 ="SELECT EmpID, EmpName, Salary FROM employee " +

                                                                                     "WHERE DeptID = " + dept.getDeptID() +

                                     " ORDER BY EmpID";

                                                Statement stmt1 = con.createStatement();

                                                // ใช้ ResultSet ใหม่เพื่อป้องกันการสิ้นสุดการทำงานของ ResultSet rs

                                                ResultSet rs1 = stmt1.executeQuery(sql1);

                                                while(rs1.next()){

                                                            Employee emp = new Employee();

                                                            emp.setEmpID(rs1.getInt(1));

                                                            emp.setEmpName(rs1.getString(2));

                                                            emp.setSalary(rs1.getFloat(3));

                                                            dept.addEmps(emp);                                                     

                                                }

                                                rs1.close();

                                                stmt1.close();

                                                listdept.add(dept);

                                    }

                                    rs.close();

                                    stmt.close();

                                    con.close();

                                    return listdept;

                           }

                           catch(Exception ex){

                                    System.out.println("lab7_1>>EmployeeManager>>getAllEmployee()>>" + ex.getMessage());

                                    return null;

                           }

                        }

           

            public Employee getEmployee(int id){ //คิวรีข้อมูลพนักงานเฉพาะรหัสที่เลือก

                           try{

                                    ConnectionDB condb = new ConnectionDB();

                                    Connection con = condb.connectAdminDB();

                                    Employee emp = new Employee();

                                    String sql = "SELECT EmpID, EmpName, Salary, DeptID FROM employee " +

                                                                         "WHERE EmpID = " + id;          

                                    Statement stmt = con.createStatement();

                                    ResultSet rs = stmt.executeQuery(sql); //เก็บผลการ select ไว้ในตัวแปร rs

                                    while(rs.next()){

                                                emp.setEmpID(rs.getInt(1));

                                                emp.setEmpName(rs.getString(2));

                                                emp.setSalary(rs.getFloat(3));

                                                emp.setDeptID(rs.getInt(4));     

                                    }

                                    rs.close();

                                    stmt.close();

                                    con.close();

                                    return emp;

                           }

                           catch(Exception ex){

                                    System.out.println("lab7_1>>EmployeeManager>>getEmployee()>>" + ex.getMessage());

                                    return null;

                           }

                        }

           

            public int removeEmployee(int id){ //การลบข้อมูลจากฐานข้อมูล

                        int success = 0;            

                        Connection con = new ConnectionDB().connectAdminDB();

                       

                        String remove = "DELETE FROM employee WHERE EmpID = " + id;

                        Statement stmt;

                        try{

                                    stmt = con.createStatement();

                                    success = stmt.executeUpdate(remove);

                        }catch (SQLException e){

                                    e.printStackTrace();

                        }

                        return success;

            }

           

            public int editEmployee(int id, String name, float sa, int dept){ //การ Update ฐานข้อมูล

                        int success = 0;

                        String thiName = name;

                       

                        try {

                                    thiName = new String( name.getBytes("ISO8859-1"), "UTF-8");

                        } catch (UnsupportedEncodingException e1) {

                                    // TODO Auto-generated catch block

                                    e1.printStackTrace();

                        }

                       

                        System.out.println(thiName);

                        Connection con = new ConnectionDB().connectAdminDB();

                        String edit = "UPDATE employee SET EmpName ='" + thiName + "', Salary=" + sa +

                                                                        ", DeptID=" + dept + " WHERE EmpID = " + id;

                        Statement stmt;

                        try{

                                    stmt = con.createStatement();

                                    success = stmt.executeUpdate(edit);

                        }catch (SQLException e){

                                    e.printStackTrace();

                        }

                        return success;

            }

            public boolean addEmployee(int id, String name, float sa, int dept){ //การ Insert ฐานข้อมูล

                        String thiName = name;

                       

                        try {

                                    thiName = new String( name.getBytes("ISO8859-1"), "UTF-8");

                        } catch (UnsupportedEncodingException e1) {

                                    // TODO Auto-generated catch block

                                    e1.printStackTrace();

                        }

                       

                        System.out.println(thiName);

                        Connection con = new ConnectionDB().connectAdminDB();

                        String sql = "INSERT INTO employee VALUES (" + id + ", '" + thiName + "', " + sa + ", " + dept + ")";

                        Statement stmt;

                        try{

                                    stmt = con.createStatement();

                                    stmt.execute(sql);

                                    return true;

                        }catch (SQLException e){

                                    e.printStackTrace();

                                    return false;

                        }                                  

            }

}

 

ThaiUtil.java

          Class ThaiUtil สำหรับการแปลงรหัสภาษาให้ภาษาไทย หรือรหัส ASCII CODE

package Employee;

 

public class ThaiUtil {

              /** Creates a new instance of ThaiUtil */

             

    public static String Unicode2ASCII(String unicode) {

 

        StringBuffer ascii = new StringBuffer(unicode);

 

        int code;

 

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

 

            code = (int)unicode.charAt(i);

 

            if ((0xE01<=code) && (code <= 0xE5B ))

 

                ascii.setCharAt( i, (char)(code - 0xD60));

 

        }

 

        return ascii.toString();

 

    }

 

    

 

    public static String ASCII2Unicode(String ascii) {

 

        StringBuffer unicode = new StringBuffer(ascii);

 

        int code;

 

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

 

            code = (int)ascii.charAt(i);

 

            if ((0xA1 <= code) && (code <= 0xFB))

 

                unicode.setCharAt( i, (char)(code + 0xD60));

 

        }

 

        return unicode.toString();

 

    }

 

 

}

 

          Servlet สำหรับการจัดการระหว่างหน้าเว็บกับส่วนประมวลผล โดยการทำงาน doGet หรือ doPost จะขึ้นอยู่กับ คำสั่ง <form> ใน Html ว่าเลือกใช้ Method “get” หรือ “post” 

EmployeeServlet.java

           

package Employee;

 

import java.io.IOException;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

 

 

/**

 * Servlet implementation class EmployeeServlet

 */

public class EmployeeServlet extends HttpServlet {

            private static final long serialVersionUID = 1L;

      

    /**

     * @see HttpServlet#HttpServlet()

     */

    public EmployeeServlet() {

        super();

        // TODO Auto-generated constructor stub

    }

 

            /**

             * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

             */

            protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                        // TODO Auto-generated method stub

                        response.setCharacterEncoding("UTF-8"); //คำสั่งให้แสดงผลเป็นภาษาไทย

                        String act = "";

 

                        EmployeeManager emp = new EmployeeManager();

                        ArrayList <Department> listDept = new ArrayList<Department>();

                       

                        if(request.getParameter("act")!=null) {

                                    act = request.getParameter("act");

                        }

                       

                        if(act!=""){

                                   

                                    if(act.equalsIgnoreCase("show"))

                                    {                      

                                                listDept = emp.getAllEmployee();

                                                request.setAttribute("list", listDept);

                                                goTo("/showAllEmp.jsp",request,response);

                                    }

                        }

            }

 

            /**

             * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

             */

            protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                        // TODO Auto-generated method stub

                       

            }

           

           

            protected void goTo(String url, HttpServletRequest request, HttpServletResponse response)

            { // Method สำหรับการกำหนดให้แสดงผลที่หน้าเว็บ url ใด

                        if (url != null)

                        {

                                    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);

                    try

                    {

                        dispatcher.forward(request, response);

                    }

                    catch (Exception e) {}                    

                }

            }

}

 

UpdateServlet.java

 

package Employee;

 

import java.io.IOException;

import java.util.ArrayList;

import java.util.Hashtable;

 

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

/**

 * Servlet implementation class UpdateServlet

 */

public class UpdateServlet extends HttpServlet {

            private static final long serialVersionUID = 1L;

      

    /**

     * @see HttpServlet#HttpServlet()

     */

    public UpdateServlet() {

        super();

        // TODO Auto-generated constructor stub

    }

 

            /**

             * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

             */

            protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                        // TODO Auto-generated method stub

                        int success = 0;

                        String act = "";

                        int id = 0;

                        EmployeeManager emp = new EmployeeManager();

                        ArrayList <Department> listDept = new ArrayList<Department>();

                        Hashtable<String, String> imDept = new Hashtable<String, String>();

                       

                        if(request.getParameter("act")!=null) {

                                    act = request.getParameter("act");

                        }

                        if(request.getParameter("id") != null) {

                                    id = Integer.parseInt(request.getParameter("id"));

                        }

                       

                        if(act!=""){                               

                                    if(act.equalsIgnoreCase("del")) {

                                                success = emp.removeEmployee(id);

                                                if(success>0)

                                                {                      

                                                            listDept = emp.getAllEmployee();

                                                            request.setAttribute("list", listDept);

                                                            goTo("/showAllEmp.jsp",request,response);

                                                }

                                    }

                                   

                                    if(act.equalsIgnoreCase("select"))

                                    {                                              

                                                imDept = emp.getDepartment();

                                                request.setAttribute("imDept",imDept);

                                               

                                                Employee imEmp = emp.getEmployee(id);                                              

                                                request.setAttribute("imEmp", imEmp);

                                                goTo("/editEmployee.jsp",request,response);

                                    }

                        }

            }

 

            /**

             * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

             */

            protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                        // TODO Auto-generated method stub

                        response.setCharacterEncoding("UTF-8");

                        String act = "";

                        int success = 0;

                        int id = 0;

                        EmployeeManager emp = new EmployeeManager();

                        ArrayList <Department> listDept = new ArrayList<Department>();

                       

                        if(request.getParameter("id") != null) {

                                    id = Integer.parseInt(request.getParameter("id"));

                        }

                       

                        if(request.getParameter("act")!=null) {

                                    act = request.getParameter("act");                                 

                        }

                       

                        if(act.equalsIgnoreCase("edit")){

                                    String name = request.getParameter("name");

                                    float salary = Float.parseFloat(request.getParameter("salary"));

                                    int dept = Integer.parseInt(request.getParameter("ddlDep"));

                                    success = emp.editEmployee(id, name, salary, dept);      

                                    if(success>0){

                                                listDept = emp.getAllEmployee();

                                                request.setAttribute("list", listDept);

                                                goTo("/showAllEmp.jsp",request,response);

                                    }

                        }

                       

            }

            protected void goTo(String url, HttpServletRequest request, HttpServletResponse response)

            {

                        if (url != null)

                        {

                                    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);

                    try

                    {

                        dispatcher.forward(request, response);

                    }

                    catch (Exception e) {}                    

                }

            }

}

 

 

 

 

          หวังว่าจะเป็นประโยชน์ต่อไปนะคะ ทั้งนี้รายละเอียดขอขอบคุณและขออนุญาตแสดงตัวอย่างจากแบบฝึกหัดซึ่งได้รับการถ่ายทอดความรู้จากคณาจารย์สาขาวิชาเทคโนโลยีสารสนเทศ คณะวิทยาศาสตร์ หลักสูตร ปริญญาใบที่สอง นะคะ

คำสำคัญ :
JAVA  OOAD  
กลุ่มบทความ :
หมวดหมู่ :
แชร์ :
https://erp.mju.ac.th/acticleDetail.aspx?qid=83
ความคิดเห็นทั้งหมด (0)
ไม่มีข้อมูลตามเงื่อนไขที่ท่านกำหนด
รายการบทความการแลกเปลี่ยนเรียนรู้หมวดหมู่ : กลุ่มงานเทคโนโลยีสารสนเทศ
ความรู้ที่ได้จากการศึกษาค้นคว้าด้วยตนเอง » Assignment in Microsoft Teams
การมอบหมายงาน (Assignment ) สำหรับนักศึกษาในห้องเรียน ระบบ Microsoft Teams สร้างความสะดวกในการมอบหมายงานให้กับนักศึกษา การส่งงาน และสามารถให้คะแนนนักศึกษาแต่ละคนในห้องเรียน สำหรับผู้ที่อยู่ในสถาบัน...
Assignment  Microsoft Teams  การมอบหมายงาน  การให้คะแนน     กลุ่มงานตามสมรรถนะบุคลากร   กลุ่มงานเทคโนโลยีสารสนเทศ
ผู้เขียน สุภาพรรณ อนุตรกุล  วันที่เขียน 26/3/2567 18:01:20  แก้ไขล่าสุดเมื่อ 28/3/2567 14:01:52   เปิดอ่าน 15  ครั้ง | แสดงความคิดเห็น 0  ครั้ง
ความรู้ที่ได้รับจากการเข้าร่วมประชุม/อบรม/สัมมนา » Google Sites สร้างเว็บไซต์ได้อย่างง่ายดาย
Google Sites คือโปรแกรมหนึ่งของ บริษััท Google ที่เปิดให้ สมาชิกของ Google สามารถ สร้างเว็บไซต์ของตนเองได้ฟรี โดยการสร้างเว็บไซต์ ด้วย Google Sites นั้น ทำได้ง่าย เหมือนกับการแก้ไข เอกสารธรรมดา ๆ ช...
Google Sites  เว็บไซต์     กลุ่มงานตามสมรรถนะบุคลากร   กลุ่มงานเทคโนโลยีสารสนเทศ
ผู้เขียน สุภาพรรณ อนุตรกุล  วันที่เขียน 11/3/2567 17:19:12  แก้ไขล่าสุดเมื่อ 28/3/2567 16:35:00   เปิดอ่าน 73  ครั้ง | แสดงความคิดเห็น 0  ครั้ง
การพัฒนาระบบสารสนเทศ » การกำหนดผลลัพธ์การเรียนรู้รายวิชา CLOs ผ่าน มคอ.3
หัวข้อนี้เน้น การออกแบบหลักสูตรให้ตอบสนองต่อผลการเรียนรู้ที่คาดหวังของหลักสูตร สะท้อนถึงความต้องการ ของผู้ที่เกี่ยวข้องครบทุกภาคส่วน และผลการเรียนรู้นั้นต้องครอบคลุมทั้งความรู้ ทักษะเฉพาะทางและทักษ...
CLO  ELO  PLO     กลุ่มงานตามสมรรถนะบุคลากร   กลุ่มงานเทคโนโลยีสารสนเทศ
ผู้เขียน สมนึก สินธุปวน  วันที่เขียน 18/11/2566 23:45:59  แก้ไขล่าสุดเมื่อ 28/3/2567 11:42:23   เปิดอ่าน 203  ครั้ง | แสดงความคิดเห็น 0  ครั้ง
การพัฒนาระบบสารสนเทศ » แอปพลิเคชันสำหรับช่วยในการจัดเรียนการสอน
การเรียนผ่านเครื่องมือช่วยสอนโดยใช้แอปพลิเคชันมีบทบาทในการส่งเสริมและช่วยเหลืออาจารย์และนักศึกษาเป็นอย่างมาก บทความนี้ได้รวบรวม 8 เครื่องมือช่วยสอนสำหรับอาจารย์ในการช่วยจัดการเรียนการสอนให้เกิดความ...
Canva  Kahoot  Nearpod  Plicker  Quizizz  Storyboard  Visme  Voxer     กลุ่มงานตามสมรรถนะบุคลากร   กลุ่มงานเทคโนโลยีสารสนเทศ
ผู้เขียน สมนึก สินธุปวน  วันที่เขียน 18/11/2566 23:01:13  แก้ไขล่าสุดเมื่อ 28/3/2567 17:05:48   เปิดอ่าน 186  ครั้ง | แสดงความคิดเห็น 0  ครั้ง