HelloWorld in android

1. double-click on eclipse shortcut

2. create new project; File > New > Project > Android 

3. select Android Project and click Next button to continue

4. Project Name field; HelloWorld

5. choose your Build Target (refer previous tutorial) and click Next button

6. define your Package Name; com.mohdfaizal.HelloWorld

7. HelloWorld project successfully created

8. expand /res/layout and double click on main.xml file

9. add textview and button

- original file -

- edited file -

10. save (ctrl + s) and run the application

11. android emulator will be displayed

12. unlock and double-click on HelloWorld launcher

13. finish. yeay!

setup android environments

1. click on this link: http://developer.android.com/sdk/index.html

2. download package: android-sdk_r17-windows.zip

3. extract the zip file

4. create a directory (e.g: Android 4.0)

5. expand the direcory and double click on SDK Manager

6. click on ‘Install 16 packages…’ button. choose Accept and proceed to install.

7. Android SDK Manager Log will be displayed while downloading the packages.

8. open eclipse and click on Help > Install New Software

9. type this link: https://dl-ssl.google.com/android/eclipse/

10. Developer Tools will be displayed, expand and check all. click Next to continue.

11. click Next button to proceed

12. choose I accept the terms of the license agreements

13. installing the related software

14. click Ok to proceed

15. after finished, restart your eclipse

 

16. choose Use existing SDK

17. click Finish to proceed

18. click on Window > AVD Manager

19. create New AVD

20. config your Launch Options

21. finish. yeay!

simple jasperreports tutorial

1. create a new Java Desktop Application project in your netbeans

File > New Project > Java > Java Desktop Application

2. click Next to proceed

3. define your project name and application class; my project name for this tutorial is TestingReport

4. define libraries such below:

5. create table ‘jasper_users’

CREATE TABLE `jasper_users` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `firstname` varchar(64) DEFAULT NULL,
 `lastname` varchar(64) DEFAULT NULL,
 `address` varchar(64) DEFAULT NULL,
 `email` varchar(64) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `jasper_users` (firstname, lastname, address, email)
VALUES ('John', 'Doe', 'Utah, USA', 'johndoe@email.com');

INSERT INTO `jasper_users` (firstname, lastname, address, email)
VALUES ('Mohd', 'Faizal', 'KL, Malaysia', 'mohdfaizal@email.com');

INSERT INTO `jasper_users` (firstname, lastname, address, email)
VALUES ('Onizuka', 'Sr.', 'Tokyo, Japan', 'onizuka@email.com');

6. Main.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mohdfaizal.jasperdemo;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author mohdfaizal
 */
public class Main extends JFrame implements ActionListener {

 private Logger logger = Logger.getLogger(Main.class);
 private JTextField txtFirstname = new JTextField(20);
 private JButton submitButton = new JButton("Submit");

public Main() {
 init();
 }

private void init() {
 Container conn = getContentPane();
 conn.setLayout(new FlowLayout());
 conn.add(new JLabel("Search by First Name: "));
 conn.add(txtFirstname);
 conn.add(submitButton);
 setLocationRelativeTo(null);
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 submitButton.addActionListener(this);
 pack();
 }

public void actionPerformed(ActionEvent e) {
 if (e.getSource().equals(submitButton)) {

Connection connection = null;
 JasperReport jasperReport = null;
 JasperPrint jasperPrint = null;

 try {
 // get search input
 String parameterName = txtFirstname.getText();
 String reportSource = "src/com/mohdfaizal/template/ReportTemplate.jrxml";
 // set parameters
 Map map = new HashMap();
 map.put("param", parameterName);
 // establish database connection
 Class.forName("com.mysql.jdbc.Driver");
 connection = DriverManager.getConnection("jdbc:mysql://localhost/tutorial", "root", "password");
 // compile report
 jasperReport = (JasperReport) JasperCompileManager.compileReport(reportSource);
 jasperPrint = JasperFillManager.fillReport(jasperReport, map, connection);
 //close connection
 connection.close();
 //view report to UI
 JasperViewer.viewReport(jasperPrint, false);

} catch (Exception ex) {
 // exception handling
 logger.error(ex.getMessage(), ex);
 System.err.println(ex.getMessage());
 }
 }
 }

public static void main(String[] args) {
 new Main().setVisible(true);
 }
}

