Sunday, September 15

Check prime number (Best Method)

boolean isPrime(int number) {
    //check if n is a multiple of 2
    if (number%2 == 0) return false;
    //if not, then just check the odds
    for(int i=3;i*i<=number;i+=2) {
        if(number%i == 0)
            return false;
    }
    return true;
}

Wednesday, April 17

Templating in jsf 2.0 simple example

This is the template code: template.xhtml and this file is located in WEB-INF/template/usertemplate

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>
<ui:insert name="pageTitle">Page Title</ui:insert>
</title>
<style type="text/css">
body {
 font-family: Verdana, Arial, Helvetica, sans-serif;
 font-size: 14px;
}
#header {
 font-family: Verdana, Arial, Helvetica, sans-serif;
 font-size: 18px;
}
#content{
width:80%;
margin:0 auto;
}
#footer {
 font-family: Verdana, Arial, Helvetica, sans-serif;
 font-size: 12px;
 text-align: center;
 color: #8E969D;
 border:1px solid black;
 width:80%;
 margin:0 auto;
 background-color: gray;
}
#abc{
background-color: black;
opacity:0.4;
filter:alpha(opacity=40)
}
</style>
</h:head>
<h:body>
<div id="header">
<ui:insert name="header">
<!--Header Content here-->
</ui:insert>
</div>
<div id="content">
<ui:insert name="content">
<!--Body Content here-->
</ui:insert>
</div>
<div id="footer">
<ui:insert name="footer">
<!--Footer Content here-->
</ui:insert>
</div>
</h:body>
</html>



this is one facelet code: 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"> 

<ui:composition template="/WEB-INF/templates/userTemplate/template.xhtml">
<ui:define name="content">
<h:form id="addBrandForm" style="border:2px solid #CCC; padding:3px;" onmouseover="style.border='2px solid #0276FD'" onmouseout="style.border='2px solid #CCC'">
<h:outputLabel value="Facelet" />
</h:form>
</ui:define>
</ui:composition>
</html>

Friday, March 1

Hibernate Example (Retrieve data from database)


package com.hibernate.example;




import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class TestEmployee {

public static void main(String[] args) {
AnnotationConfiguration config = new AnnotationConfiguration();
config.configure();
config.addAnnotatedClass(Employee.class);

new SchemaExport(config).create(true,true);

Employee emp1 = new Employee();
emp1.setEmpname("Umair");
Employee emp2 = new Employee();
emp2.setEmpname("Baig");


SessionFactory factory = config.buildSessionFactory();
Session session = factory.openSession();


session.beginTransaction();
session.save(emp1);
session.save(emp2);


session.getTransaction().commit();
Query query = session.createQuery("From Employee");
List emp = query.list();


for(int i=0;i<emp.size();i++){
Employee empUser = (Employee) emp.get(i);
System.out.println("Employee Name : "+empUser.getEmpname()+"\n");
}
session.close();
}
}

HIbernate Example (Java Application)


package com.hibernate.example;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;


@Entity
public class Employee {
private int empid;
private String empname;

@Id
@GeneratedValue
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}

}




package com.hibernate.example;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class TestEmployee {

public static void main(String[] args) {
AnnotationConfiguration config = new AnnotationConfiguration();
config.configure();
config.addAnnotatedClass(Employee.class);

new SchemaExport(config).create(true,true);

Employee emp1 = new Employee();
emp1.setEmpname("Umair");
Employee emp2 = new Employee();
emp2.setEmpname("Baig");


SessionFactory factory = config.buildSessionFactory();
Session session = factory.openSession();


session.beginTransaction();
session.save(emp1);
session.save(emp2);


session.getTransaction().commit();
session.close();


}

}


Monday, January 21

Print Document, Data Grid View in c# windows form application

Download source code: Download Here
C# Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;

namespace PrintDocumnentTutorial
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


            dataGridView1.Columns.Add("SrNO", "Sr No.");
            dataGridView1.Columns.Add("Empname", "Employee Name");



        }

        private void button2_Click(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.AllowUserToAddRows = false;



            dataGridView1.Rows.Clear();

            dataGridView1.Rows.Add();
            dataGridView1.Rows[0].Cells[0].Value = 1;
            dataGridView1.Rows[0].Cells[1].Value = "Umair";

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count == 0)
            {
                System.Windows.MessageBox.Show("Data is Empty");
            }
            else
            {
                printDocument1.Print();
            }
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Font fh = new System.Drawing.Font(new FontFamily("Arial"), 12);
            Bitmap dataGridViewImage = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
            dataGridView1.DrawToBitmap(dataGridViewImage, new Rectangle(0, 15, this.dataGridView1.Width, this.dataGridView1.Height));
            e.Graphics.DrawImage(dataGridViewImage, 0, 15);
            
            e.Graphics.DrawString("add1", fh, Brushes.Black, new System.Drawing.Point(0, (this.dataGridView1.Height + 200)));
            e.Graphics.DrawString("add2", fh, Brushes.Black, new System.Drawing.Point(0, (this.dataGridView1.Height + 215)));
            e.Graphics.DrawString("add3", fh, Brushes.Black, new System.Drawing.Point(0, (this.dataGridView1.Height + 230)));
            
        }

    }
}

Thursday, January 17

Filing in C# WPF Application

Download Source Code: FileCode
Xaml :

<Window x:Class="FileHandling.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="190,106,0,0" Name="filedatatxt" VerticalAlignment="Top" Width="120" />
        <Button Content="Write Text" Height="23" HorizontalAlignment="Left" Margin="235,135,0,0" Name="writeBtn" VerticalAlignment="Top" Width="75" Click="writeBtn_Click" />
    </Grid>
</Window>




C#:


using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FileHandling
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();




            // Reading from file which is located in debug folder
            // Begins
            string[] lines = File.ReadAllLines("Highscore.txt");
            foreach (string tempLines in lines) {
                MessageBox.Show(tempLines);
            }
            // Ends
        }

        private void writeBtn_Click(object sender, RoutedEventArgs e)
        {
            StreamWriter writeFile = new StreamWriter("Highscore.txt",true);
            writeFile.WriteLine(filedatatxt.Text);
            writeFile.Close();
        }
    }
}