Saturday, August 9, 2014

Maven + Hibernate + Setter + Getter


/**
 *
 * @author sakib4
 */
public class InvDclStockCategory  implements java.io.Serializable {
 
    private int id;
    private String description;
     private Date creatDate;
     private Date lastUpdateDate;
     private String inactive;

    public InvDclStockCategory() {
    }

   
    public InvDclStockCategory(int id) {
        this.id = id;
    }
    public InvDclStockCategory(int id, String description, Date creatDate,Date lastUpdateDate , String inactive ) {
       this.id = id;
       this.description = description;
       this.creatDate = creatDate;
       this.lastUpdateDate = lastUpdateDate;
       this.inactive = inactive;
    }
  
    public int getId() {
        return this.id;
    }
   
    public void setId(int id) {
        this.id = id;
    }
    public String getDescription() {
        return this.description;
    }
   
    public void setDescription(String description) {
        this.description = description;
    }
   
   
    public void setCreatDate(Date creatDate) {
        this.creatDate = creatDate;
    }
   
    public Date getCreatDate() {
       return this.creatDate = creatDate;
    }

   
    public Date getLastUpdateDate() {
        return this.lastUpdateDate;
    }
   
    public void setLastUpdateDate(Date lastUpdateDate) {
        this.lastUpdateDate = lastUpdateDate;
    }

    public String getInactive() {
        return this.inactive;
    }
   
    public void setInactive(String inactive) {
        this.inactive = inactive;
    }
   
}

Maven + Hibernate + Select & Result List()

Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Query q = session.createQuery(hql);
            List resultList = q.list();
            displayResult(resultList);
            session.getTransaction().commit();

private void displayResult(List resultList) {
        Vector<String> tableHeaders = new Vector<String>();
        Vector tableData = new Vector();
        tableHeaders.add("Id");
        tableHeaders.add("Category Name");
        tableHeaders.add("Status");
        tableHeaders.add("LastUpdate Date");

        for (Object o : resultList) {
            InvDclStockCategory Cat = (InvDclStockCategory) o;
            Vector<Object> oneRow = new Vector<Object>();
            oneRow.add(Cat.getId());
            oneRow.add(Cat.getDescription());
            oneRow.add(Cat.getInactive());
            oneRow.add(Cat.getLastUpdateDate());
         
            tableData.add(oneRow);
        }
        resultTable.setModel(new DefaultTableModel(tableData, tableHeaders));

    }

Maven + Hibernate Session Delete

 Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();     
   
                Student userAdd =  (Student)session.get(Student.class, Id);
             
                session.delete(userAdd);
                session.getTransaction().commit();
                session.close();

Maven + Hibernate Session Update

      Session session = HibernateUtil.getSessionFactory().openSession();
          session.beginTransaction();     
 InvDclStockCategory cat = InvDclStockCategory)session.get(InvDclStockCategory.class, Id);
  
                cat.setDescription(Name);
                cat.setInactive(Status);
                
                session.update(cat);
                session.getTransaction().commit();
                session.close();

Maven + Hibernate Session Save

Session session = HibernateUtil.getSessionFactory().openSession();
          session.beginTransaction();     
 
                InvDclStockCategory cat = new InvDclStockCategory();
  
                cat.setDescription(Name);
                cat.setInactive(Status);
                session.save(cat);
                session.getTransaction().commit();
                session.close();
               

Maven + Hibernate Mappings + Auto Increment Sequence Uses

 <!-- hbm.xml files settings -->

<hibernate-mapping>
    <class name="com.datacraft.mavenproject1.InvDclStockCategory" table="inv_dcl_stock_category" schema="public">
        <id name="id" type="int">
            <column name="id" />
               
             <generator class="sequence">
                 <param name="sequence">inv_dcl_stock_category_id_seq</param>
             </generator>
         
        </id>
        <property name="description" type="string">
            <column name="description" length="255" />
        </property>
      
        <property name="inactive" type="string">
            <column name="inactive" />
        </property>
        <property name="creatDate" type="date">
            <column name="create_date" length="13"  />
        </property>
        <property name="lastUpdateDate" type="date">
            <column name="last_update_date" length="13" />
        </property>
    </class>

Thursday, June 26, 2014

Java Regular Expressions common matching symbols

Regular Expression                              Description                                      Example

. Matches any single character          (“..”, “a%”) – true(“..”, “.a”) – true
                                                                                                (“..”, “a”) – false

^xxx     Matches xxx regex at the beginning of the line        (“^a.c.”, “abcd”) – true
                                                                                         (“^a”, “ac”) – false

xxx$   Matches regex xxx at the end of the line   (“..cd$”, “abcd”) – true(“a$”, “a”) – true
                                                                               (“a$”, “aca”) – false

