当前位置:编程学习 > JAVA >>

关于Hibernate连接sqlserver 2008的问题

我写了一个测试程序,怎么也连不上sqlserver,不知道问题出现在什么地方,请高手们指点,控制台显示的部分提示信息如下:
3018 DEBUG [2013-01-01 20:42:14]  opening new JDBC connection
3102 DEBUG [2013-01-01 20:42:14]  created connection to: jdbc:sqlserver://202.195.98.126:1433;databaseName=weibo, Isolation Level: 2
drop table weibo.stu
3122 DEBUG [2013-01-01 20:42:14]  drop table weibo.stu
3142 DEBUG [2013-01-01 20:42:14]  Unsuccessful: drop table weibo.stu
3142 DEBUG [2013-01-01 20:42:14]  无法对 表 'weibo.stu' 执行 删除,因为它不存在,或者您没有所需的权限。
create table weibo.stu (id int identity not null, name varchar(255) null, primary key (id))
3142 DEBUG [2013-01-01 20:42:14]  create table weibo.stu (id int identity not null, name varchar(255) null, primary key (id))
3162 ERROR [2013-01-01 20:42:14]  Unsuccessful: create table weibo.stu (id int identity not null, name varchar(255) null, primary key (id))
3162 ERROR [2013-01-01 20:42:14]  指定的架构名称 "weibo" 不存在,或者您没有使用该名称的权限。
3162 INFO  [2013-01-01 20:42:14]  schema export complete
3162 DEBUG [2013-01-01 20:42:14]  returning connection to pool, pool size: 1
3163 INFO  [2013-01-01 20:42:14]  cleaning up connection pool: jdbc:sqlserver://202.195.98.126:1433;databaseName=weibo
Table created.
3163 DEBUG [2013-01-01 20:42:14]  commit
3163 DEBUG [2013-01-01 20:42:14]  automatically flushing session
3163 DEBUG [2013-01-01 20:42:14]  before transaction completion
3164 DEBUG [2013-01-01 20:42:14]  before transaction completion
3182 DEBUG [2013-01-01 20:42:14]  committed JDBC Connection
3182 DEBUG [2013-01-01 20:42:14]  after transaction completion
3182 DEBUG [2013-01-01 20:42:14]  aggressively releasing JDBC connection
3182 DEBUG [2013-01-01 20:42:14]  releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
3183 DEBUG [2013-01-01 20:42:14]  returning connection to pool, pool size: 1
3183 DEBUG [2013-01-01 20:42:14]  after transaction completion


我是在运行HibernateExportSchema时报的上面的问题,HibernateExportSchema这个类的源码如下:
package com.entity;


import java.io.File;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class HibernateSchemaExport {
 static Session session;

 static Configuration config = null;
 static Transaction tx = null;

 public static void main(String[] args) {

  try {
   config = new Configuration().configure(new File(
     "src/hibernate.cfg.xml"));

   System.out.println("Creating tables...");

   SessionFactory sessionFactory = config.buildSessionFactory();
   session = sessionFactory.openSession();
   tx = session.beginTransaction();

   SchemaExport schemaExport = new SchemaExport(config);
   schemaExport.create(true, true);

   System.out.println("Table created.");

   tx.commit();

  } catch (HibernateException e) {
   e.printStackTrace();
   try {
    tx.rollback();
       } catch (HibernateException e1) {
    e1.printStackTrace();
   }
  } finally {

  }
 }

}


下面是Student.hbm.xml的源代码
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
    <class name="com.entity.Student" table="stu" schema="weibo" >
        <id name="id" type="int">
            <column name="id"  />
            <generator class="native" />
        </id>
          
        <property name="name" type="string" column="name"/>

    </class>
</hibernate-mapping>
 
Hibernate.cfg.xml的源码如下:
<?xml version='1.0' encoding='utf-8'?>
 <!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


 <hibernate-configuration>
 <session-factory>

<!--配置SQLServer连接属性-->
 <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
  <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
  <property name="connection.url">jdbc:sqlserver://202.195.98.126:1433;databaseName=weibo</property>
  <property name="connection.username">sa</property>
  <property name="connection.password">asdf@123</property>

<!--在控制台显示SQL语句-->
 <property name="show_sqlserver">true</property>

<!--根据需要自动生成、更新数据表-->
 <property name="hbm2ddl.auto">update</property>
  <property name="myeclipse.connection.profile">sqlserver</property>

<!--注册所有ORM映射文件-->
 <mapping resource="com/entity/Student.hbm.xml" />
 </session-factory>
 </hibernate-configuration>


请问我上面的配置哪里有问题吗,我试过了现有的信息是可以连接上SQLServer数据库,但是就是不能使用HibernateExportSchema自动生成数据库的表,我试过用JDBC直接操作数据库是没有问题的,为什么用hibernate就不可以了呢,报的是没有权限,搞不懂,请高手指导!!





hibernate sqlserver --------------------编程问答-------------------- 你的 weibo 库中,没有任何表啊 --------------------编程问答-------------------- 另外,你的这个数据库放到公网上,任何人都可以连接并执行操作的。 --------------------编程问答-------------------- 是的,没有表,我就是用HibernateExportSchema生成表的 --------------------编程问答-------------------- 在生成的时候一直报错,一直提示“无法对 表 'weibo.stu' 执行 删除,因为它不存在,或者您没有所需的权限。”,没有遇到过这种情况,请高手指点! --------------------编程问答-------------------- 用JDBC可以这样创建表吗
create table weibo.stu (id int identity not null, name varchar(255) null, primary key (id)) --------------------编程问答-------------------- 使用JDBC是可以的! --------------------编程问答-------------------- 我尝试过直接使用JDBC去连接数据库,没有问题,就是没有办法使用Hibernate的自动生成工具类HibernateExportSchema来生成表。 --------------------编程问答--------------------
引用 7 楼 liupingtoday 的回复:
我尝试过直接使用JDBC去连接数据库,没有问题,就是没有办法使用Hibernate的自动生成工具类HibernateExportSchema来生成表。

关键它是指库不存在

3162 ERROR [2013-01-01 20:42:14]  指定的架构名称 "weibo" 不存在,或者您没有使用该名称的权限。 --------------------编程问答-------------------- 是报这个错误,但是这个数据库的名字我已经建好了,就是找不到原因在什么地方,是没有权限的问题吗?
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,