java hibernate +mysql demo

origin article:http://www.javatpoint.com/example-to-create-hibernate-application-in-eclipse-ide 

required jar files: 
hibernate: http://www.javatpoint.com/src/hb/hibernatejar.zip
mysql connector Jar:  http://files.cnblogs.com/files/netact/mysql-connector-java-5.1.36.zip 

 

1,create a java project in eclipse.

 

2, add package:com.javatpoint.mypackage and Employee class:

 1 package com.javatpoint.mypackage;
 2 
 3 public class Employee {
 4     
 5     private int id;
 6     private String firstName, lastName;
 7 
 8     public int getId() {
 9         return id;
10     }
11 
12     public void setId(int id) {
13         this.id = id;
14     }
15 
16     public String getFirstName() {
17         return firstName;
18     }
19 
20     public void setFirstName(String firstName) {
21         this.firstName = firstName;
22     }
23 
24     public String getLastName() {
25         return lastName;
26     }
27 
28     public void setLastName(String lastName) {
29         this.lastName = lastName;
30     }
31     
32 }
Employee.java

 

3,add hibernate.cfg.xml  , please change the parameters in this file:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC  
 3           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
 4           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 
 6 <hibernate-configuration>
 7     <session-factory>
 8         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
 9         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
10         <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
11         <property name="hibernate.connection.username">hp.wang</property>
12         <property name="hibernate.connection.password">123a</property>
13         <mapping resource="Employee.hbm.xml" />
14     </session-factory>
15 </hibernate-configuration>
hibernate.cfg.xml

 

4, add employee.hbm.xml file,

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC  
 3  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
 4  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping>
 7     <class name="com.javatpoint.mypackage.Employee" table="employee">
 8         <id name="id">
 9             <generator class="assigned"></generator>
10         </id>
11 
12         <property name="firstName"></property>
13         <property name="lastName"></property>
14 
15     </class>
16 
17 </hibernate-mapping>  
employee.hbm.xml

 

5, add the class with main():

 1 package com.javatpoint.mypackage;
 2 
 3 import java.util.Random;
 4 
 5 import org.hibernate.Session;
 6 import org.hibernate.SessionFactory;
 7 import org.hibernate.Transaction;
 8 import org.hibernate.cfg.Configuration;
 9 
10 public class StoreData {
11     public static void main(String[] args) {
12 
13         // creating configuration object
14         Configuration cfg = new Configuration();
15         cfg.configure("hibernate.cfg.xml");// populates the data of the
16                                             // configuration file
17 
18         // creating seession factory object
19         SessionFactory factory = cfg.buildSessionFactory();
20 
21         // creating session object
22         Session session = factory.openSession();
23 
24         // creating transaction object
25         Transaction t = session.beginTransaction();
26 
27         Employee e1 = new Employee();
28         Random rd = new Random();
29         int id = rd.nextInt(50000) + 1;
30         e1.setId(id);
31         e1.setFirstName("firstname"+id);
32         e1.setLastName("lastname"+id);
33 
34         session.persist(e1);// persisting the object
35 
36         t.commit();// transaction is committed
37         session.close();
38 
39         System.out.println("successfully saved");
40 
41     }
42 }
StoreData.java

6, create db and table in mysql:

1 CREATE TABLE `employee` (
2   `id` int(11) NOT NULL,
3   `firstName` varchar(50) DEFAULT NULL,
4   `lastName` varchar(50) DEFAULT NULL,
5   PRIMARY KEY (`id`)
6 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
create table

7, run the application.

 

SQL Dialects in Hibernate

For connecting any hibernate application with the database, you must specify the SQL dialects. There are many Dialects classes defined for RDBMS in the org.hibernate.dialect package. They are as follows:



 

RDBMSDialect
Oracle (any version)org.hibernate.dialect.OracleDialect
Oracle9iorg.hibernate.dialect.Oracle9iDialect
Oracle10gorg.hibernate.dialect.Oracle10gDialect
MySQLorg.hibernate.dialect.MySQLDialect
MySQL with InnoDBorg.hibernate.dialect.MySQLInnoDBDialect
MySQL with MyISAMorg.hibernate.dialect.MySQLMyISAMDialect
DB2org.hibernate.dialect.DB2Dialect
DB2 AS/400org.hibernate.dialect.DB2400Dialect
DB2 OS390org.hibernate.dialect.DB2390Dialect
Microsoft SQL Serverorg.hibernate.dialect.SQLServerDialect
Sybaseorg.hibernate.dialect.SybaseDialect
Sybase Anywhereorg.hibernate.dialect.SybaseAnywhereDialect
PostgreSQLorg.hibernate.dialect.PostgreSQLDialect
SAP DBorg.hibernate.dialect.SAPDBDialect
Informixorg.hibernate.dialect.InformixDialect
HypersonicSQLorg.hibernate.dialect.HSQLDialect
Ingresorg.hibernate.dialect.IngresDialect
Progressorg.hibernate.dialect.ProgressDialect
Mckoi SQLorg.hibernate.dialect.MckoiDialect
Interbaseorg.hibernate.dialect.InterbaseDialect
Pointbaseorg.hibernate.dialect.PointbaseDialect
FrontBaseorg.hibernate.dialect.FrontbaseDialect
Firebirdorg.hibernate.dialect.FirebirdDialect

 

..

 

转载于:https://www.cnblogs.com/netact/p/4833614.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值