当前位置:首页>开发>正文

hibernate怎么连接数据库 使用hibernate注解,怎样连接数据库

2023-05-22 12:33:04 互联网 未知 开发

 hibernate怎么连接数据库 使用hibernate注解,怎样连接数据库

hibernate怎么连接数据库

1 C3P0
只需在hibernate.cfg.xml中加入
5
30
1800
50
还有在classespath中加入c3p0-0.8.4.5.jar

2 dbcp
在hibernate.cfg.xml中加入
100
1
60000
10
100
1
60000
10
还有在classespath中加入commons-pool-1.2.jar 和commons-dbcp-1.2.1.jar.

使用hibernate注解,怎样连接数据库

数据库不需要你连接阿,通过你的配置文件都可以看出,你配置好之后,hibernate帮你连上数据库,而你想得到一个session,可以通过SessionFactory来创建,由于你是用的annotation而不是XML,那具体代码如下:
Configuration cfg = new AnnotationConfiguration().config()
SessionFactory sf = cfg.buildSessionFactory()
Session session = sf.openSession()
这样就创建了一个session之后,就可以对数据库进行操作了。
但是用完别忘了释放资源哦,
close。

求救,hibernate连接数据库

Hibernate连接数据库
class="org.apache.commons.dbcp.BasicDataSource"> value="com.microsoft.sqlserver.jdbc.SQLServerDriver"> value="jdbc:sqlserver://localhost:1433databaseName=数据库名"> 数据库用户名 数据库密码
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> org.hibernate.dialect.SQLServerDialect

eclipse配置hibernate后怎么连接数据库

eclipse配置hibernate后怎么连接数据库的具体方法

root
123456

Hibernate如何动态链接数据库

一.导包 mysql
二.在默认src下创建hibernate.cfg.xml
1.创建xml文件,命名为hibernate.cfg.xml
2.添加约束
(在org.hibernate/hibernate-configuration-3.0.dtd中)
1 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"3 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

org.hibernate.dialect.MySQLDialectjdbc:mysql://localhost:3306/houserentsys

com.mysql.jdbc.Driverroot12345truefalseupdate

hbm2ddl.auto属性:
create:表示启动的时候先drop,再create
create-drop: 也表示创建,只不过再系统关闭前执行一下drop
update: 这个操作启动的时候会去检查schema是否一致,如果不一致会做scheme更新
validate: 启动时验证现有schema与你配置的hibernate是否一致,如果不一致就抛出异常,并不做更新
三.实体 实现序列化接口 封装属性和构造方法 实体.xml 位置随意
(在org.hibernate/hibernate-mapping-3.0.dtd中)
br>"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
在hibernate.cfg.xml 添加 映射文件的引用

七个步骤(在新建的执行文件Test.java中)
//1.加载配置文件
Configuration cfg=new Configuration().configure()
//2.获得sessionfactory
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry()
SessionFactory sf=cfg.buildSessionFactory(serviceRegistry)
//3.创建session
Session session=sf.openSession()
//4.创建事务
Transaction tx=session.beginTransaction()
//5.操作
District dis=new District(100,"海淀区")
session.save(dis)
//6.提交 回滚
tx.commit()//tx.rollback()
//7.释放资源

关于Hibernate连接数据库

这样才是正确的,你那个是依赖JDBC的链接,所以不需要URL和class:

hibernate.properties
hibernate.dialect=org.hibernate.dialect.SQLServerDialect
hibernate.connection.driver_class=com.microsoft.jdbc.sqlserver.SQLServerDriver
hibernate.connection.url=jbdc:microsoft:sqlserver://localhost:2000/SAMPLEDB
hibernate.connection.username=sa
hibernate.connection.password=123456

最新文章