Home Answers Viewqa Hibernate Database connectivity Hibernate mysql connection.

 
 


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

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 Pages:
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
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 --> <
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
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
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 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
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
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
JDBC CONNECTIVITY
static void main(String args[]) throws Exception{ Connection connection...){ rs.close(); } if(connection!=null){ } } } public static Connection getConnection
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
Check PHP MySQL Connectivity
Check PHP MySQL Connectivity: The first step of any kind of connectivity... a connection to a MySQL server. mysql_error(): Returns the error message. mysql_select_db: Select a MySQL database mysql_query: It is a function which is used for send
connectivity - JDBC
, Please check the mysql-connector-java-5.0.6-bin.jar file for Connection...connectivity  I hav MySQL 5.0, JDK 1.5, Tomcat 5.0 in my system when I tried to connect to database and insert data it is showing exeception
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
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
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... driver as per your application needs. About JDBC Java Database Connectivity
connection with MySQL to java.
connection with MySQL to java.   how to connect MySQL database with jsp
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
C Program with Database Connectivity
C Program with Database Connectivity  How To Connect MySQL Database in C Program with Graphics
JDBC - Java Database Connectivity Tutorial
JDBC - Java Database Connectivity Tutorials       Java Database Connectivity...; New Features in JDBC 4.0 Java database connectivity (JDBC) is the Java
Connectivity with sql in detail - JDBC
) { System.out.println("MySQL Connect Example."); Connection conn = null...Connectivity with sql in detail  Sir/Madam, I am.... Thankyou.  Hi Friend, Put mysql-connector
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
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
Jdbc Mysql Connection Url
JDBC Mysql Connection Url      ... JDBC Mysql Connection. In this program, the code explain the JDBC url and string connection that helps you in connecting between url and database. For this we
Correctly Open and Close database connection
; } Completely Open and Close A Database Connection Database Connection... depends on database connectivity. Therefore it is very important to open and close database connections properly. If any database connection is open
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
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
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
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
JDBC CONNECTIVITY
file in environment variable.   Java Mysql connectivity Other JDBC...JDBC CONNECTIVITY  How do i connect my database to java. I have the connector file and i place it in lib of jdk folder and installed the jdbc driver
connectivity
connectivity  fetching the value of any form into the database
connectivity
connectivity  how to connect java with database
regarding connectivity program only - JDBC
) { System.out.println("MySQL Connect Example."); Connection con = null; String url...regarding connectivity program only  can u give me code regarding..., i am sending simple code of connectivity import java.sql.*; public
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
connectivity step
connectivity step  sir.pls give me step one by one for connect... Class.forName("oracle.jdbc.driver.OracleDriver"); 3) Connect to database:*** a) If you are using oracle oci driver,you have to use: Connection conn
Problem with open connection - Hibernate
hibernate application and the database is ORACLE 10g.I am getting the below error.I connected to the database by using JDBC(with same driver and url).Please tell me... open connection
Database Connection - JDBC
Database Connection  In java How will be connect Database through JDBC?  Hi Friend, Please visit the following link: http://www.roseindia.net/jdbc/jdbc-mysql/MysqlConnect.shtml Thanks
Connection to database - Java Beginners
, we have 0nly used mysql database... Thanks...Connection to database   HI I have just Started working on a Project on Jsp We hve some JSP Pages & trying to connect to our Database . We
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
java database connection - Struts
; value="jdbc:mysql:///database_name?autoReconnect=true"...java database connection  how to connect a database in struts program?  First Add two jar file1> commons-dbcp-1.2.x.jar2> mysql
connection with mysql with jstl - JSP-Servlet
connection with mysql with jstl  hi, i m working on jstl . i hv done connection in MS -Access with JSTL . The code is working well. but when i am trying to connect database with mysql 5.0 it is creating problem. Plz tell me
Check Whether Record is already Exists or Not with Database Connectivity - Java Beginners
Check Whether Record is already Exists or Not with Database Connectivity ... in the database with Swing Application ,if It is Already Exists then I want To Show MsgBox else that will Store in Database. plz Help Me Sir  Hi Friend, Try
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
ZF DB Connectivity
ZF Database Connectivity: In this current tutorial we will study how.... The connectivity with database, declaring table etc. and other activities like...: First of all we need to establish the connection with the database, to do
Database connectivity with jsp code - JSP-Servlet
Database connectivity with jsp code  I have written a program in java having connectivity with online_exam. Its working properly. Connection has been established and the code in java is showing the output. But the problem
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.
connection
connection   how to make multiple database connection using jdbc

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.