7. ReportTemplate.jrxml


<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="ReportTemplate" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
 <property name="ireport.zoom" value="1.5"/>
 <property name="ireport.x" value="0"/>
 <property name="ireport.y" value="0"/>
 <parameter name="param" class="java.lang.String"/>
 <queryString>
 <![CDATA[
SELECT
 id,
 firstname,
 lastname,
 address,
 email
FROM
 jasper_users
WHERE
 firstname LIKE $P{param}]]>
 </queryString>
 <field name="id" class="java.lang.Integer"/>
 <field name="firstname" class="java.lang.String"/>
 <field name="lastname" class="java.lang.String"/>
 <field name="address" class="java.lang.String"/>
 <field name="email" class="java.lang.String"/>
 <background>
 <band splitType="Stretch"/>
 </background>
 <title>
 <band height="56" splitType="Stretch">
 <staticText>
 <reportElement x="0" y="0" width="555" height="46"/>
 <textElement textAlignment="Center" verticalAlignment="Middle">
 <font size="22" isBold="true"/>
 </textElement>
 <text><![CDATA[Jasper Reports Demo]]></text>
 </staticText>
 </band>
 </title>
 <pageHeader>
 <band height="13" splitType="Stretch"/>
 </pageHeader>
 <columnHeader>
 <band height="25" splitType="Stretch">
 <staticText>
 <reportElement x="50" y="0" width="100" height="20"/>
 <textElement>
 <font isBold="true"/>
 </textElement>
 <text><![CDATA[First Name]]></text>
 </staticText>
 <staticText>
 <reportElement x="161" y="0" width="100" height="20"/>
 <textElement>
 <font isBold="true"/>
 </textElement>
 <text><![CDATA[Last Name]]></text>
 </staticText>
 <staticText>
 <reportElement x="274" y="0" width="100" height="20"/>
 <textElement>
 <font isBold="true"/>
 </textElement>
 <text><![CDATA[Address]]></text>
 </staticText>
 <staticText>
 <reportElement x="395" y="0" width="149" height="20"/>
 <textElement>
 <font isBold="true"/>
 </textElement>
 <text><![CDATA[E-mail]]></text>
 </staticText>
 <staticText>
 <reportElement x="10" y="0" width="27" height="20"/>
 <textElement>
 <font isBold="true"/>
 </textElement>
 <text><![CDATA[ID]]></text>
 </staticText>
 </band>
 </columnHeader>
 <detail>
 <band height="25" splitType="Stretch">
 <textField>
 <reportElement x="50" y="0" width="100" height="20"/>
 <textElement/>
 <textFieldExpression class="java.lang.String"><![CDATA[$F{firstname}]]></textFieldExpression>
 </textField>
 <textField>
 <reportElement x="161" y="0" width="100" height="20"/>
 <textElement/>
 <textFieldExpression class="java.lang.String"><![CDATA[$F{lastname}]]></textFieldExpression>
 </textField>
 <textField>
 <reportElement x="274" y="0" width="100" height="20"/>
 <textElement/>
 <textFieldExpression class="java.lang.String"><![CDATA[$F{address}]]></textFieldExpression>
 </textField>
 <textField>
 <reportElement x="395" y="0" width="149" height="20"/>
 <textElement/>
 <textFieldExpression class="java.lang.String"><![CDATA[$F{email}]]></textFieldExpression>
 </textField>
 <textField>
 <reportElement x="10" y="0" width="27" height="20"/>
 <textElement/>
 <textFieldExpression class="java.lang.Integer"><![CDATA[$F{id}]]></textFieldExpression>
 </textField>
 </band>
 </detail>
 <columnFooter>
 <band splitType="Stretch"/>
 </columnFooter>
 <pageFooter>
 <band height="23" splitType="Stretch"/>
 </pageFooter>
 <summary>
 <band splitType="Stretch"/>
 </summary>
</jasperReport>


8. run the application



9. finish. yeay!