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();


Thursday, 26 February 2015

Create Excel File in java

Create Excel File in java 

In this example I select data from database and write that data into a Excel file.


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.poi.hpsf.HPSFException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.sql.*;



public class tntWriteTOExcel {

    public static void excel() throws HPSFException {
      try
      {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
       
        int no=OtherFunctions.RandomNumber();
               
        ArrayList data = new ArrayList();
        ArrayList headers = new ArrayList();

        File file123 = new File("E:\\MemberList.xls");

        headers.add("Student ID");
        headers.add("CardID");
        headers.add("FirstName");
       
        headers.add("Middle Name");
        headers.add("Last Name");
        headers.add("DOB");
       
           
         connection = ConnectionConfiguration.getConnection(); //This my database connection.

         preparedStatement = connection.prepareStatement("SELECT * FROM studentregistration");
         resultSet = preparedStatement.executeQuery();  

        //for (int i = 0; i <= 5; i++) {
       
         while(resultSet.next())
         {
            ArrayList cells = new ArrayList();
            cells.add(resultSet.getString("Stu_ID"));
            cells.add(resultSet.getString("Stu_CardID"));
            cells.add(resultSet.getString("Stu_FirstName"));
           
            cells.add(resultSet.getString("Stu_MidName"));
            cells.add(resultSet.getString("Stu_LastName"));
            cells.add(resultSet.getString("Stu_DOB"));
           
         
           
            data.add(cells);
        }

        exportToExcel("Test", headers, data, file123);
    }
   
    catch(Exception Ex)
    {
        System.out.println("Export to Excel "+Ex);
    }
            }
    public static void exportToExcel(String sheetName, ArrayList headers,
            ArrayList data, File outputFile) throws HPSFException {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet(sheetName);

        int rowIdx = 0;
        short cellIdx = 0;

        // Header
        HSSFRow hssfHeader = sheet.createRow(rowIdx);
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        for (Iterator cells = headers.iterator(); cells.hasNext();) {
            HSSFCell hssfCell = hssfHeader.createCell(cellIdx++);
            hssfCell.setCellStyle(cellStyle);
            hssfCell.setCellValue((String) cells.next());
        }
        // Data
        rowIdx = 1;
        for (Iterator rows = data.iterator(); rows.hasNext();) {
            ArrayList row = (ArrayList) rows.next();
            HSSFRow hssfRow = sheet.createRow(rowIdx++);
            cellIdx = 0;
            for (Iterator cells = row.iterator(); cells.hasNext();) {
                HSSFCell hssfCell = hssfRow.createCell(cellIdx++);
                hssfCell.setCellValue((String) cells.next());
            }
        }

        wb.setSheetName(0, sheetName, HSSFWorkbook.ENCODING_COMPRESSED_UNICODE);
        try {
            FileOutputStream outs = new FileOutputStream(outputFile);
            wb.write(outs);
            outs.close();
            System.out.println("success");
        } catch (IOException e) {
          throw new HPSFException(e.getMessage());
        }
    }
   

    }



MySQL Database Connection


MySQL Database Connection 



import java.sql.*;
/**
 *
 * @author Rajkumar
 */
public class ConnectionConfiguration {
     //public static final String URL = "jdbc:mysql://localhost:3306/passerin_crm";
   public static final String URL = "jdbc:mysql://localhost:3306/demopasserine_crm";
//    /**
//     * In my case username is "root" *
//     */
    public static final String USERNAME = "root";
//    /**
//     * In my case password is "1234" *
//     */
    public static final String PASSWORD = "root";
//
  //  public static final String URL = "jdbc:mysql://passerinegroup.com:3306/passerin_crm";
    /**
     * In my case username is "root" *
     */
 //   public static final String USERNAME = "passerin_crm";
    /**
     * In my case password is "1234" *
     */
  //  public static final String PASSWORD = "crm@!@#";
//////
   
