Monday, October 18, 2021

Tutorial Java Web ZK Framework - Form Entry Part2 (Entry - Edit - Delete) Grid - Simple For Beginners #java

 

 
 
Tutorial Java Web ZK Framework - Form Entry Part2 (Entry - Edit - Delete) Grid
 
 

 

Create a simple form with simple code, and you can do it without having to have too complicated logic. :)

Here we try to help you understand that making an application is easy and does not need complicated steps.
 
 
A.  Form Design Customer.zul
 
<window id="win_customer" title="ENTRY CUSTOMER" 
border="normal" width="100%" use="com.apps.customer">

<grid height="150px" width="100%">
  <columns>
    <column width="30%"></column>
    <column width="70%"></column>
  </columns>
    <rows>
        <row>
            <label value="ID CUSTOMER"></label>
            <textbox id="idcust" width="100px"></textbox>
        </row>
        <row>
            <label value="NAME CUSTOMER"></label>
            <textbox id="namecust" width="100px"></textbox>
        </row>
        <row>
            <label value="ADDRESS"></label>
            <textbox id="address" width="100px"></textbox>
        </row>
        <row>
            <label value=""></label>
            <hbox>
            <button id="entry" label="ENTRY" onClick="win_customer.cleartext()"></button>
            <button id="save"  label="SAVE"  onClick="win_customer.saveitem(idcust.getValue(),namecust.getValue(),address.getValue())"></button>
            </hbox>
        </row>
    </rows>
</grid>

<listbox id="lstData" width="100%"  emptyMessage="No Items Match Your
  Search" onCreate="win_customer.viewlist()">
    <listhead>
        <listheader label="ID CUSTOMER" width="min"></listheader>   
        <listheader label="NAME CUSTOMER" width="400px"></listheader>   
        <listheader label="ADDRESS CUSTOMER" width="min"></listheader>   
        <listheader label="EDIT" width="100px"></listheader>   
        <listheader label="DELETE" width="100px"></listheader>   
    </listhead>
    </listbox>
  </window> 

B. Code Customer.java
 
package com.apps;

import com.apps.db.DBSQLConnection;
import com.google.common.util.concurrent.ExecutionError;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import org.zkoss.zul.*;
import org.zkoss.util.media.AMedia;
import org.zkoss.zk.ui.Sessions;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;

public class customer extends Window {
    String SQL;     
    private String actionType="Entry";
    public void saveitem(String cid, String cname, String caddress) throws Exception {
       Connection Conn = new DBSQLConnection().openConnection(); 
        if (actionType.equals("Edit")) {
             SQL="UPDATE CUSTOMER SET namecust=?, address=? WHERE IDCUST=? ";
		}else {       
			SQL="INSERT INTO  CUSTOMER (NAMECUST,ADDRESS,IDCUST) VALUES (?,?,?)";
		}
        PreparedStatement prep = Conn.prepareStatement(SQL);
        prep.setString(1, cname);
        prep.setString(2, caddress);
        prep.setString(3, cid);
        prep.executeUpdate();
        Conn.close();  
       
     Messagebox.show("Data has been saved successfully..","Data Saved",Messagebox.OK,Messagebox.INFORMATION);   
              
     viewlist();        
  
    }
    
    public void viewlist() throws Exception { 
        Listbox lb=(Listbox)this.getFellow("lstData");
        lb.getItems().clear();             
        SQL="SELECT * FROM CUSTOMER";
        Connection Conn = new DBSQLConnection().openConnection();
        Statement st = Conn.createStatement();  
        ResultSet rs = st.executeQuery(SQL);
  
        while (rs.next()){  
             Listitem li = new Listitem();           
             final String cidcust=rs.getString("idcust"); 
			
			/*CREATE BUTTON EDIT IN GRID*/  
             Button btn = new Button();  
             btn.setLabel("EDIT");  
             btn.setWidth("60px");  
             btn.setHeight("25px");
             btn.setIconSclass("z-icon-file-o");           
             btn.addEventListener(Events.ON_CLICK, new
             org.zkoss.zk.ui.event.EventListener() {
				@Override  
                public void onEvent(Event event) throws Exception { 
                    EditView(cidcust)
                }           
            });
            Listcell lc = new Listcell();
            lc.appendChild(btn);
            /*CREATE BUTTON DELETE IN GRID*/
             Button btndel = new Button();
             btndel.setLabel("DELETE");
             btndel.setWidth("80px");
             btndel.setHeight("25px");  
             btndel.setIconSclass("z-icon-file-o"); 
             btndel.addEventListener(Events.ON_CLICK, new
             org.zkoss.zk.ui.event.EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					DeleteView(cidcust);
                }           
            });
       
