刚接触hibernate,请前辈指教

ttoc 2004-11-02 04:55:06
我哭了,我发现我真的很苯,看了很多资料也没看明白,希望哪位能帮小弟一把,给我做个hibernate+jtds驱动 连sqlserver200的jsp页,连本机的pubs数据库,随便取一条记录就可以,真的很着急,小弟先谢了
...全文
179 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ttoc 2004-11-04
  • 打赏
  • 举报
回复
谢谢大家了~~~
ttoc 2004-11-03
  • 打赏
  • 举报
回复
我的邮箱是ttoc@163.com
fmzbj 2004-11-03
  • 打赏
  • 举报
回复
用这个:
<property name="hibernate.connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>

<property name="dialect">
net.sf.hibernate.dialect.SQLServerDialect
</property>
紫翎观星 2004-11-03
  • 打赏
  • 举报
回复
学习!

能不能给我也发一份:cwx714@126.com
ttoc 2004-11-02
  • 打赏
  • 举报
回复
to alphadd(大海):

谢谢了,果然能用,但是怎么在jsp中用呢???!!!
刘大黑 2004-11-02
  • 打赏
  • 举报
回复
开发的IDE:JBuilderX
 
   使用的数据库:MS Sql Server 2000
 
   使用的数据库驱动:jtds

  说明:

  1、hibernate在配置文件中明确说明“Microsoft Driver (not recommended!)”,因此先使用jtds Driver。
 

 2、jtds 可以http://sourceforge.net/projects/jtds具体下载的链接http://prdownloads.sourceforge.net/jtds/jtds-0.9.jar?use_mirror=unc下载到。


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

  准备工作:

  1、下载Hibernate,目前最高版本是2.1.6 地址如下: http://prdownloads.sourceforge.net/hibernate/hibernate-2.1.6.zip?use_mirror=optusnet

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

  3、在JBuilder中创建一个lib,起名为JTDS,将jtds-0.9.jar放进去

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

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

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

  4、在testhibernate项目中创建一个目录,例如src(src是项目标准的代码位置)

  5、将hibernate源文件里的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

## jTDS
#hibernate.connection.driver_class net.sourceforge.jtds.jdbc.Driver
#hibernate.connection.url jdbc:jtds:sqlserver://160.1.111.166/Sale;SelectMethod=cursor
#hibernate.jdbc.use_scrollable_resultset false

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

## MS SQL Server

hibernate.dialect net.sf.hibernate.dialect.SQLServerDialect
hibernate.connection.username sale_user
hibernate.connection.password 82920810

## jTDS
hibernate.connection.driver_class net.sourceforge.jtds.jdbc.Driver
hibernate.connection.url jdbc:jtds:sqlserver://160.1.111.166/Sale;SelectMethod=cursor
hibernate.jdbc.use_scrollable_resultset false

  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();
 }
}
yujiabian 2004-11-02
  • 打赏
  • 举报
回复
留下你的信箱,我发给你一个在oracle平台的例子
niyboy 2004-11-02
  • 打赏
  • 举报
回复
给你一个简单的配置,看能不能帮你;


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

<hibernate-configuration>
<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory name="java:comp/env/hibernate/SessionFactory">

<!-- properties -->
<property name="connection.datasource">my/first/datasource</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/onetomany?</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool.size">20</property>
<property name="hibernate.show_sql">true</property>
<property name="jdbc.fetch_size">50</property>
<property name="jdbc.batch_size">25</property>
<property name="jdbc.use_scrollable_resultset">false</property>
<property name="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</property>

<!-- mapping files -->
<mapping resource="teacher.hbm.xml"/>
<mapping resource="student.hbm.xml"/>

</session-factory>

</hibernate-configuration>
ttoc 2004-11-02
  • 打赏
  • 举报
回复
麻烦指点一下
ttoc 2004-11-02
  • 打赏
  • 举报
回复
to loverisyour(我是民工又咋的,不能搞开发啊!)

另外一个配置文件????呵呵?刚刚接触,不知道两一个叫啥!!!
liuyb94242 2004-11-02
  • 打赏
  • 举报
回复
这个错误是报你的方言错误,
我看你用的是net.sf.hibernate.dialect.SQLServerDialect
是不是你的hibernate的版本没有SQLServerDialect这个类啊,
你用SybaseDialect也可以的。
loverisyour 2004-11-02
  • 打赏
  • 举报
回复
不能设置方言?看着好象没写错啊,是不是你的hibernate少包,另外一个配置文件没放到一起吧 ?还真没碰到过这样的情况
ttoc 2004-11-02
  • 打赏
  • 举报
回复
我们要求使用hibernate通过jtds连接sqlserver,我的hibernate.cfg.xml是这么写的
<session-factory>
<property name="hibernate.dialect">net.sf.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:jtds:sqlserver://localhost/netcieve;SelectMethod=cursor</property>
<property name="hibernate.jdbc.use_scrollable_resultset">true</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.jdbc.fetch_size">50</property>
<!-- Mapping files -->
.......

.......


我要连本机的netcieve数据库,密码为空,这么写有错吗?运行时为什么出现异常,说:javax.servlet.ServletException: The dialect was not set. Set the property hibernate.dialect.

????希望大家能告诉我这么做,先谢谢了

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