[abc] Can match any of the letter a, b or c. [] are known as character classes.
                                                        (“^[abc]d.”, “ad9″) – true(“[ab].d$”, “bad”) – true
                                                        (“[ab]x”, “cx”) – false

[abc][12] Can match a, b or c followed by 1 or 2  
                                              (“[ab][12].”, “a2#”) – true(“[ab]..[12]“, “acd2″) – true
                                               (“[ab][12]“, “c2″) – false

[^abc] When ^ is the first character in [], it negates the pattern,
                                                                             matches anything except a, b or c
                                          (“[^ab][^12].”, “c3#”) – true(“[^ab]..[^12]“, “xcd3″) – true
                                          (“[^ab][^12]“, “c2″) – false

[a-e1-8] Matches ranges between a to e or 1 to 8
                                                           (“[a-e1-3].”, “d#”) – true(“[a-e1-3]“, “2″) – true
                                                           (“[a-e1-3]“, “f2″) – false

xx|yy  Matches regex xx or yy (“x.|y”, “xa”) – true(“x.|y”, “y”) – true
                                                      (“x.|y”, “yz”) – false


Wednesday, June 25, 2014

Java Interface OOP Concept

package java_mysql;

/**
 *
 * @author sakib
 */
public interface MySQL_Interface {
 
 
  public  String Insert_Store_Procedure(String Table_Fields,String Table_Name,String DB_Name);
 
  public  String Update_Store_Procedure(String Table_Fields,String Table_Name,String DB_Name);
 
  public  String Delete_Store_Procedure(int Table_Field,String Table_Name,String DB_Name);
 
  public  String Select_Store_Procedure(String Table_Fields,String Table_Name);
 
  public  String Create_Table(String Table_Fields,String Table_Name);
 
  public  String Insert_Trigger(String DB_Table_Fields,String Table_Name );
 
  public  String Update_Trigger(String DB_Table_Fields,String Trigger_Name );
 
  public  String Delete_Trigger(String DB_Table_Fields,String Trigger_Name );
 
   
}

Tuesday, June 10, 2014

Junit test for testing methods of Java

package junit_test;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Sakib
 */
public class UtilsTest {
 
    public UtilsTest() {
    }
 
    @BeforeClass
    public static void setUpClass() {
    }
 
    @AfterClass
    public static void tearDownClass() {
    }
 
    @Before
    public void setUp() {
    }
 
    @After
    public void tearDown() {
    }

        /**
     * Test of percentage calculate, of class Utils.
     */
    @Test
    public void testgetPercent() {
     
        double rate = 10;
        double amount = 500;
        double expResult = 50;
        double result = Utils.percentage(rate,amount);
        assertEquals(expResult, result,2);
          }
 
}
Result :Test Passed

Monday, February 10, 2014

jTable Clear All Rows


        clear(); // clear crm contacts
        DefaultTableModel dtm = (DefaultTableModel) jTable_Contact.getModel();
        dtm.setRowCount(0);

Java Oracle Get Generated Key

 ResultSet result = null;
                                   java.sql.Statement pst = conn.createStatement();
                           
                            pst.executeUpdate("INSERT INTO sales_cust_branch (branch_code,debtor_no, br_name, branch_ref, br_address, area, area_details, salesman, sales_man_details, contact_name, default_inventory_location, inv_loc_details, tax_group_id, tax_group_details, sales_account, sales_discount_account,receivables_account, payment_discount_account, sales_account_details, sales_discount_acc_details, receivables_account_details, payment_discount_acc_details, default_ship_via, default_ship_via_details, disable_trans,br_post_address, group_no, group_details, notes, inactive ,company_id, user_id, login_ip,insert_date,pc_user) VALUES "
                                               + "(SALES_DEBTORS_BRANCH.nextval,'"+ debator_no + "','"  + Branch_Name+ "','"  + Branch_Short_Name + "','"  + Billing_Add + "','"  + Sales_Area + "','"  + Sales_Area_Details + "','"  + Sales_Person + "','"  + Sales_Person_Details + "','"  + Contact_Person + "','"  + Def_Inv_Loc + "','"  + Def_Inv_Loc_Details + "','"  + Tax_Group + "','"  + Tax_Group_Details + "','"  + Sales_Ac_Id + "','"  + Sales_Dis_Id + "','"  + Sales_Rcv_Id + "','"  + Sales_Py_Dis_Id + "','"  + Sales_Account + "','"  + Sales_Discount_Account + "','"  + Accounts_Receiveable_Account + "','"  + Promot_Payment_Discount_Account + "','"  + Def_Ship_Com + "','"  +  Def_Ship_Com_Details+ "','"  + 0 + "','"  + Mailing_Add + "','"  +  Sales_Group_Id+ "','"  + Sales_Group_Details + "','"  + Br_General_Notes + "','"  + I_S_Br + "','"  + Dashboard.company_id + "','"  + Dashboard.user_id + "','"  + u.Get_Ip() + "',TO_DATE('"  + u.todays_datetime()+ "', 'yyyy-mm-dd hh24:mi:ss'),'"  + System.getProperty("user.name") + "')", new String [] {"branch_code"});

                            result = pst.getGeneratedKeys();
                            result.next();
                            branch_id = String.valueOf(result.getLong(1));
 System.out.println(branch_id );
Result: 25

Saturday, January 25, 2014

jTable Row Value Tool Tip

jTable_direct_delivery = new javax.swing.JTable()
{

    //JTable

    //Implement table cell tool tips.        
    public String getToolTipText(MouseEvent e) {
        String tip = null;
        java.awt.Point p = e.getPoint();
        int rowIndex = rowAtPoint(p);
        int colIndex = columnAtPoint(p);
        int realColumnIndex = convertColumnIndexToModel(colIndex);

        try {
            // if (realColumnIndex == 2 && rowIndex!=0) { //comment row, exclude heading
                //     tip = getValueAt(rowIndex, colIndex).toString();
                // }
            if (realColumnIndex == 9 ) { //comment row, exclude heading
                tip = "Dispatch";
            }
            if (realColumnIndex == 10 ) { //comment row, exclude heading
                tip = "Edit";
            }

            if (realColumnIndex == 2 && rowIndex >=0 ) { //comment row, exclude heading
                tip = getValueAt(rowIndex, colIndex).toString();
            }
        } catch (RuntimeException e1) {
            //catch null pointer exception if mouse is over an empty line
        }

        return tip;
    }

}
;

jTable Hand Cursor Mouse Hover

 private void jTable_direct_deliveryMouseMoved(java.awt.event.MouseEvent evt) {                                                
        if(jTable_direct_delivery.columnAtPoint(evt.getPoint())==7 ||  jTable_direct_delivery.columnAtPoint(evt.getPoint())==8 )
                    {
                        setCursor(Cursor.HAND_CURSOR);

                    }
                    else
                    {
                        setCursor(0);
                    }
    }   

jTable Row value with Image Symbol

 //get table model
            DefaultTableModel model = (DefaultTableModel) jTable_direct_delivery.getModel();
            model.addRow(new Object[]{
                item_code, item_description, quantity, unit, price,
                discount, line_total });
               jTable_direct_delivery.setAutoResizeMode(jTable_direct_delivery.AUTO_RESIZE_OFF);
              jTable_direct_delivery.getColumnModel().getColumn(0).setPreferredWidth(70);
              jTable_direct_delivery.getColumnModel().getColumn(1).setPreferredWidth(270);
              jTable_direct_delivery.getColumnModel().getColumn(2).setPreferredWidth(50);
              jTable_direct_delivery.getColumnModel().getColumn(3).setPreferredWidth(52);
              jTable_direct_delivery.getColumnModel().getColumn(4).setPreferredWidth(110);
              jTable_direct_delivery.getColumnModel().getColumn(5).setPreferredWidth(78);
              jTable_direct_delivery.getColumnModel().getColumn(6).setPreferredWidth(90);
   
               TableColumn someColumn = jTable_direct_delivery.getColumnModel().getColumn(7);
                someColumn.setCellRenderer(new IconifiedRenderer(new ImageIcon(getClass().getResource("/images/edit.gif"))) );
               TableColumn someColumn1 = jTable_direct_delivery.getColumnModel().getColumn(8);
                 someColumn1.setCellRenderer(new IconifiedRenderer(new ImageIcon(getClass().getResource("/images/remove.png"))) );

Get Return Key SQL Insert Auto ID

  pst.executeUpdate("INSERT INTO sales_dcl_order_details ("
                                        + "order_no, trans_type, stk_code, description, unit,"
                                        + "unit_price, quantity, discount_percent, insert_date ) VALUES "
                                        + "('"+ max_id + "','"  + 30+ "','"  + item_code + "','"  + item_desc + "','"  + item_unit + "'"
                                        + ",'"  + item_price + "','"  + item_quentity + "','"  + item_discount + "','"  + u.todays_datetime() + "')",Statement.RETURN_GENERATED_KEYS);
                                     
                                        generatedKeys = pst.getGeneratedKeys();
                                        if(generatedKeys.next()){
                                        src_id = generatedKeys.getString(1);
                                        }