Database connectivity Hibernate mysql connection.

Database connectivity Hibernate mysql connection.

How to do database connectivity in Hibernate using mysql?

View Answers

May 26, 2012 at 7:27 PM

A. Hibernate: -Hibernate is an Open Source persistence technology. It provides Object/Relational mapping library. It solves object-relational impedance mismatch problems. Here is a simple example of mysql database connectivity using hibernate. 1. Configuring Hibernate- hibernate-cfg.xml is configuration file saved in the same folder where the source code of class file is saved. It creates the connection pool and set-up required environment.

  1. In you configure your database connectivity, set sql dialect ,create database schema etc. Here hibernate is your database name which contain tables. ?net.roseindia.table.Employee" is your class in which you are mapping your table.

Now create Persistent class ?Hibernate uses the Plain Old Java Object (POJO) classes to map to the databse table. Here is the code of Employee.java-

package net.roseindia.table;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@javax.persistence.Entity
@Table(name = "employee") //database table name employee
public class Employee implements Serializable {

    @Id
    @GeneratedValue
    private int empId;

    @Column(name = "emp_name", nullable = false)
    private String empName;

    @Column(name = "emp_salary", nullable = false)
    private int salary;

    @Column(name = "designation", nullable = false)
    private String designation;

    @Column(name = "address", nullable = false)
    private String address;

    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;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public String getDesignation() {
        return designation;
    }

