服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

jbuilderx+sql server开发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驱动方面的配置

  找到

## 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:.

  这段,这里是说默认的是使用hypersonicsql,我们使用的是ms sql server,因此将整段注释掉

## 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:.

  并且,找到

## 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

  这段,比如我们使用的数据库服务器机器名为yuj,数据库名为testhi,连接到数据库上去的用户名为sa,密码为sa,则修改后这段成为

## 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

  7、创建一个类testhibernate.person,这是个标准的javabean,只有3个属性和相应的get/set方法

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;
 }
}

  8、创建一个对象-关系映射的xml文件person.hbm.xml,放在和person.java相同的目录下面

<?xml version="1.0" encoding="gb2312"?>
<!doctype hibernate-mapping system "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
 <class name="testhibernate.person">
  <!--hibernate为我们生成主键id-->
  <id name = "id" unsaved-value = "null">
   <generator class="uuid.hex"/>
  </id>

  <!--默认把类的变量映射为相同名字的表列,当然我们可以修改其映射方式-->
  <property name="name"/>
  <property name="address"/>
 </class>
</hibernate-mapping>

  9、创建调用类person的客户端程序client1.java

package testhibernate;

import net.sf.hibernate.session;
import net.sf.hibernate.transaction;
import net.sf.hibernate.sessionfactory;
import net.sf.hibernate.cfg.configuration;
import net.sf.hibernate.tool.hbm2ddl.schemaexport;

/**
*本类只是用来创建表的,并不往表内部插入任何数据,并且只能使用一次,否则会删除已有的表的
*/
public class client1
{
 private static sessionfactory sessionfactory;

 public static void main(string[] args) throws exception
 {
  configuration conf = new configuration().addclass(person.class);

  //第一次运行时用来在数据库中创建表
  //并且把sql语句输出到txt文件用的
  //以后的运行不能使用该段代码,否则每次都会先删除原表,再新建该表
  schemaexport dbexport = new schemaexport(conf);
  dbexport.setoutputfile("sql.txt");
  dbexport.create(true, true);
 }
}

package testhibernate;

import net.sf.hibernate.session;
import net.sf.hibernate.transaction;
import net.sf.hibernate.sessionfactory;
import net.sf.hibernate.cfg.configuration;
import net.sf.hibernate.tool.hbm2ddl.schemaexport;

public class client2
{
 private static sessionfactory sessionfactory;

 public static void main(string[] args) throws exception
 {
  configuration conf = new configuration().addclass(person.class);
  sessionfactory = conf.buildsessionfactory();
  session s = sessionfactory.opensession();

  transaction t = s.begintransaction();

  person yuj = new person();
  yuj.setname("john");
  yuj.setaddress("上海");

  person x = new person();
  x.setname("zhaoyh");
  x.setaddress("上海");

  //持久化
  s.save(yuj); //此时yuj已经可以在数据库中找到
  s.save(x); //此时x已经可以在数据库中找到

  t.commit();
  s.close();
 }
}

  查看数据库中,增加了2条记录,ok!初步使用成功了,剩下的慢慢研究吧……

扫描关注微信公众号