Skip to main content

how to save data using hibernate

In this post i explain how to insert data into MySQL database using Hibernate with the help of NetBeans IDE.
First you need to create a database and a table in the MySQL database. While creating the table keep in mind that, the table must contain a primary key field. I use the following query for create the table.
 create table test_table (id int not null primary key auto_increment,name varchar(50), email varchar(50));  
Ads by Google


Now you need to follow the steps given bellow.

Step  1 :
             Open up  the NetBeans IDE.
Step  2 :
             Go to the services and expand the database.
example in hibernate
Step 3:
           Now right click MySQL server and choose connect. 
save data using hibernate in mysql
Step 4:
          Now you need to enter your MySQL username and password and choose the database to which you want to insert data. Right click the database and choose connect. 
mysql database with hibernate
Step 5:
          Create a new Web Application and choose the hibernate framework and choose the database connection.
hibernate data saving example
Step 6:
         Right click the project name  from the other option, choose Hibernate and from the hibernate folder choose the hibernate reverse engineering wizard. 
hibernate netbeans
Step 7:
Click next and choose the table / tables and click the add button and finally click finish. 
hibernate simple data saving example

Now you got your hibernate reverse engineering xml file. 

Step 8:
Right click the project, choose other and from hibernate folder select the "Hibernate mapping files and POJOs" from database.
data saving in java

You need to provide a package name and click finish. Now you got your hibernate mapping xml file and the POJO class file as shown bellow. 
what is hibernate

Step 9 :
Create a form in your index.jsp as shown bellow.
  <form action="TestServlet">  
  Name :<input type="text" name="name"/>  
  Email :<input type="text" name="email" />  
  <input type="submit" value="SAVE" />       
  </form>  

Step 10:
Create a new servlet and add the following code segment in processRequest method as shown bellow.
    String name,email;  
    name = request.getParameter("name");  
    email = request.getParameter("email");  
    SessionFactory sf = new Configuration().configure().buildSessionFactory();  
    Session s = sf.openSession();  
    Transaction tr = s.beginTransaction();  
    TestTable t = new TestTable(name, email);  
    s.save(t);  
    tr.commit();  
    s.close();  

Now run the index.jsp file and check data in your database table.

Watch Video Tutorial of this Topic

Comments

Popular posts from this blog

Android Swipe Views with Tabs

In this post we are going to learn about how to integrate the android tab view with the fragments using ViewPager and ActionBar class. For displaying the tabs on the top of the screen you need to interact with the android action bar, this is because the tab views is connected with the action bar. Ads by Google In this example application we make three tabs called "java", "php" and ".Net" and there are three seperate fragement view for each of these tabs. First you need to add the ViewPager into the activity_main.xml file. <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" > </android.support.v4.view.ViewPager> Now create three layout files for the fragments. 1. java_layout.xml <?xml version="1.0" encod

Android MySQL Database Operations

In this post we are going to learn about how to connect to MySQL Database from your Android Application and perform database operations. Here we create an android app that contain some login Activity through which the user can retrieve information from database and a registration Activity through which the user can add information into the database.  First you need to have the following components installed in your development machine.  1. Database : Here we use the MySQL database. 2. Web Server : Here we use the Apache Web Server. 3. Server side Scripting Language :   Here we use PHP for server side scripting. 4. Android Development environment : You must install android sdk and android studio.   I recommend you to download and install WAMPSERVER. The wamp server installer contains the following components. Apache Server Application MySQL Database PHP/phpMyAdmin First we have to create the database and table in MySQL. You can use the phpMyAdmin for mange yo

"please correct the errors on this form" adsense error simple solution

Many of the bloggers now facing the problem with their adsense widgets. Whenever try to add the new  adsense link unit using the blogger widgets, it shows the error " please correct the errors on this form " as shown bellow. In blogger you can add the adsense units in two ways. First one is adding the adsense using the blogger widgets(Most of the beginners doing this) and the second method is to obtain the adsense code from the adsense login page and place it into the targeted position on the blog. To improve the revenue from adsense you have to place the appropriate ad unit into the right position. The adsense link units are very important for increasing the adsense revenue. So due to this error many of the bloggers are unable to place the link units.  Here is the solution for the problem. 1. Login into your adsense and blogger account. 2. Now make sure that only two adsense widgets (units) are present on your blog, this is because Google allows onl