Create Jasper Report in NetBeans



STEP 1: Download ireport plugin for Netbeans

STEP 2: Install plugin in Netbeans
·        Click tools --> select option  plugin


·        Select Downloaded tab --> click Add Plugin Button --> Browse and select your plugin from system location --> click open button
·        After that Click Install button --> Next --> Install --> Validation warning dialog box appear  Click Continue Button --> click finish

STEP3: Right click your project and select Empty report


STEP 4: select this database icon and create database link

STEP 5: Create Sample Jasper Report and select database icon next to preview button

  •  After that drag and drop text field
  • Right click -->select Edit expression and
  • Select your table field here


STEP 6: Call Jasper Report from Java code with database  connection
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import net.sf.jasperreports.engine.*;

import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.view.JasperViewer;
/**
 *
 * @author ganesh
 */
public class anand {
    public static void main(String[] args) throws IOException
    {
          try
        {
      
//Specify Your Report Path
JasperReport jasperReport=JasperCompileManager.compileReport("Report\\report3.jrxml");
        
            Class.forName("com.mysql.jdbc.Driver");

            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tally" , "root", "root");
Statement stmt = null;
ResultSet rset = null;
String queryString = "SELECT category.`accountno` AS category_accountno, category.`name` AS category_name,category.`type` AS category_type FROM `category` category WHERE accountno BETWEEN 100 and 111";
stmt = conn.createStatement();
rset = stmt.executeQuery(queryString);
 JRResultSetDataSource jasperReports = new JRResultSetDataSource(rset);

            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,null, jasperReports);
//Report saved in specified path
            JasperExportManager.exportReportToPdfFile(jasperPrint,"D:\\b1.pdf");
//Report open in Runtime
          Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " +"D:\\b1.pdf");
JRXlsExporter exporterXLS = new JRXlsExporter();
OutputStream output = new FileOutputStream(new File("D:\\b1.xls"));
 exporterXLS.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
 exporterXLS.setParameter(JRExporterParameter.OUTPUT_STREAM, output);
 exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_COLLAPSE_ROW_SPAN, Boolean.TRUE);
 exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, Boolean.TRUE);
 exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
 exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
 exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_DETECT_CELL_TYPE, Boolean.FALSE);
 exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
 exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_IGNORE_GRAPHICS, Boolean.TRUE);
 exporterXLS.exportReport();
            //JasperViewer.viewReport(jasperPrint);

        }
 catch (JRException e)
        {
            e.printStackTrace();
        }
        catch (ClassNotFoundException ex)
        {
            ex.printStackTrace();
        }
        catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }
}

Required  Jar Files:
  1.             commons-beanutils-1.8.2.jar
    2.      commons-collections-3.2.1.jar
    3.      commons-digester-2.1.jar
    4.      commons-logging-1.1.1.jar
    5.      groovy-all-1.7.5.jar
    6.      iText-2.1.7.jar
    7.      jasperreports-4.5.1.jar
    8.      jdt-compiler-3.1.1.jar
    9.      mysql-connector-java-5.1.6-bin.jar
    10.  poi-3.7-20101029.jar
    11.  rs2xml.jar






java email coding


import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.Multipart;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class sendmail 
{
  public static void main(String[] args)
{

String from="Sender Emailid here";      // Sender Emailid here Ex:mysample@gmail.com 
String to="Receiver Emailid here";                           //Receiver Emailid here 
String SMTP_AUTH_PWD="";                                  //Your Gmail Password
String fileAttachment="Specify File Path here";       //Specify File Path here  
String text="hi";                                                       //Text here
System.out.println(fileAttachment);
System.out.println(from);
System.out.println(to);
try
{
String subject="hi";                                         //Subject here
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.auth", "true");
String SMTP_HOST_NAME = "smtp.gmail.com";
int SMTP_HOST_PORT = 465;
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
//Session session = Session.getDefaultInstance(props, null);
Message message = new MimeMessage(mailSession);
message.setSubject("Testing SMTP-SSL");
message.setContent("", "text/plain");
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, from, SMTP_AUTH_PWD);
// Get a mail session
// Define a new mail message
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
// Create a message part to represent the body text
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(text);
//use a MimeMultipart as we need to handle the file attachments
Multipart multipart = new MimeMultipart();
//add the message body to the mime message
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source =new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler( new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
multipart.addBodyPart(messageBodyPart);
// add any file attachments to the message
// addAtachments(attachments, multipart);
// Put all message parts in the message
message.setContent(multipart);
// Send the message
transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
// Transport.send(message);

}
catch(Exception e1)
{
System.out.println(e1.getMessage());
}
}
}
Required Jar Files:
  1. activation.jar
  2. concurrent-1.3.2.jar
  3. log4j-1.2.15.jar
  4. mailapi-1.4.4.jar
  5. simple-java-mail-2.0.jar
  6. smtp-1.4.4.jar

 
java4practices © 2013 | Designed by Ganesh Rengarajan