Welcome

Friday, 25 January 2019

Method CRUD pada Web Service Java

Method 
Dalam membangun sebuah web servise kita butuh sebuah database, disini misal kita buat Database
db_mahasiswa

 Class Koneksi.java

//@author Yudho Setyawan


public class konekmakul {
    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/db_mahasiswa";
    String usern = "root";
    String passw = "";
    Connection con;
    PreparedStatement ps;
    ResultSet rs;
   
    public Connection getCon(){
        try{
            Class.forName(driver);
            con = DriverManager.getConnection(url, usern, passw);
        }catch(Exception ex) {
            System.out.println(ex.getMessage());
        }
        return con;
    }
}

 

//Method Tambah 

@WebMethod(operationName = "addmakul")
    public void addnilai(
            @WebParam(name = "kode_matakuliah") int kode_matakuliah,
            @WebParam(name = "matakuliah") String matakuliah,
            @WebParam(name = "sks") String sks
           
    ) {
        try {
            con = kon.getCon();
//Nama tabel makul
            ps = con.prepareStatement("insert into makul value (?,?,?)");
            ps.setInt(1, kode_matakuliah);
            ps.setString(2, matakuliah);
            ps.setString(3, sks);
           
            ps.executeUpdate();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

//Method Hapus
 @WebMethod(operationName = "delmakul")
    public void delmakul(
            @WebParam(name = "kode_matakuliah") int kode_matakuliah) {
        try {
            con = kon.getCon();
            ps = con.prepareStatement("delete from makul where kode_matakuliah = ?");
            ps.setInt(1, kode_matakuliah);
            ps.executeUpdate();
        } catch (Exception ex) {
            System.out.println("Failed to remove student because " + ex.toString());
        }
    }

//Method Edit
@WebMethod(operationName = "editmakul")
    public void editmakul(
            @WebParam(name = "kode_matakuliah") int kode_matakuliah,
            @WebParam(name = "matakuliah") String matakuliah,
            @WebParam(name = "sks") String sks
          ) {
        try {
            con = kon.getCon();
            ps = con.prepareStatement("update makul set matakuliah=?,sks=? where kode_matakuliah=?");
           
            ps.setString(1, matakuliah);
            ps.setString(2, sks);
            ps.setInt(3, kode_matakuliah);
            ps.executeUpdate();
        } catch (Exception ex) {
            System.out.println("Failed to edit student because " + ex.toString());
        }
    }

//Method Tampil
@WebMethod(operationName = "tampilmakul")
    public List tampilmakul1() {
        List daftar = new ArrayList();

        String sql_daftar = "select * from makul";

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_mahasiswa", "root", "");
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(sql_daftar);
            while (rs.next()) {
                daftar.add("<td>" + rs.getInt("kode_matakuliah") + "</td><td>" + rs.getString("matakuliah") + "</td><td>" + rs.getString("sks") + "</td></tr>");
            }
            con.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return daftar;
    }
   
}

//Method Cari
 @WebMethod(operationName = "getmakul")
    public List getmakul(
            @WebParam(name = "kode_matakuliah") int kode_matakuliah) {
        List makul = new ArrayList();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_mahasiswa", "root", "");
            Statement st = conn.createStatement();
            con = kon.getCon();
            ps = con.prepareStatement("select * from makul where kode_matakuliah=?");
            ps.setInt(1, kode_matakuliah);
            ResultSet rst = ps.executeQuery();
            while (rst.next()) {
                makul.add("<div class=\"form-group\">\n" +
"            <label class=\"control-label col-sm-1\">Kode MataKuliah</label>\n" +
"            <div class=\"col-sm-2\">\n" +
"                            <input type=\"text\" class=\"form-control\" name=\"kode_matakuliah\" value=\""+rst.getInt("kode_matakuliah")+"\" readonly=\"true\">\n" +
"                        </div>\n" +
"        </div>\n" +
"                \n" +
"                <div class=\"form-group\">\n" +
"            <label class=\"control-label col-sm-1\">Matakuliah</label>\n" +
"            <div class=\"col-sm-2\">\n" +
"                            <input type=\"text\" class=\"form-control\" name=\"matakuliah\" value=\""+rst.getString("matakuliah")+"\">\n" +
"                        </div>\n" +
"        </div>\n" +
"                \n" +
"                <div class=\"form-group\">\n" +
"            <label class=\"control-label col-sm-1\">Sks</label>\n" +
"            <div class=\"col-sm-2\">\n" +
"                <input type=\"text\" class=\"form-control\" name=\"sks\" value=\""+rst.getString("sks")+"\">\n" +
"                        </div>\n" +

             
"        </div>");

No comments:

Post a Comment

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();
Designed By Yudho C Setyawan