    public void setDesignation(String designation) {
        this.designation = designation;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

}

Here we are using annotation for mapping our table employee to our class Employee.java. 3. Now create an util class as HibernateUtil.java

package net.roseindia.util;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {
    private static SessionFactory sessionFactory;

    static {
        try {
            sessionFactory = new AnnotationConfiguration().configure()
                    .buildSessionFactory();

        } catch (HibernateException exception) {
            exception.printStackTrace();
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

4.Now we are going to create our main class.Here we are inserting data in to table.

package net.roseindia.application;

import net.roseindia.table.Employee;
import net.roseindia.util.HibernateUtil;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class MainClaz {
    public static void main(String[] args) {

        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        Employee employee = new Employee();
        employee.setEmpName("Rose");
        employee.setAddress("Patna");
        employee.setSalary(18000);
        employee.setDesignation("Manager");
        session.save(employee);
        transaction.commit();
        session.clear();
        session.close();
        sessionFactory.close();

    }
}

May 28, 2012 at 6:09 PM

hibernate-cfg.xml is configuration file saved in the same folder where the source code of class file is saved. It creates the connection pool and set-up required environment.

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>

        <!-- Database connection settings -->

        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->

        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache -->

        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <!-- Echo all executed SQL to stdout -->

        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">none</property>
        <mapping class="net.roseindia.table.Employee" />
    </session-factory>

</hibernate-configuration>









Related Tutorials/Questions & Answers:
Database connectivity Hibernate mysql connection.
Database connectivity Hibernate mysql connection.  How to do database connectivity in Hibernate using mysql
database connectivity using mysql
database connectivity using mysql  java file: eg1.java package eg...[]) throws SQLException { try { String connectionURL = "jdbc:mysql://loaclhost:1522/mydb"; Connection connection = null; Statement statement = null
Advertisements
DriverClass hibernate mysql connection.
DriverClass hibernate mysql connection.  What is DriverClass in Hibernate using mysql connection?   DriverClass implements java.sql.Driver....-factory> <!-- Database connection settings --> <
PHP Mysql Database Connection
PHP Mysql Database Connection       PHP Mysql Database Connection is used to build a connection... not connect: ".mysql_error()); mysql_select_db($database,$connection
Establish a Connection with MySQL Database
Establish a Connection with MySQL Database   ... coding methods of establishing the connection between MySQL database and quartz... the job class that establishes the connection with the MySQL database by using
database connectivity
database connectivity  describe java program steps in order to get connectivity to database along with example
database connectivity
database connectivity  how to create database connectivity between HTML and sql server2005
connectivity with mysql
connectivity with mysql  if this code of roseindia doesnt work.../phpdatabase/Check-PHP-MySQL-Connectivity.html   Please visit the following.../tutorial/php/phpdatabase/ http://www.roseindia.net/sql/mysql-example/php-mysql
Java connectivity with MySQL
Java connectivity with MySQL  Java connectivity with MySQL
database connectivity
database connectivity  i have written class.forName and getconnection in one method which returns connection string object under particular class and in someother class i want to call that for statement object
Database Connectivity
Database Connectivity  I tried to establish database connection with sqlserver2008 through GlassFish server. When I set the path of sqljdbc.jar(E:\Glass Fish\glassfish-v2ur1\javadb\lib\sqljdbc.jar) it shows me error
database connectivity
("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql...database connectivity  i m trying to connect this servlet with database but i m not able to Code is ot giving any error and there is no update
how to auto generate number in jsp with the database connection mysql for employee number?
how to auto generate number in jsp with the database connection mysql for employee number?  how to auto generate number in jsp with the database connection mysql for employee number?   <%@page import="java.sql.
how to auto generate number in jsp with the database connection mysql for employee number?
how to auto generate number in jsp with the database connection mysql for employee number?  how to auto generate number in jsp with the database connection mysql for employee number?   <%@page import="java.sql.
j2me mysql connectivity
j2me mysql connectivity  I do a project on reservation using j2me. I need a connectivity to a MYSQL database. How do I do this Thanks and regards Karthiga
form text box connection with mysql database feild - JDBC
form text box connection with mysql database feild  Respected Sir, What is the coding to connect a form text box field with mysql database table field will you explain me with simple example.. thanking you.. 
Help me on database connectivity in J2ME - Java Beginners
Help me on database connectivity in J2ME  i want help in J2ME. i want code for database connection with MySQL. spcl to fecth and insert data from databse. please help me
in connectivity - Hibernate
insertted  Hi friend, This is connectivity and hibernate configuration code com.mysql.jdbc.Driver jdbc:mysql://localhost... the hibernate and postgresql that progrram is running while showing no error
connection with MySQL to java.
connection with MySQL to java.   how to connect MySQL database with jsp
Connection to Database
I manually make a connection to MySQL database in my web pages? How joomla is connection to MySQL without getting any connection error? For reference, My... is the code how I am connecting to MySQL: I am connection to MySQL database in 2
DataBase Connectivity with MySql in Visual Web JSF Application Using Net Beans IDE
DataBase Connectivity with MySql in Visual Web JSF Application Using Net Beans... connectivity with MySQL in visual web jsf application using java persistence api. In this application, we are going to create a database connection with mysql step
database connectivity - JDBC
database connectivity  example java code for connecting Mysql database using java  Hi friend, Code for connecting Mysql database using...."); Connection conn = null; String url = "jdbc:mysql://localhost:3306
DataBase Connection
DataBase Connection  How to connect java and Oracle 10g? Tell me Jdbc connection
database connection
database connection  hi all ready created database table using mysql database,employee payroll,attendance,employee details used database i need how to import and export excel file into database using jsp? pls help me any one
jdbc connectivity to mysql tutorial
jdbc connectivity to mysql tutorial  here is complete jdbc connectivity to mysql tutorial for newbies. Person who don't even know how to install mysql and don't know about which driver to use can do with ease
JDBC, Java Database Connectivity
tutorials. Java Database Connectivity or JDBC for short is Java bases API... to database and then get the database information. After making a connection...: In the second step we have to make connection to the database server. Step 3
database connectivity problem
database connectivity problem  what if client dont have database with them then what is other way to run successfully our programm   Please visit the following link: http://www.roseindia.net/jdbc
Oracle Database connectivity probem
Oracle Database connectivity probem  hi Below is the code of oracle database connectivity, when i compile it, it will show the error... ("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection("jdbc:oracle:thin
C Program with Database Connectivity
C Program with Database Connectivity  How To Connect MySQL Database in C Program with Graphics
Database Connectivity Withe Oracle
Database Connectivity Withe Oracle  Develop a program to perform the database driven operation like insert, Delete, Update and select. To perform the above operations create one table named Employee. Field Name Field Type EmpId
connection to mysql in jsp
connection to mysql in jsp  please i got this error message when trying to connect to mysql database in jsp. Unknown character set: 'utf8mb4
Error in connecting to the mySQL database in TOMCAT using more than one PC (database connection pooling)
Error in connecting to the mySQL database in TOMCAT using more than one PC (database connection pooling)  how do i implement connection pooling...="com.mysql.jdbc.Driver" url="jdbc:mysql://10.9.58.8:3306/alcs_htht"/> </Context>
Access 2007 database connectivity
Access 2007 database connectivity  i design an application form... source and destination. pls tell me the code of connectivity with access 2007 database using JComboBox.thanks
Java database connectivity
Java database connectivity  Hi sir I need a code to create an application where user enter name in text box and that should be stored in database...("com.mysql.jdbc.Driver").newInstance(); Connection conn
database connection
database connection  how to connect the jsp page with database
database connectivity - JDBC
database connectivity  can u ppl pls provide me with database connectivity programs with MSacces   Hi Friend, If you havn't create your... a user DSN 4. Select Microsoft Access Driver(*.mdb) 5. Select database name
database connection
database connection  i wanted to no how to connect sqlite database through netbeans? is it posible to connect it to a database that is on a remote pc? thank you
Check PHP MySQL Connectivity
Check PHP MySQL Connectivity: The first step of any kind of connectivity... a message and terminates the program. mysql_connect: Open a connection to a MySQL..._db: Select a MySQL database mysql_query: It is a function which is used
database connectivity in java - JDBC
database connectivity in java  import java.io.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.sql.*; class...=temp4; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection
Spring MVC and Hibernate with DWR 3 -- Configuration Using MySQL Database
Spring MVC and Hibernate with DWR 3 -- Configuration Using MySQL Database  How to configure Spring MVC and Hibernate with DWR 3, Simple example -- reading a table, editing-deleting and adding new rows
database connectivity using jsp code
database connectivity using jsp code  i have two tables employee... key)this is employee table schema in mysql adduser->(Emp_ID varchar(50),Ename...(10)) this is adduser table schema in mysql
secure php mysql connection
secure php mysql connection  secure php mysql connection script
Connecting to MYSQL Database in Java
Connecting to MYSQL Database in Java  I've tried executing the code...("MySQL Connect Example."); Connection conn = null; String url... the following link: JDBC MySQl Connectivity   I have mysql-connector
mysql jdbc connectivity
mysql jdbc connectivity  i want to connect retrieve data from mysql using jdbc
Servlets mysql connectivity
Servlets mysql connectivity  please tell me the steps to connect mysql with java servlets using connector j
Creating a Database in MySQL
Creating a Database in MySQL       After establishing the connection with MySQL database by using... the connection with MySQL database and takes a database name as its input
JAVA DATABASE CONNECTION WITH JTABLE
JAVA DATABASE CONNECTION WITH JTABLE  HOw To Load Database Contents From Access Database to JTable without using Vector
Hibernate- Oracle connection - Hibernate
Hibernate- Oracle connection  In Eclipse I tried Windows --> Open perspective--> other in that Database Development Right click... and password as scott and tiger. clicked Test connection. I'm getting Ping
database connection by using java bean
database connection by using java bean  i need a code for bean class to connect to mysql database. subsequently to use dis bean class whereever i need 2 connect 2 database
About DataBase Connectivity
About DataBase Connectivity  I want a simple code in which is like two tier application one system have data base and application is running on other system. Pls inform the way hoe to connect both system {I want to run

Ads