Hibernate - Hello, World! using Annotation Mapping

This tutorial explains you, how to develop a simple database application using Hibernate Object Relational Mapping (ORM) framework and MySQL database. This article uses Annotation for mapping; the XML mapping is explained in: Hibernate - Hello, World! using XML Mapping

You need Eclipse for Java EE developers and MySQL server in your system in-order to try this tutorial.
If you prefer to import the necessary libraries manually as shown in "Step 3 Without Maven", Eclipse for Java developers is enough. Those who prefer Maven projects, need to have M2Eclipse plugin in their Eclipse. This tutorial recommends Eclipse for Java EE, since it has M2Eclipse plugin by default. Also please be informed that the project attached at the end of this tutorial is created using Maven.
Step 1:
Create a database “javahelps” and a table “student” in MySQL.
CREATE DATABASE IF NOT EXISTS javahelps;

CREATE  TABLE javahelps.student ( 
student_id INT NOT NULL ,
student_name VARCHAR(45) NOT NULL ,
student_age INT NOT NULL ,
PRIMARY KEY (student_id) );

Read More

Hibernate - Hello, World! using XML Mapping

This tutorial explains you, how to develop a simple database application using Hibernate Object Relational Mapping (ORM) framework and MySQL database. This article uses XML mappings; the annotation mapping is explained in: Hibernate - Hello, World! using Annotation Mapping

You need Eclipse for Java EE developers and MySQL server in your system in-order to try this tutorial.
If you prefer to import the necessary libraries manually as shown in "Step 3 Without Maven", Eclipse for Java developers is enough. Those who prefer Maven projects, need to have M2Eclipse plugin in their Eclipse. This tutorial recommends Eclipse for Java EE, since it has M2Eclipse plugin by default. Also please be informed that the project attached at the end of this tutorial is created using Maven.
Step 1:
Create a database “javahelps” and a table “student” in MySQL.
CREATE DATABASE IF NOT EXISTS javahelps;

CREATE  TABLE javahelps.student ( 
student_id INT NOT NULL ,
student_name VARCHAR(45) NOT NULL ,
student_age INT NOT NULL ,
PRIMARY KEY (student_id) );


Step 2:
Create a new Java project “HibernateXML” in Eclipse.

Read More

Jersey 1.x - Hello, world!

Developing RESTful Web services that seamlessly support exposing your data in a variety of representation media types and abstract away the low-level details of the client-server communication is not an easy task without a good toolkit. In order to simplify development of RESTful Web services and their clients in Java, a standard and portable JAX-RS API has been designed. Jersey RESTful Web Services framework is open source, production quality, framework for developing RESTful Web Services in Java that provides support for JAX-RS APIs and serves as a JAX-RS (JSR 311 & JSR 339) Reference Implementation.

This tutorial shows how to develop a simple hello world RESTful application using Jersey 1.19. This tutorial uses Eclipse for Java EE developers and Apache Tomcat as the development tools. If you do not have them, please setup them before continuing this tutorial.

For Ubuntu users:
For any users:
Integrate Tomcat with Eclipse

Step 1:
Create a new “Dynamic Web Project” in Eclipse, by NewProjectDynamic Web Project.


Read More
Serialization in Java

Serialization in Java

Serialization is the process of converting Java objects into a stream of bytes. The stream of bytes can be transmitted through a network connection, stored in a database as a BLOB object or saved as a binary file. The stored or transmitted stream of bytes can be reconstructed to Java object later. This article explains Serialization in Java, using a simple step by step example.
Note: The sample codes provided in this tutorial, use try-with-resources blocks to auto close the input and output streams. You need Java 7 or latest version to compile and run these codes. For more information about try-with-resources, visit to this tutorial: Try With Resources
Create a Student class with the attributes name, and age.
public class Student {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }
}
Create a Main class as shown below, to create an object of Student.
public class Main {

    public static void main(String[] args) {
        Student stu = createStudent();
        System.out.println("Name: " + stu.getName());
        System.out.println("Age: " + stu.getAge());
    }

    /**
     * Create a sample Student object.
     *
     * @return a Student object.
     */
    public static Student createStudent() {
        // Create a Student object
        Student stu = new Student();
        stu.setName("Alice");
        stu.setAge(24);

        return stu;
    }
}

Read More

Android: Memo Application

This article provides a step by step guidance to develop a simple Memo application. You need to have basic knowledge on Android database and ListView to understand and complete this project. If you are not familiar with them, please visit to the following tutorials:

Step 1:
Create a new Android project “Memo”  with a package name “com.javahelps.memo

Step 2:
Right click on the Java package and create a new class 'Memo'. This class will be used as the model to represent the memos later in this project.
package com.javahelps.memo;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Memo implements Serializable {
    private Date date;
    private String text;
    private boolean fullDisplayed;
    private static DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyy 'at' hh:mm aaa");

    public Memo() {
        this.date = new Date();
    }

    public Memo(long time, String text) {
        this.date = new Date(time);
        this.text = text;
    }

    public String getDate() {
        return dateFormat.format(date);
    }

    public long getTime() {
        return date.getTime();
    }

    public void setTime(long time) {
        this.date = new Date(time);
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getText() {
        return this.text;
    }

    public String getShortText() {
        String temp = text.replaceAll("\n", " ");
        if (temp.length() > 25) {
            return temp.substring(0, 25) + "...";
        } else {
            return temp;
        }
    }

    public void setFullDisplayed(boolean fullDisplayed) {
        this.fullDisplayed = fullDisplayed;
    }

    public boolean isFullDisplayed() {
        return this.fullDisplayed;
    }
    @Override
    public String toString() {
        return this.text;
    }
}
The DateFormat is used to format the date when displaying the time of creation of the memo in the application. The fullDisplayed flag is used to define whether the complete memo is displayed to the user or part of the memo only displayed to the user. This property will be used to shrink and expand the memo in the application.

Read More

Contact Form

Name

Email *

Message *