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;
    }
    

0 comments:

Post a Comment