Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, March 3, 2012

Hibernate Tutorial Net Beans7.1

This simple tutorials will illustrate how we can use Hibernate frame work to access a MySQL database in net beans 7.1. Here I'm not going to explain this with more details, because It is very difficult to add all the details. I assume that you have enough knowledge in relevant domains.For basics click here. Here you can find important pieces of codes.

Details about target MySQL database which I'm going to access


Database name : combo
Table name : items

1. Configuring hibernate

Here is the code

  
  com.mysql.jdbc.Driver
  jdbc:mysql://localhost:3306/combo
  root
  root123
  10
  org.hibernate.dialect.MySQLDialect
  thread
  org.hibernate.cache.NoCacheProvider
  true
  update
  
  


Highlighted lines(5&6) configure username property & password property. Replace them with yours.

2. POJO class.
package firsthibernetapp;

public class Item {
    private String icode;
    private String idiscription;
    private int iprice;
    private int iid;

    public void setIcode(String icode) {
        this.icode = icode;
    }

    public void setIdiscription(String idiscription) {
        this.idiscription = idiscription;
    }

    public void setIid(int iid) {
        this.iid = iid;
    }

    public void setIprice(int iprice) {
        this.iprice = iprice;
    }

    public String getIcode() {
        return icode;
    }

    public String getIdiscription() {
        return idiscription;
    }

    public int getIid() {
        return iid;
    }

    public int getIprice() {
        return iprice;
    }
}

3. Hibernate mapping file

  
  
  
  
  
  
  
  
  
  
  
  
  
  


<id
column="itemId" name="iid" type="int"> This line maps to the primary key of the database table.
If you are using  auto generate primary keys replace "assigned" in line 4 with "native".

4. Setting values
package firsthibernetapp;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class FirstHibernetApp {
   

    public static void main(String[] args) {
        Session session = null;
    try{
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    session =sessionFactory.openSession();
    session.beginTransaction();

    Item item = new Item();
    System.out.println("Inserting Record");
    item.setIid(23);
    item.setIcode("FX00011D");
    item.setIdiscription("Combo");
    item.setIprice(1500);
    session.save(item);
    System.out.println("Done");
    session.getTransaction().commit();
    }catch(Exception e){
    System.out.println(e.getMessage());
    }

  finally{
  session.flush();
  session.close();
   }
 }
}

If your database & everything fine you should be able to see a result like this
     . . . 

Run SELECT * FROM items; on items table to see updated table:





If you find this post helpful don't forget to leave a comment.



Thursday, February 23, 2012

Create java web service with client using Axis2 & Eclipse. (Part 1)

For updated tutorials visit my new blog.
 

Friday, January 6, 2012

JAVA first step
Downloading, Installing JAVA & Creating the development environment


Before start learning java you have to make the programming environment.
That means

1. Download & install JDK (Java Development Kit).
2. Creating the environment variables of the computer.
3. Download an IDE for java programming(Eclipse or NetBeans)

1. Downloading & Installing JDK.

I hope to write this tutorials using Java SE development Kit 7 on Windows environment.
Click here to download Java.
At the moment java 1.7 is the latest. Don't forget to choose your OS correctly in the downloading  page & if you are using 64bit Windows download JDK 64bit release. After downloading the file double click on the exe file & install it.


2. Creating the environment variables of the computer.

First click on start button, right click on Computer, choose Properties...


In properties window click on Advanced system settings


In System Properties window choose Advanced tab & click on Environment Variables


In the Environment Variables window under the user variables choose variable path & click on Edit button.....

In Edit User Variable window, after the value already in the value field, put semicolon & paste the path of the bin folder. Finally click OK.


You can find the bin folder in C:Program Files>Java>jdk1.7.0>bin


After updating the User Variables we have to update the System Variables. It is same as updating User Variables. If there is a system variable we have to edit it like previous.


Under the System Variables choose variable path & click on Edit button.....


In the opening window after the values already in the Variable value field place a semicolon & then paste the path of the bin folder.Finally click OK.



If there is no variable called "path" under System Variables we have to create one.Click on New... & create new system variable.The variable name should be "path" & the variable value should be path of the bin folder.

                              

After making changes apply all by clicking on Apply button in the System Properties window.

3. Downloading an IDE for java programming(Eclipse or NetBeans)

Using an IDE for developing java is easy & attractive way. So we can use Eclipse or NetBeans for that purpose since both of them are popular among programmers.
But I'm personally thinking Eclipse is an easy & most attractive IDE for developing Java programs for the beginners. It doesn't make you tired.

Click here to download Eclipse IDE.(Eclipse 3.7.1 Indigo is the latest).
Click here to download NetBeans IDE.(Choose package with all features).


In next lesson I hope to teach basics in Eclipse....
I hope to see your comments.......

Thank You All.

Thursday, January 5, 2012

Basics of Eclipse
How to create new java project  new package & new class in Eclipse ??

I hope to describe step by step developing new java project, package & new class in this tutorial. 

After downloading the Eclipse   SDK 3.7.1 .zip folder as described in my first tutorial first unzip it.
(Click here to read my first tutorial if you didn't.)
Then double click the eclipse.exe to open the IDE.

                                          


Then choose a work space. Workspace is a folder which stores our project files.

                    


After selecting a Workspace click OK.
In the opening Welcome window click on Workbench.

                           

Then select File > New > Java Project to create new java project.



In the Create a Java Project window specify a project name as you want.

               

No need to change other settings at the moment.
Then click finish.

Click New toolbar option. Then select Package because we need to store classes in a package.Remember that  there are also another alternatives to create new package.


Give appropriate package name.


Click Finish to create new package. A package is like a folder which contains classes of the project.

Finally we want to create a class. To do that again click on New toolbar option. Then select Class.


Specify class name in Create a new java class window. In my case I give a class name the class as HelloWorld. You can give any name as you want. First letter of a class must be capital.




No need to change other default settings. Finally click finish to finalize class creation.
Finally you can see the created class.



In my next tutorial I hope to develop this class to demonstrate basic concepts of  java.

Thank You !!!

Chathura's Blog