I had found a simple and created an util class that can help in easy way to handle a lot of validations in JText files.
Friday, 26 July 2013
how to load Image with relative path and re-size it #java
I had faced it while i was trying to add some image to desktop Applications.
and found many ways by buffered image and loading images itself , here is an easy-way to handle it
Wednesday, 24 July 2013
How To insert and Retrieve image from DB #jdbc #java #MySQL
here is a few steps to insert and retrieve image files from DB.
1. Creating the Image column:
how to change the coffee cup in #Java Frames #GUI
it's weird when you install your program to customer and found that cup in your frames,
While handling this issue is very easy.
you just want to change the icon Image of your fame.
While handling this issue is very easy.
you just want to change the icon Image of your fame.
Sunday, 21 July 2013
Two ways to validate email address #regular expression #java
validation on the email address will be handled using two ways below :
- Regular expression
- apache-commons Email validator.( you need to download the Jar.)
Thursday, 18 July 2013
How to Make All .Jar and lip files in one JAR file . #net-Beans. #java
How to make my project in only one jar file using net-beans?
In order to make the created JAR include the lips jar.
All what you need is to add below data in Build.xml of your project :
How To convert .jar to .exe #java
How to change jar file and make it executable ".exe" file ?
that's a serious question and there are a lot of programs do it.
but I prefer that one and will make it ease to use it in few steps
but I prefer that one and will make it ease to use it in few steps
How to make a jar file ? #java #net-beans #eclipse #batch file #Windows
In this post, you will know how to create a jar file using net-beans , Eclipse and using ant scripts in easy and straightforward way.
Net-beans.
Step 1: you had to right click on the project
Step 2: Click Clean and Build.
Step 3: check out the Jar that already created under the project folder (./dist)
PS. to change the entry man class. choose it and edit in MANIFEST.MF
Main-Class: package.MainClass
Net-beans.
Step 1: you had to right click on the project
Step 2: Click Clean and Build.
Step 3: check out the Jar that already created under the project folder (./dist)
PS. to change the entry man class. choose it and edit in MANIFEST.MF
Main-Class: package.MainClass
OR
change the Build.xml and add below tag.
<manifest file="MANIFEST.MF">
<attribute name="Main-Class" value="package.MainClass" />
</manifest>
How to run .jar file by #double click #batch file #java
Well. I had faced this issue a lot while I was installing the product to my clients.
and I found just a simple way to handle it easy :)
Double click into the jar file:
Step 1: Start Control panel
Step 2: Click Default Programs
Step 3: Click Associate a file type or protocol with a specific program
Step 4: Double click .jar
Step 5: Browse C:\Program Files\Java\jre7\bin\javaw.exe
Step 6: Click the button Open
Step 7: Click the button OK
OR
Step 1: right-click on the jar file
Step 2: Open With a Java Runtime listed ( C:\Program Files\Java\jre7\bin\javaw.exe)
Step 3: make it the default program to run with.
and I found just a simple way to handle it easy :)
Double click into the jar file:
Step 1: Start Control panel
Step 2: Click Default Programs
Step 3: Click Associate a file type or protocol with a specific program
Step 4: Double click .jar
Step 5: Browse C:\Program Files\Java\jre7\bin\javaw.exe
Step 6: Click the button Open
Step 7: Click the button OK
OR
Step 1: right-click on the jar file
Step 2: Open With a Java Runtime listed ( C:\Program Files\Java\jre7\bin\javaw.exe)
Step 3: make it the default program to run with.
Wednesday, 17 July 2013
Observer design Pattern with Examples .#Java
Observer design pattern is a fundamental core pattern where Observe watch for any change in state or property of Subject.
For Example Company updates all its shareHolder for any decision .
they make here Company is Subject and Shareholders are Observers,
any change in policy of company , the company will notifies all its Shareholders (observers).
For Example Company updates all its shareHolder for any decision .
they make here Company is Subject and Shareholders are Observers,
any change in policy of company , the company will notifies all its Shareholders (observers).
What is Observer design Pattern?
Observer design pattern is very important pattern and as name suggest it’s used to observe things. Suppose you want to notify for change in a particular object than you observer that object and changes are notified to you. Object which is being observed is refereed as Subject and classes which observe subject are called Observer.
This pattern is used heavily along with Model View Controller Design pattern where change in model is propagated to view so that it can render it with modified information.
Saturday, 13 July 2013
Difference between Enumeration and Iterator ? #java #interview
both Iterator and Enumeration provides way to traverse or navigate through entire collection in java
Enumeration is older and its there from JDK1.0 while iterator was introduced later.
Iterator can be used with Java arraylist, java hashmap keyset and with any other collection classes.
As far Enumeration is older than Iterator so that functionality of Enumeration interface is duplicated by the Iterator interface.
Only major difference is Iterator has a remove() method while Enumeration doesn't. Enumeration acts as Read-only interface, because it has the methods only to traverse and fetch the objects, where as by using Iterator we can manipulate the objects like adding and removing the objects from collection e.g. Arraylist.
Friday, 12 July 2013
Difference between Set, List and Map in #Java #interview
Set (Interface)
- Set is an un-ordered collection which doesn’t
allows duplicate (no-duplicate) elements
- We can iterate the values by calling iterator() method
Set s = new HashSet();
Iterator iter = s.iterator();
List (Interface)
- List is an ordered collection which allows duplicate elements
- We can iterate the values by calling iterator()
method
List li = new
ArrayList();
Iterator iter = li.iterator();
Map (Interface)
- In Map we used to store the data in key and value pairs, we
may have duplicate values but no duplicate keys
- In Map we don’t have iterator() method, but
we can get the keys by calling the method keySet()
List li = new
ArrayList();
Iterator iter = li.iterator();
Iterator iter = li.iterator();
Map (Interface)
- In Map we used to store the data in key and value pairs, we may have duplicate values but no duplicate keys
- In Map we don’t have iterator() method, but we can get the keys by calling the method keySet()
Map m; // insert values
Set s = m.keySet();
// Get Map keys into the Set and then
iterate this Set object normally
// m.keySet() returns Set object with Map
keys
Iterator iter = s.iterator();
When to use List, Set and Map in Java
- If you need to access elements
frequently by using index, than List is a way to go. Its implementation
e.g. ArrayList provides faster access if you know index.
- If you want to store elements and want
them to maintain an order on which they are inserted into collection
then go for List again, as List is an ordered collection and maintain
insertion order.
- If you want to create collection of
unique elements and don't want any duplicate than choose any Set
implementation e.g.HashSet, LinkedHashSet or TreeSet. All Set
implementation follow there general contract e.g. uniqueness but also add
addition feature e.g. TreeSet is a SortedSet and elements stored on TreeSet can
be sorted by using Comparator or Comparable in Java. LinkedHashSet also
maintains insertion order.
- If you store data in form of key and
value than Map is the way to go. You can choose from Hashtable, HashMap, TreeMap based
upon your subsequent need. In order to choose between first two
see difference between HashSet and HashMap in Java.
References
- http://java67.blogspot.com/2013/01/difference-between-set-list-and-map-in-java.html
- http://www.java4s.com/core-java/difference-between-java-set-list-and-map-collections/
- If you need to access elements frequently by using index, than List is a way to go. Its implementation e.g. ArrayList provides faster access if you know index.
- If you want to store elements and want them to maintain an order on which they are inserted into collection then go for List again, as List is an ordered collection and maintain insertion order.
- If you want to create collection of unique elements and don't want any duplicate than choose any Set implementation e.g.HashSet, LinkedHashSet or TreeSet. All Set implementation follow there general contract e.g. uniqueness but also add addition feature e.g. TreeSet is a SortedSet and elements stored on TreeSet can be sorted by using Comparator or Comparable in Java. LinkedHashSet also maintains insertion order.
- If you store data in form of key and value than Map is the way to go. You can choose from Hashtable, HashMap, TreeMap based upon your subsequent need. In order to choose between first two see difference between HashSet and HashMap in Java.
- http://java67.blogspot.com/2013/01/difference-between-set-list-and-map-in-java.html
- http://www.java4s.com/core-java/difference-between-java-set-list-and-map-collections/
Thursday, 11 July 2013
Principles of Object-Oriented Programming #JAVA EXAPLES
it's briefly explain the 4 major principles that make a language object-oriented:
Encapsulation, Data Abstraction, Polymorphism and Inheritence.
Encapsulation
Encapsulation hides the details of that implementation. An accessor is a method that is used to ask an object about itself. In OOP, these are usually in the form of properties, which have, under normal conditions, a get method, which is an accessor method. However, accessor methods are not restricted to properties and can be
any public method that gives information about the state of the object.
while hiding the implementation of exactly how the data gets modified. Mutilators are commonly another portion of the property discussed above, except this time its the set method that lets the caller modify the member data behind the scenes.
class Loan{
private int duration; //private variables examples of encapsulation
private String loan;
private String borrower;
private String salary;
private int duration; //private variables examples of encapsulation
private String loan;
private String borrower;
private String salary;
//public constructor can break encapsulation instead use factory method
private Loan(int duration, String loan, String borrower, String salary){
this.duration = duration;
this.loan = loan;
this.borrower = borrower;
this.salary = salary;
}
private Loan(int duration, String loan, String borrower, String salary){
this.duration = duration;
this.loan = loan;
this.borrower = borrower;
this.salary = salary;
}
//no argument consustructor omitted here
// create loan can encapsulate loan creation logic
public Loan createLoan(String loanType){
//processing based on loan type and than returning loan object
//processing based on loan type and than returning loan object
return loan;
}
}
Why Encapsulation:
1. Encapsulated Code is more flexible and easy to change with new requirements.
2. Encapsulation in Java makes unit testing easy.
3. Encapsulation in Java allows you to control who can access what.
4. Encapsulation also helps to write immutable class in Java which are a good choice in multi-threading environment.
5. Encapsulation reduce coupling of modules and increase cohesion inside a module because all piece of one thing are encapsulated in one place.
6. Encapsulation allows you to change one part of code without affecting other part of code.
Abstraction
Data abstraction and encapsulation are closely tied together, because a simple definition of data abstraction is the development of classes, objects, types in terms of their interfaces and functionality, instead of their implementation details. Abstraction denotes a model, a view, or some other focused representation for an actual item.
Its the development of a software object to represent an object we can find in the real world. Encapsulation hides the details of that implementation.
Why Abstraction:
1) Use abstraction if you know something needs to be in class but implementation of that varies.
2) In Java you cannot create instance of abstract class , its compiler error.
3) Abstract is a keyword in java.
4) a class automatically becomes abstract class when any of its method declared as abstract.
5) Abstract method doesn't have method body.
6) Variable cannot be made abstract, its only behavior or methods which would be abstract.
7) If a class extends an abstract class or interface it has to provide implementation to all its abstract method to be a concrete class. Alternatively this class can also be abstract.
7) If a class extends an abstract class or interface it has to provide implementation to all its abstract method to be a concrete class. Alternatively this class can also be abstract.
Inheritance
Objects can relate to each other with either a “has a”, “uses a” or an “is a” relationship. “Is a” is the inheritance way of object relationship.
So, take a library, for example. A library lends more than just books, it also lends magazines, audio-cassettes and microfilm. On some level, all of these items can be treated the same: All four types represent assets of the library that can be loaned out to people.
However, even though the 4 types can be viewed as the same, they are not identical. A book has an ISBN and a magazine does not. And audio-cassette has a play length and microfilm cannot be checked out overnight.Each of these library’s assets should be represented by its own class definition.
Without inheritance though, each class must independently implement the characteristics that are common to all loanable assets.
Example will mentioned with Polymorphism Example.
Polymorphism
Polymorphism means one name, many forms.
also is the ability to create a variable, a function, or an object that has more than one form. In principle, polymorphism can arise in other computing contexts and shares important similarities with the concept of degeneracy in biology.
There are 2 basic types of polymorphism.
- Overridding, also called run-time polymorphism,
- Overloading, which is referred to as compile-time polymorphism.
Method overloading means writing two are or more methods in the same class by using same method name, but passing the parameters is different.
Method overriding means we use the method names in the different classes,that means parent class method is used in the child class.
Example:
public abstract class Human
{
public abstract void printGender();
}
public class Male extends Human
{
@Override
public void printGender()
{
{
System.out.println("man");
}
}
public class Female extends Human
{
@Override
public void printGender()
{
{
System.out.println("women");
}
}
public class boy extends male
{
@Override
public void printGender()
{
{
System.out.println("teanager");
}
public void printGender(String name)
{
System.out.println(name +"teenager");
}
}
- Male is Inherit Human.
- Boy .printGender() method is override the method printGender in Man Class.
- printGender(name)/printGender() is overloaded.
References:
- http://stackoverflow.com/questions/154577/polymorphism-vs-overriding-vs-overloading
- http://codebetter.com/raymondlewallen/2005/07/19/4-major-principles-of-object-oriented-programming/
- http://javarevisited.blogspot.com/2012/03/what-is-encapsulation-in-java-and-oops.html
- http://javarevisited.blogspot.com/2010/10/abstraction-in-java.html
Tuesday, 9 July 2013
How to Create And start connection using #Hibernate #java
• Steps to create your first application using Hibernate:
Execute the create statement to create the table – Execute the create statement to create the table- Create your java project
- Add the required jars to your project Add the required jars to your project
- Create a java bean that’ll represent the table
- Create the XML mapping file which should be saved as className.hbm.xml and located near the class
- Create hibernate.cfg.xml to configure the Hibernate
- Create a test class to start inserting objects in the DB.
Create a java bean that’ll represent the table (POJO)
import import java.util.Date java.util.Date;;
public class Employee {
private int id;
private String name;
private Long phone;
private String address;
private Date birthdate ;
this.name = name; }
public Long getPhone(){
return phone;
}
public void setPhone(Long phone) {
this.phone = phone;
}
public Employee() {
}
public String getAddress() {
return address;
}
public public int getId() {
return id;
}
private void setId (int id) {
this.id = id;
}
public void setAddress (String address) {
this.address == address;
}
public Date getBirthdate() {
return birthdate ;
}
public String getName() {
return name;
}
public void setBirthdate (Date birthdate) {
this.birthdate =birthdate;
}
}
and Hibernate.cfg.xml will be like
• Create hibernate.cfg.xml
this file is the main configuration file that contain- Driver name
- Data base path
- DB User Name and password
- mapped Tables.
Example :
<hibernate--configuration>
<session--factory name="sessionfactory">
<!-- driver name-->
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- db path-->
<property name ="hibernate.connection.url">
jdbc:mysql://localhost:3306/Hibernate
</property>
<!--DB username / password-->
<property name ="hibernate.connection.username ">root</property>
<property name ="hibernate.connection.password"></property>
<property name ="hibernate.dialect>
org.hibernate.dialect.MySQLDialect
</property>
<!--table mapping-->
<mapping resource="Employee.hbm.xml"/>
</session factory>
</hibernate--configuration>
How to Start the connection ?
- Create Session Factory instance
- get session if exist or open new session;
- begin transaction with the session .
- do you change (persist / update the object
- commit the change
Create a test class
import java.util.Date ;import org.hibernate.Session;
import org.hibernate.SessionFactory ;
import org.hibernate.cfg.Configuration;
public class Test {
public static void main(String[] args)
{
SessionFactory sessionFactory= new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Employee emp = new Employee();
emp setName("name");
emp.setPhone (new Long(1234567);
emp.setBirthdate(new Date());
emp.setAddress("address");
session.beginTransaction();
session.persist(emp);
session.getTransaction().commit();
System. out.println ("Insertion Done");
}
}
Good luck
Subscribe to:
Posts (Atom)