JBuilderX+SQL Server开发hibernate

news/2024/5/19 1:22:40 标签: sql server, hibernate, hsqldb, jdbc, import, string
环境:

   开发的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!初步使用成功了,剩下的慢慢研究吧……

http://www.niftyadmin.cn/n/1406164.html

相关文章

Android ---js与java的相互调用

android中的关键代码: webview.getSettings().setJavaScriptEnabled(true); webview.addJavascriptInterface(object,"name");//把Name"name"的对象添加到object中。object如果是this,就是window.name webview.loadUrl("file:/…

java equals 区别_java中equals和 == 的区别

摘自:https://blog.csdn.net/weixin_37690143/article/details/797677611、功能不同""是判断两个变量或实例是不是指向同一个内存空间。"equals"是判断两个变量或实例所指向的内存空间的值是不是相同。2、定义不同"equals"在JAVA中是…

iReport JasperReport配置及用户简明手册

关于iReport和JasperReport的简明手册 By Gem 伍子轩 1安装1.1 iReport的安装iReport直接解压后,将J2SDK/lib目录下的tools.jar拷贝到iReport/lib目录下,然后在iReport目录下运行iReport.bat即可。需要注意的是tools.jar的版本必须要与本机安装的jre一致…

C# 读写Excel、CVS

读CVS:C#中提供分析结构化文本文件的方法和属性的类“TextFieldParser”(参考MSDN)using (TextFieldParser tp new TextFieldParser(docsourceMapfileName, Encoding.GetEncoding("UTF8"))){tp.TextFieldType FieldType.Delimited;定义文本分…

Lucene入门与使用

本文主要面向具体使用,适用于已熟悉java编程的lucene初学者。 1. Lucene的简介 1.1 Lucene 历史org.apache.lucene包是纯java语言的全文索引检索工具包。 Lucene的作者是资深的全文索引/检索专家,最开始发布在他本人的主页上,2001年10月…

java 数组名的地址_数组名VS数组名取地址

实验准备:我们使用VC6.0进行实验,首先需要打开VC6.0的RTTI选项(默认关闭)Project->settings->c/c->category->clanguage,将Enable Run-Time TypeInfomation(RTTI)选中测试代码:#include using namespace std;int main(){int arra…

c#对csv的读写

public class CSVFileHelper{/// <summary>/// 将DataTable中数据写入到CSV文件中/// </summary>/// <param name"dt">提供保存数据的DataTable</param>/// <param name"fileName">CSV的文件路径</param>public stati…

OSCache使用

Cache是一种用于提高系统响应速度、改善系统运行性能的技术。尤其是在Web应用中&#xff0c;通过缓存页面的输出结果&#xff0c;可以很显著的改善系统运行性能。本文中作者给大家介绍一个实现J2EE框架中Web应用层缓存功能的开放源代码项目----OSCache。通过应用OSCache&#x…