            Listcell lcdel = new Listcell();
            lcdel.appendChild(btndel);
            li.setValue(rs.getString("idcust"));
            li.appendChild(new Listcell(rs.getString("idcust")));
            li.appendChild(new Listcell(rs.getString("namecust")));
            li.appendChild(new Listcell(rs.getString("address")));
            li.appendChild(lc);
            li.appendChild(lcdel);
            lb.appendChild(li);
        }
        rs.close();
        st.close();
        Conn.close();
    }
  
     
    public void rptcustomer() throws Exception {
        Connection Conn = new DBSQLConnection().openConnection();
        Statement st = Conn.createStatement();
        ResultSet rs = st.executeQuery("SELECT * FROM CUSTOMER");
        Map param = new HashMap();
        String namafile="customerlist";

        String reportSrc  = Sessions.getCurrent().getWebApp().getRealPath("content/rpt/rptcustomer.jasper");
        String reportDest = Sessions.getCurrent().getWebApp().getRealPath("content/rpt/"+namafile);
        
        JRResultSetDataSource jasperReports = new JRResultSetDataSource(rs);
        JasperPrint print = JasperFillManager.fillReport(reportSrc,param,jasperReports);     
        JasperExportManager.exportReportToPdfFile(print, reportDest);
  
        Iframe iframe =(Iframe)this.getFellow("rpt");
        File f = new File(reportDest);
        byte[] buffer = new byte[(int) f.length()];
        FileInputStream fs = new FileInputStream(f);
        fs.read(buffer);  
        fs.close();
        ByteArrayInputStream is = new ByteArrayInputStream(buffer);
        AMedia amedia = new AMedia(namafile, "pdf", "application/pdf", is);
             
        iframe.setContent(amedia);
    }
  
     
    
    /*EDIT FORM CODE*/
    public void EditView(String cidcust)throws Exception {
        Textbox tidcust=(Textbox)this.getFellow("idcust");
        Textbox tnamecust=(Textbox)this.getFellow("namecust");
        Textbox taddress=(Textbox)this.getFellow("address");
  
        Connection Conn = new DBSQLConnection().openConnection();
        Statement st = Conn.createStatement();
        ResultSet rs = st.executeQuery("SELECT * FROM CUSTOMER WHERE IDCUST='"+cidcust+"'");
			while (rs.next()){
                tidcust.setValue(rs.getString("idcust"));
                tnamecust.setValue(rs.getString("namecust"));
                taddress.setValue(rs.getString("address"));
			}
         actionType="Edit";
    }
    
  
    /*DELETE RECORD FORM CODE*/
    public void DeleteView(String cidcust)throws Exception {
        SQL="DELETE FROM CUSTOMER WHERE IDCUST=?";
  
        Connection Conn = new DBSQLConnection().openConnection();
        PreparedStatement prep = Conn.prepareStatement(SQL);
        prep.setString(1, cidcust);
        prep.executeUpdate();
        Conn.close();
         
        Messagebox.show("Data has been Delete successfully..","Data Delete",Messagebox.OK,Messagebox.INFORMATION);   
               
        viewlist();        
  
    }
    
    /*CLEAR TEXT To ENTRY*/
    public void cleartext()throws Exception {
        Textbox tidcust=(Textbox)this.getFellow("idcust");
        Textbox tnamecust=(Textbox)this.getFellow("namecust");
        Textbox taddress=(Textbox)this.getFellow("address");
        tidcust.setValue("");
        tnamecust.setValue("");
        taddress.setValue("");
        tidcust.setFocus(true);
        actionType="Entry";
     }
}
  



C. Tutorial Youtube 





D. Note 

 

 

 

 

 

No comments:

Post a Comment

Free Templates Source Code Simple Project Java ZK Framework

  Free Templates Source Code Simple Project Java ZK Framework   ZK Framework aims to combine the simplicity and security from its server-cen...