Minggu, 13 Januari 2019

CRUD Web Service dengan Bootstrap Menggunakan Netbeans - Part 2 - Method Operasi CRUD

Implementasi CRUD Web Service (Client dan Server) dengan Bootstrap Menggunakan Netbeans



Setelah melakukan Persiapan Awal seperti membuat Database, Project Client dan Server seperti yang sudah dijelaskan di tutorial CRUD Web Service dengan Bootstrap - Part 1 - Persiapan Awal, maka saat ini kita sudah sampai di Part 2 yang akan membahas tentang Method Operasi CRUD Web Service ke dalam Project Server Siakad

Implementasi Method CRUD Web Service ke dalam Project Server

  1. Aktifkan project server Siakad, kemudian buka file Mahasiswa.java.
  2. Masukkan script berikut ke dalam file Mahasiswa.java.
  3. Script koneksi untuk mendefinisikan pemanggilan variable untuk koneksi pada class koneksiMySQL.
  4. koneksiMySQL kon = new koneksiMySQL();
        Connection con;
        PreparedStatement ps;
        ResultSet rs;
    
  5. Script method tampilMahasiswa(). Method ini digunakan untuk menampilkan data yang ada di database ke dalam bentuk tabel html.
  6. @WebMethod(operationName = "tampilMahasiswa")
    public List tampilMahasiswa() {
        List daftar = new ArrayList();
        String sql_daftar = "select * from mahasiswa";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/siakad", "root", "");
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(sql_daftar);
            while (rs.next()) {
                daftar.add("<td>" + rs.getInt("nim") + "</td><td>" + rs.getString("nama") + "</td><td>" + rs.getString("jurusan") + "</td><td>" + rs.getString("email") + "</td><td>" + rs.getString("alamat") + "</td><td><a href=\"edit-mhs.jsp?nim=" + rs.getInt("nim") + "\"><span class=\"glyphicon glyphicon-edit\"></span></a><br><a href=\"hapus-mhs.jsp?nim=" + rs.getInt("nim") + "\"><span class=\"glyphicon glyphicon-remove\"></span></a></td><tr></tr>");
            }
            con.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return daftar;
    }
    
  7. Script method addMahasiswa(). Method ini digunakan untuk menambahkan data ke dalam database mysql.
  8. @WebMethod(operationName = "addMahasiswa")
    public void addDosen(
        @WebParam(name = "nim") int nim,
        @WebParam(name = "nama") String nama,
        @WebParam(name = "jurusan") String jurusan,
        @WebParam(name = "email") String email,
        @WebParam(name = "alamat") String alamat
    ) {
        try {
            con = kon.getCon();
            ps = con.prepareStatement("insert into mahasiswa value (?,?,?,?,?)");
            ps.setInt(1, nim);
            ps.setString(2, nama);
            ps.setString(3, jurusan);
            ps.setString(4, email);
            ps.setString(5, alamat);
            ps.executeUpdate();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
    
  9. Script method getMahasiswa(). Method ini digunakan untuk menampilkan satu data tertentu berdasarkan nim kemudian ditampilkan dalam bentuk form.
  10. @WebMethod(operationName = "getMahasiswa")
    public List getMahasiswa(
        @WebParam(name = "nim") int nim) {
        List mahasiswa = new ArrayList();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/siakad", "root", "");
            Statement st = conn.createStatement();
            con = kon.getCon();
            ps = con.prepareStatement("select * from mahasiswa where nim=?");
            ps.setInt(1, nim);
            ResultSet rst = ps.executeQuery();
            while (rst.next()) {
                mahasiswa.add("<div class=\"form-group\">\n" +
                    " <label class=\"control-label col-sm-1\">NIM</label>\n" +
                    " <div class=\"col-sm-2\">\n" +
                    " <input type=\"text\" class=\"form-control\" name=\"nim\" value=\"" + rst.getInt("nim") + "\" readonly=\"true\">\n" +
                    " </div>\n" +
                    " </div>\n" +
                    " \n" +
                    " <div class=\"form-group\">\n" +
                    " <label class=\"control-label col-sm-1\">Nama</label>\n" +
                    " <div class=\"col-sm-2\">\n" +
                    " <input type=\"text\" class=\"form-control\" name=\"nama\" value=\"" + rst.getString("nama") + "\">\n" +
                    " </div>\n" +
                    " </div>\n" +
                    " \n" +
                    " <div class=\"form-group\">\n" +
                    " <label class=\"control-label col-sm-1\">Jurusan</label>\n" +
                    " <div class=\"col-sm-2\">\n" +
                    " <input type=\"text\" class=\"form-control\" name=\"jurusan\" value=\"" + rst.getString("jurusan") + "\">\n" +
                    " </div>\n" +
                    " </div>\n" +
                    " \n" +
                    " <div class=\"form-group\">\n" +
                    " <label class=\"control-label col-sm-1\">Email</label>\n" +
                    " <div class=\"col-sm-2\">\n" +
                    " <input type=\"text\" class=\"form-control\" name=\"email\" value=\"" + rst.getString("email") + "\">\n" +
                    " </div>\n" +
                    " </div>\n" +
                    " \n" +
                    " <div class=\"form-group\">\n" +
                    " <label class=\"control-label col-sm-1\">Alamat</label>\n" +
                    " <div class=\"col-sm-2\">\n" +
                    " <input type=\"text\" class=\"form-control\" name=\"alamat\" value=\"" + rst.getString("alamat") + "\">\n" +
                    " </div>\n" +
                    " </div>");
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return mahasiswa;
    }
    
  11. Script method editMahasiswa(). Method ini digunakan untuk mengubah suatu data dari database sesuai yang diubah di dalam form edit.
  12. @WebMethod(operationName = "editMahasiswa")
    public void editMahasiswa(
        @WebParam(name = "nim") int nim,
        @WebParam(name = "nama") String nama,
        @WebParam(name = "jurusan") String jurusan,
        @WebParam(name = "email") String email,
        @WebParam(name = "alamat") String alamat) {
        try {
            con = kon.getCon();
            ps = con.prepareStatement("update mahasiswa set nama=?,jurusan=?,email=?,alamat=? where nim=?");
            ps.setString(1, nama);
            ps.setString(2, jurusan);
            ps.setString(3, email);
            ps.setString(4, alamat);
            ps.setInt(5, nim);
            ps.executeUpdate();
        } catch (Exception e) {
            System.out.println("Failed to edit student because " + e.toString());
        }
    }
    
  13. Script method delMahasiswa(). Method ini digunakan untuk menghapus suatu data dari database berdasarkan nim.
  14. @WebMethod(operationName = "delMahasiswa")
    public void delMahasiswa(
        @WebParam(name = "nim") int nim) {
        try {
            con = kon.getCon();
            ps = con.prepareStatement("delete from mahasiswa where nim = ?");
            ps.setInt(1, nim);
            ps.executeUpdate();
        } catch (Exception e) {
            System.out.println("Failed to remove student because " + e.toString());
        }
    }
    
  15. Berikut adalah script lengkap dari file Mahasiswa.java.
  16. package siakad.mahasiswa;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;
    import javax.jws.WebService;
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    
    @WebService(serviceName = "Mahasiswa")
    public class Mahasiswa {
    
        koneksiMySQL kon = new koneksiMySQL();
        Connection con;
        PreparedStatement ps;
        ResultSet rs;
    	
        @WebMethod(operationName = "getMahasiswa")
        public List getMahasiswa(
            @WebParam(name = "nim") int nim) {
            List mahasiswa = new ArrayList();
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/siakad", "root", "");
                Statement st = conn.createStatement();
                con = kon.getCon();
                ps = con.prepareStatement("select * from mahasiswa where nim=?");
                ps.setInt(1, nim);
                ResultSet rst = ps.executeQuery();
                while (rst.next()) {
                    mahasiswa.add("<div class=\"form-group\">\n" +
                        " <label class=\"control-label col-sm-1\">NIM</label>\n" +
                        " <div class=\"col-sm-2\">\n" +
                        " <input type=\"text\" class=\"form-control\" name=\"nim\" value=\"" + rst.getInt("nim") + "\" readonly=\"true\">\n" +
                        " </div>\n" +
                        " </div>\n" +
                        " \n" +
                        " <div class=\"form-group\">\n" +
                        " <label class=\"control-label col-sm-1\">Nama</label>\n" +
                        " <div class=\"col-sm-2\">\n" +
                        " <input type=\"text\" class=\"form-control\" name=\"nama\" value=\"" + rst.getString("nama") + "\">\n" +
                        " </div>\n" +
                        " </div>\n" +
                        " \n" +
                        " <div class=\"form-group\">\n" +
                        " <label class=\"control-label col-sm-1\">Jurusan</label>\n" +
                        " <div class=\"col-sm-2\">\n" +
                        " <input type=\"text\" class=\"form-control\" name=\"jurusan\" value=\"" + rst.getString("jurusan") + "\">\n" +
                        " </div>\n" +
                        " </div>\n" +
                        " \n" +
                        " <div class=\"form-group\">\n" +
                        " <label class=\"control-label col-sm-1\">Email</label>\n" +
                        " <div class=\"col-sm-2\">\n" +
                        " <input type=\"text\" class=\"form-control\" name=\"email\" value=\"" + rst.getString("email") + "\">\n" +
                        " </div>\n" +
                        " </div>\n" +
                        " \n" +
                        " <div class=\"form-group\">\n" +
                        " <label class=\"control-label col-sm-1\">Alamat</label>\n" +
                        " <div class=\"col-sm-2\">\n" +
                        " <input type=\"text\" class=\"form-control\" name=\"alamat\" value=\"" + rst.getString("alamat") + "\">\n" +
                        " </div>\n" +
                        " </div>");
                }
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
            return mahasiswa;
        }
    	
        @WebMethod(operationName = "addMahasiswa")
        public void addDosen(
            @WebParam(name = "nim") int nim,
            @WebParam(name = "nama") String nama,
            @WebParam(name = "jurusan") String jurusan,
            @WebParam(name = "email") String email,
            @WebParam(name = "alamat") String alamat
        ) {
            try {
                con = kon.getCon();
                ps = con.prepareStatement("insert into mahasiswa value (?,?,?,?,?)");
                ps.setInt(1, nim);
                ps.setString(2, nama);
                ps.setString(3, jurusan);
                ps.setString(4, email);
                ps.setString(5, alamat);
                ps.executeUpdate();
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
    	
        @WebMethod(operationName = "delMahasiswa")
        public void delMahasiswa(
            @WebParam(name = "nim") int nim) {
            try {
                con = kon.getCon();
                ps = con.prepareStatement("delete from mahasiswa where nim = ?");
                ps.setInt(1, nim);
                ps.executeUpdate();
            } catch (Exception e) {
                System.out.println("Failed to remove student because " + e.toString());
            }
        }
    	
        @WebMethod(operationName = "editMahasiswa")
        public void editMahasiswa(
            @WebParam(name = "nim") int nim,
            @WebParam(name = "nama") String nama,
            @WebParam(name = "jurusan") String jurusan,
            @WebParam(name = "email") String email,
            @WebParam(name = "alamat") String alamat) {
            try {
                con = kon.getCon();
                ps = con.prepareStatement("update mahasiswa set nama=?,jurusan=?,email=?,alamat=? where nim=?");
                ps.setString(1, nama);
                ps.setString(2, jurusan);
                ps.setString(3, email);
                ps.setString(4, alamat);
                ps.setInt(5, nim);
                ps.executeUpdate();
            } catch (Exception e) {
                System.out.println("Failed to edit student because " + e.toString());
            }
        }
    	
        @WebMethod(operationName = "tampilMahasiswa")
        public List tampilMahasiswa() {
            List daftar = new ArrayList();
            String sql_daftar = "select * from mahasiswa";
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/siakad", "root", "");
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery(sql_daftar);
                while (rs.next()) {
                    daftar.add("<td>" + rs.getInt("nim") + "</td><td>" + rs.getString("nama") + "</td><td>" + rs.getString("jurusan") + "</td><td>" + rs.getString("email") + "</td><td>" + rs.getString("alamat") + "</td><td><a href=\"edit-mhs.jsp?nim=" + rs.getInt("nim") + "\"><span class=\"glyphicon glyphicon-edit\"></span></a><br><a href=\"hapus-mhs.jsp?nim=" + rs.getInt("nim") + "\"><span class=\"glyphicon glyphicon-remove\"></span></a></td><tr></tr>");
                }
                con.close();
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
            return daftar;
        }
    }
    
  17. Simpan semua file, kemudian deploy lalu silakan Test Web Service dan coba masukkan satu data ke dalam database.

Implementasi Method Operasi CRUD Web Service ke dalam Project Server selesai. Selanjutnya pada Part 3 akan membahas tentang bagaimana Konfigurasi Template Bootstrap untuk Project Client Web Service. Jika anda ingin melanjutkan, silakan klik tombol di bawah ini




Tutorial CRUD Web Service dengan Bootstrap Menggunakan Netbeans lainnya :

Implementasi CRUD Web Service dengan Bootstrap Menggunakan Netbeans

CRUD Web Service dengan Bootstrap - Part 1 - Persiapan Awal

CRUD Web Service dengan Bootstrap - Part 2 - Method Operasi CRUD

CRUD Web Service dengan Bootstrap - Part 3 - Konfigurasi Template Bootstrap

CRUD Web Service dengan Bootstrap - Part 4 - Client Tampil Data Mahasiswa

CRUD Web Service dengan Bootstrap - Part 5 - Client Tambah Data Mahasiswa

CRUD Web Service dengan Bootstrap - Part 6 - Client Hapus Data Mahasiswa

CRUD Web Service dengan Bootstrap - Part 7 - Client Edit Data Mahasiswa