    public static Connection getConnection() {
        Connection connection = null;

        try {
             Class.forName("com.mysql.jdbc.Driver");
             System.out.println("Connectinggggggg.....");
             connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
             System.out.println("Connected..");
           
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return connection;
    }

}

Thursday, 25 December 2014

Mail Function in PHP

mail.php

<?php
ob_start();



$email = trim($_GET['j_email']);


$support_address = "rajkumarsahoo@yahoo.com"; //Your Company Email ID//

$headers2 = "From: ".$support_address;

if ( $email == "" )

{
echo "Name is empty";
}
else
{

// Your Auto Replay Message//
mail($email,"Thank You For Contact ","Thank You We Will Contact You Shortly");
//unset($email);
echo "<script>
                                                          window.location.href = 'contact_full.php';
                                                          </script>";
}

?>

Create Process Bar Like Gmail In HTML

index.html

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>DEMO - Gmail like progress bar with CSS and jQuery</title>
<script type='text/javascript'
src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<style>
body {
background: rgb(245, 245, 245);
}

.g-div {
position: absolute;
top: calc(50% -   6px);
left: calc(50% -   160px);
text-align: center;
width: 320px;
font-weight: bold;
}

.loader-gmail {
padding: 1px;
overflow: hidden;
border: 1px solid #a4a4a4;
}

.progress {
width: 320px;
height: 8px;
background-image: linear-gradient(135deg, #6187f2 0%, #6187f2 25%, #5679da 25%, #5679da
50%, #6187f2 50%, #6187f2 75%, #5679da 75%, #5679da 100%);
background-repeat: repeat;
background-position: 0px 0px;
background-size: 16px 16px;
background-clip: content-box;
animation: loading 1s linear infinite;
-o-animation: loading 1s linear infinite;
-moz-animation: loading 1s linear infinite;
-webkit-animation: loading 1s linear infinite;
}

@keyframes loading {
    from {
      background-position:0px 0px;
    }
    to {
 background-position: -16px 0px;
    }
}
@-webkit-keyframes loading {
    from {
      background-position:0px 0px;
    }
    to {
 background-position: -16px 0px;
    }
}
@-moz-keyframes loading {
    from {
      background-position:0px 0px;
    }
    to {
 background-position: -16px 0px;
    }
}
@-o-keyframes loading {
    from {
      background-position:0px 0px;
    }
    to {
 background-position: -16px 0px;
    }
}
</style>

<script>
    var p=0;
    function timeout_trigger() {
  $(".progress").css("max-width",p+"%");
  $(".progress-view").text(p+"%");
  if(p!=100) {
  setTimeout('timeout_trigger()', 50);
  }
  p++;
}
    timeout_trigger();
  </script>
</head>
<body>
<div style="width:730px;margin:auto">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- demos -->
<ins class="adsbygoogle"
     style="display:inline-block;width:728px;height:90px"
     data-ad-client="ca-pub-4537529140156254"
     data-ad-slot="4444050329"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>

<div class="g-div">
Loading <span class="progress-view">0%</span>
<div class="loader-gmail">
<div class="progress"></div>
</div>
</div>


</body>


</html>

Create Dynamic URL in jsp

1.index.jsp page

<html>
  <head>
    <title>the c:url action (1)</title>
  </head>
  <body>
    This page takes 3 values that you specify, and forwards them to another JSP.
    That JSP will create a URL to another page, that then extracts the
    parameters and displays them.
    <p />
      <form action="createURL.jsp" method="post">
        <table>
          <tr><td>Enter name:</td>
              <td><input type="text" name="name"   /></td></tr>
          <tr><td>Enter age:</td>
              <td><input type="text" name="age"    /></td></tr>
          <tr><td>Enter gender:</td >
              <td><input type="text" name="gender" /></td></tr>
        </table>
        <input type="submit" value="Submit details" />
      </form>
  </body>

</html>


2. createURL.jsp

<%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%>

<c:url value="displayValues.jsp" var="displayURL">
  <c:param name="nameParam"   value="${param.name}" />
  <c:param name="ageParam"    value="${param.age}" />
  <c:param name="genderParam" value="${param.gender}" />
</c:url>

<html>
  <head>
    <title>the c:url action (2)</title>
  </head>
  <body>
    This page receives the values you specified, and creates a URL that contains
    them.
    <p />
    The generated URL is <c:out value="${displayURL}" />. <p/>
    Click <a href='<c:out value="${displayURL}" />'>here</a> to view the it.
  </body>

</html>

3.displayValues.jsp

<%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%>

<html>
  <head>
    <title>the c:url action (3)</title>
  </head>
  <body>
    <h3>List of query string parameters:</h3>
    <ul>
      <c:forEach items="${param}" var="currentParam">
        <li><c:out value="${currentParam.key}" />
            = <c:out value="${currentParam.value}" /></li>
      </c:forEach>
    </ul>
 </body>
</html>