hibernate mysql demo_java hibernate +mysql demo

1,create a java project in eclipse.

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.javatpoint.mypackage;2

3 public classEmployee {4

5 private intid;6 privateString firstName, lastName;7

8 public intgetId() {9 returnid;10 }11

12 public void setId(intid) {13 this.id =id;14 }15

16 publicString getFirstName() {17 returnfirstName;18 }19

20 public voidsetFirstName(String firstName) {21 this.firstName =firstName;22 }23

24 publicString getLastName() {25 returnlastName;26 }27

28 public voidsetLastName(String lastName) {29 this.lastName =lastName;30 }31

32 }

Employee.java

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 <?xml version="1.0" encoding="UTF-8"?>

2 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

5

6

7

8 org.hibernate.dialect.MySQLDialect

9 com.mysql.jdbc.Driver

10 jdbc:mysql://localhost:3306/test

11 hp.wang

12 123a

13

14

15

hibernate.cfg.xml

4, add employee.hbm.xml file,

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 <?xml version="1.0" encoding="UTF-8"?>

2 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

5

6

7

8

9

10

11

12

13

14

15

16

17

employee.hbm.xml

5, add the class with main():

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.javatpoint.mypackage;2

3 importjava.util.Random;4

5 importorg.hibernate.Session;6 importorg.hibernate.SessionFactory;7 importorg.hibernate.Transaction;8 importorg.hibernate.cfg.Configuration;9

10 public classStoreData {11 public static voidmain(String[] args) {12

13 //creating configuration object

14 Configuration cfg = newConfiguration();15 cfg.configure("hibernate.cfg.xml");//populates the data of the16 //configuration file17

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 = newEmployee();28 Random rd = newRandom();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:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

Oracle9i

org.hibernate.dialect.Oracle9iDialect

Oracle10g

org.hibernate.dialect.Oracle10gDialect

MySQL

org.hibernate.dialect.MySQLDialect

MySQL with InnoDB

org.hibernate.dialect.MySQLInnoDBDialect

MySQL with MyISAM

org.hibernate.dialect.MySQLMyISAMDialect

DB2

org.hibernate.dialect.DB2Dialect

DB2 AS/400

org.hibernate.dialect.DB2400Dialect

DB2 OS390

org.hibernate.dialect.DB2390Dialect

Microsoft SQL Server

org.hibernate.dialect.SQLServerDialect

Sybase

org.hibernate.dialect.SybaseDialect

Sybase Anywhere

org.hibernate.dialect.SybaseAnywhereDialect

PostgreSQL

org.hibernate.dialect.PostgreSQLDialect

SAP DB

org.hibernate.dialect.SAPDBDialect

Informix

org.hibernate.dialect.InformixDialect

HypersonicSQL

org.hibernate.dialect.HSQLDialect

Ingres

org.hibernate.dialect.IngresDialect

Progress

org.hibernate.dialect.ProgressDialect

Mckoi SQL

org.hibernate.dialect.MckoiDialect

Interbase

org.hibernate.dialect.InterbaseDialect

Pointbase

org.hibernate.dialect.PointbaseDialect

FrontBase

org.hibernate.dialect.FrontbaseDialect

Firebird

org.hibernate.dialect.FirebirdDialect

..

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值