posts - 54,  comments - 1,  trackbacks - 0

可以通过3钟方式为Hibernate提供JDBC连接:

1.直接提供JDBC连接对象Connection:

Class.forName("com.mysql.jdbc.Driver"); 
String url 
= "jdbc:mysql://localhost:3306/HibernateTest?user=root&password="
java.sql.Connection conn 
= DriverManager.getConnection(url); 
SessionFactory sessionFactory 
= cfg.buildSessionFactory(); 
Session session 
= sessionFactory.openSession(conn);

2.通过属性文件hibernate.properties配置JDBC来源:

hibernate.show_sql = true 
hibernate.dialect 
= net.sf.hibernate.dialect.MySQLDialect 
hibernate.connection.driver_class 
= com.mysql.jdbc.Driver 
hibernate.connection.url 
= jdbc:mysql://localhost/HibernateTest 
hibernate.connection.username = root
hibernate.connection.password 
= 

3.XML文件hibernate.cfg.xml进行配置:

<?xml version='1.0' encoding='big5'?> 
<!DOCTYPE hibernate-configuration 
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" 
    "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"
> 

<hibernate-configuration> 

    
<session-factory> 

        
<!-- 显示实际操作数据库时的SQL --> 
        
<property name="show_sql">true</property> 
        
<!-- SQL方言,这边设定的是MySQL --> 
        
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property> 
        
<!-- JDBC驱动程序 --> 
        
<property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
        
<!-- JDBC URL --> 
        
<property name="connection.url">jdbc:mysql://localhost/HibernateTest</property> 
        
<!-- 数据库使用者 --> 
        
<property name="connection.username">caterpillar</property> 
        
<!-- 数据库密码 --> 
        
<property name="connection.password">123456</property> 

        
<!-- 对象与数据库表格映像文件 --> 
        
<mapping resource="User.hbm.xml"/> 
    
</session-factory> 

</hibernate-configuration>
posted on 2006-01-02 23:15 ZhuJun 阅读(613) 评论(0)  编辑  收藏 所属分类: 开发手记开源项目

只有注册用户登录后才能发表评论。


网站导航:
 

蜀中人氏,躬耕于珠海

<2006年1月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(2)

随笔分类(71)

随笔档案(54)

博客

文档

站点

论坛

搜索

  •  

积分与排名

  • 积分 - 49829
  • 排名 - 981

最新评论

阅读排行榜