Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Hibernate Database connectivity Hibernate mysql connection.
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


ratna rathor
Database connectivity Hibernate mysql connection.
2 Answer(s)      2 years and 6 months ago
Posted in : Hibernate


How to do database connectivity in Hibernate using mysql?


Advertisement
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... The Tutorial illustrate an example from PHP Mysql Database Connection
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
Establish a Connection with MySQL Database
Establish a Connection with MySQL Database   ... coding methods of establishing the connection between MySQL database and quartz... that establishes the connection with the MySQL database by using the JDBC driver
database connectivity
database connectivity  how to create database connectivity between HTML and sql server2005
database connectivity
database connectivity  describe java program steps in order to get connectivity to database along with example
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
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
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
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.
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.. 
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
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
connection with MySQL to java.
connection with MySQL to java.   how to connect MySQL database with jsp
Hibernate connection management - Hibernate
Hibernate connection management  What is Hibernate Connection management?  Hi friend,read for more information,http://www.roseindia.net/hibernate
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 Connection
DataBase Connection  How to connect java and Oracle 10g? Tell me Jdbc connection
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
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
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
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
database connection  how to connect the jsp page with database
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
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
Advertisements
 

 

 

DMCA.com