Tuesday, 10 March 2015

Select Data From Database And Cast Into Class Type In Java

In this example i select data from database and return these data. You simple call this method and cast into required class type.

Step-1

     public Personal_CoachingGS Generatepersonalinvoice(String Query) {
        Personal_CoachingGS pc = new Personal_CoachingGS();
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            connection = ConnectionConfiguration.getConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery("SELECT a.m_name,a.m_address,a.m_city,a.m_state,a.m_mobile,a.m_game_sdate,a.m_game_edate,a.m_no_classes,a.m_price_per_class,a.m_coach_name,a.m_total_amt,"
+" b.Game_Name,c.RC_ID,c.RC_Discount,c.RC_AddedDate,d.Com_Address,d.Com_Contact,d.Com_Email FROM pc_receipt c LEFT OUTER JOIN p_member_reg a ON c.RC_Stu_ID=a.m_id"
+" LEFT OUTER JOIN gameregistration b ON a.m_game=b.Game_ID LEFT OUTER JOIN companyregistration d ON d.Com_ID=a.m_com_id WHERE a.m_id='"+Query+"'");

            while (resultSet.next()) {
                //Personal_CoachingGS pc = new Personal_CoachingGS();
             
               // pc.setM_id(resultSet.getString("m_id"));
                pc.setM_name(resultSet.getString("m_name"));
                pc.setM_address(resultSet.getString("m_address"));
                pc.setM_mobile(resultSet.getString("m_mobile"));
                pc.setM_game(resultSet.getString("Game_Name"));
                pc.setM_game_sdate(resultSet.getString("m_game_sdate"));
                pc.setM_game_edate(resultSet.getString("m_game_edate"));
               
                pc.setM_no_classes(resultSet.getString("m_no_classes"));
                pc.setM_price_per_class(resultSet.getString("m_price_per_class"));
               
                pc.setM_coach_name(resultSet.getString("m_coach_name"));
                pc.setM_total_amt(resultSet.getString("m_total_amt"));
                pc.setM_dob(resultSet.getString("m_city")+" "+resultSet.getString("m_state"));
                pc.setM_country(resultSet.getString("RC_Discount"));
                pc.setM_state(resultSet.getString("RC_AddedDate"));
               
                pc.setM_city(resultSet.getString("Com_Address"));
               
                pc.setM_email(resultSet.getString("Com_Contact"));
                pc.setM_com_id(resultSet.getString("Com_Email"));
                pc.setM_id(resultSet.getString("RC_ID"));
               
                                           
                //memberdata.add(pc);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        return pc;
    }
    

Use Of ArrayList In Java

In this example we select data from database and add these data into a arraylist and return that list.
The page in which you want to use these data simply call this method and cast into required class type. This Example In Java Swing.

Step-1

    public List<Personal_CoachingGS> selectAllInvoice(String Query) {
        List<Personal_CoachingGS> memberdata = new ArrayList<Personal_CoachingGS>();
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            connection = ConnectionConfiguration.getConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery("SELECT a.m_id,a.m_name,a.m_total_amt,b.Game_Name,c.RC_ID,c.RC_Discount,c.RC_Tax,c.RC_TotalAmount,c.RC_AddedDate FROM pc_receipt c LEFT OUTER JOIN p_member_reg a ON c.RC_Stu_ID=a.m_id"
 +" LEFT OUTER JOIN gameregistration b ON a.m_game=b.Game_ID where a.m_com_id='"+Query+"' ORDER BY a.Sno DESC");

            while (resultSet.next()) {
                Personal_CoachingGS pc = new Personal_CoachingGS();
             
                pc.setM_id(resultSet.getString("m_id"));
                pc.setM_name(resultSet.getString("m_name"));
             
                pc.setM_game(resultSet.getString("Game_Name"));
                pc.setM_email(resultSet.getString("RC_ID"));
                pc.setM_game_sdate(resultSet.getString("RC_Discount"));
                pc.setM_game_edate(resultSet.getString("RC_Tax"));
                pc.setM_country(resultSet.getString("RC_TotalAmount"));
                pc.setM_state(resultSet.getString("RC_AddedDate"));
               
           
                pc.setM_total_amt(resultSet.getString("m_total_amt"));
             
                           
                memberdata.add(pc);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        return memberdata;
    }
   
Step-2


 DefaultTableModel model;
model=(DefaultTableModel)jTable1.getModel();
     jTable1.setAutoCreateRowSorter(true);
   
     PC_ReciptDB pcdb=new PC_ReciptDB();
     List<Personal_CoachingGS> pcgs=pcdb.selectAllInvoice(cid);
     for(Personal_CoachingGS data : pcgs)
     {
      model.insertRow(model.getRowCount(), new Object[]{data.getM_id(),data.getM_name(),data.getM_email(),data.getM_game(),df.format(Double.parseDouble(data.getM_total_amt())),data.getM_game_sdate(),data.getM_game_edate(),df.format(Double.parseDouble(data.getM_country())),data.getM_state()});
     }model.fireTableDataChanged();