站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题JBuilderX+SQL Server开发hibernate

JBuilderX+SQL Server开发hibernate

摘要: 最近才开始潜心研究hibernate的使用,刚在看入门的材料,帖子很多,但对我需要的都不算太清楚。
  环境:

   开发的IDE:JBuilderX
 
   使用的数据库:MS Sql Server 2000
 
   使用的数据库驱动:JSQL Driver(JDBC 3.0)

  说明:

  1、hibernate在配置文件中明确说明“Microsoft Driver (not recommended!)”,因此先使用JSQL Driver。
 
  2、JSQL Driver可以到http://www.jnetdirect.com中得到,需要先注册个用户,才能下载到试用的版本。

  3、JDBC3.0只能在JDK1.4及以上版本中使用,JBuilderX默认的是JDK1.4

  准备工作:

  1、下载Hibernate,目前最高版本是2.1.2

  2、在JBuilder中创建一个lib,起名为hibernate_full,将hibernate\lib下的所有jar通通放进去,并将hibernate\hibernate2.jar也放进去

  3、在JBuilder中创建一个lib,起名为JSQL3,将JSQL Driver下的JNetDirect\JSQLConnect\JDBC_3.0_Driver\JSQLConnect.jar放进去

  开始进行例子:
 
  1、创建一个project,命名为testhibernate

  2、在属性里的Required Libraries里加入hibernate_full和JSQL3

  3、在菜单Project --> Project Properties --> Build --> Resource 里选中xml文件,选择“Copy” --在编译该项目的时候,会自动将src文件夹里的xml文件拷贝到classes文件夹里的相应目录下

  4、在testhibernate项目中创建一个src目录

  5、将hibernate源文件里的hibernate\src\hibernate.properties 和 log4j.properties拷贝到testhibernate项目中的src目录下

  6、修改hibernate.properties中关于MS Sql Server 2000驱动方面的配置

  找到

<table borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1> <tr> <td>## HypersonicSQL

hibernate.dialect net.sf.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class org.hsqldb.jdbcDriver
hibernate.connection.username sa
hibernate.connection.password
hibernate.connection.url jdbc:hsqldb:hsql://localhost
hibernate.connection.url jdbc:hsqldb:test
hibernate.connection.url jdbc:hsqldb:.</td></tr></table>
  这段,这里是说默认的是使用HypersonicSQL,我们使用的是MS Sql Server,因此将整段注释掉

<table borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1> <tr> <td>## HypersonicSQL

#hibernate.dialect net.sf.hibernate.dialect.HSQLDialect
#hibernate.connection.driver_class org.hsqldb.jdbcDriver
#hibernate.connection.username sa
#hibernate.connection.password
#hibernate.connection.url jdbc:hsqldb:hsql://localhost
#hibernate.connection.url jdbc:hsqldb:test
#hibernate.connection.url jdbc:hsqldb:.</td></tr></table>
  并且,找到

<table borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1> <tr> <td>## MS SQL Server

#hibernate.dialect net.sf.hibernate.dialect.SQLServerDialect
#hibernate.connection.username sa
#hibernate.connection.password sa

## JSQL Driver
#hibernate.connection.driver_class com.jnetdirect.jsql.JSQLDriver
#hibernate.connection.url jdbc:JSQLConnect://1E1/test</td></tr></table>
  这段,比如我们使用的数据库服务器机器名为yuj,数据库名为testhi,连接到数据库上去的用户名为sa,密码为sa,则修改后这段成为

<table borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1> <tr> <td>## MS SQL Server

hibernate.dialect net.sf.hibernate.dialect.SQLServerDialect
hibernate.connection.username sa
hibernate.connection.password sa

## JSQL Driver
hibernate.connection.driver_class com.jnetdirect.jsql.JSQLDriver
hibernate.connection.url jdbc:JSQLConnect://yuj/testhi</td></tr></table>
  7、创建一个类testhibernate.Person,这是个标准的JavaBean,只有3个属性和相应的get\set方法

<table borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1> <tr> <td>package testhibernate;

public class Person
{
 private String id;
 private String name;
 private String address;

 public void setId(String value)
 {
  this.id = value;
 }

 public String getId()
 {
  return id;
 }

 public void setName(String value)
 {
  this.name = value;
 }

 public String getName()
 {
  return name;
 }

 public void setAddress(String value)
 {
  this.address = value;
 }

 public String getAddress()
 {
  return address;
 }
}</td></tr></table>



↑返回目录
前一篇: 感受JDO 2.0查询语言的特点
后一篇: 在运行时实现Java的多态性