hibernate只是用来连接数据库的,没有其它操作,现在想从别的服务器上搜点数据,不会用hibernate配置了
开始是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"><!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration><session-factory>
<property name="hibernate.connection.url">
jdbc:microsoft:sqlserver://*.*.*.11:1433;;SelectMethod=cursor;databaseName=**
</property>
<property name="hibernate.connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name="hibernate.connection.username">**</property>
<property name="hibernate.connection.password">**</property>

<property name="hibernate.show_sql">True</property>
<property name="hibernate.use_outer_join">True</property> <property name="c3p0.max_size">150</property>
<property name="c3p0.min_size">20</property>
<property name="c3p0.timeout">5000</property>
<property name="c3p0.max_statements">200</property>
<property name="c3p0.idle_test_period">3000</property>
<property name="c3p0.acquire_increment">2</property>
<property name="c3p0.validate false">false</property></session-factory>
</hibernate-configuration>
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
HibernateUtil.java
package whwd.hibernate;
import org.hibernate.* ;
import org.hibernate.cfg.* ;
import org.apache.log4j.Logger;
//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;public class HibernateUtil
{
    //private static Log log = LogFactory.getLog(HibernateUtil.class) ;
    private static Logger log = Logger.getLogger("HibernateUtil.class");
    private static final SessionFactory sessionFactory;    static
    {
        try
        {
            Configuration cfg = new Configuration() ;
            //cfg.addClass(Cat.class) ;
            //sessionFactory = cfg.buildSessionFactory() ;
            sessionFactory = cfg.configure("/hibernate.cfg.xml").buildSessionFactory();
        }
        catch (Throwable ex)
        {
            log.error("初始化 SessionFactory creation failed.", ex);
            throw new ExceptionInInitializerError(ex);
        }
    }    public static final ThreadLocal session = new ThreadLocal();    public static SessionFactory getSessionFactory()
    {
            return sessionFactory;
    }    @SuppressWarnings("unchecked")
public static Session currentSession() throws HibernateException
    {
        Session s = (Session)session.get();
        // Open a new Session, if this Thread has none yet
        if (s == null) {
            s = sessionFactory.openSession();
            session.set(s);
        }
        return s;
    }    @SuppressWarnings("unchecked")
public static void closeSession() throws HibernateException
    {
        Session s = (Session)session.get();
        session.set(null);
        if (s != null)
            s.close();
    }    @SuppressWarnings("unchecked")
public static void setSession( Object obj ) throws HibernateException
    {
       session.set(obj);
    }    public static Object getSession() throws HibernateException
    {
       return session.get() ;
    }}
这是之前连接一个服务器上的数据库时的代码,可以执行

解决方案 »

  1.   

    后来,我准备另一个hibernate1.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"><!-- Generated by MyEclipse Hibernate Tools.                   -->
    <hibernate-configuration><session-factory>
    <property name="hibernate.connection.url">
    jdbc:microsoft:sqlserver://*.*.*.*:1474;SelectMethod=cursor;databaseName=***
    </property>
    <property name="hibernate.connection.driver_class">
    com.microsoft.jdbc.sqlserver.SQLServerDriver
    </property>
    <property name="hibernate.connection.username">***</property>
    <property name="hibernate.connection.password">***</property>
    <property name="hibernate.show_sql">True</property>
    <property name="hibernate.use_outer_join">True</property> <property name="c3p0.max_size">150</property>
    <property name="c3p0.min_size">20</property>
    <property name="c3p0.timeout">5000</property>
    <property name="c3p0.max_statements">200</property>
    <property name="c3p0.idle_test_period">3000</property>
    <property name="c3p0.acquire_increment">2</property>
    <property name="c3p0.validate false">false</property></session-factory></hibernate-configuration>
    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&新的HibernateUtil1.java
    package whwd.hibernate;
    import org.hibernate.* ;
    import org.hibernate.cfg.* ;
    import org.apache.log4j.Logger;
    //import org.apache.commons.logging.Log;
    //import org.apache.commons.logging.LogFactory;public class HibernateUtil1
    {
        //private static Log log = LogFactory.getLog(HibernateUtil.class) ;
        private static Logger log = Logger.getLogger("HibernateUtil1.class");
        private static final SessionFactory sessionFactory;    static
        {
            try
            {
                Configuration cfg = new Configuration() ;
                //cfg.addClass(Cat.class) ;
                //sessionFactory = cfg.buildSessionFactory() ;
                sessionFactory = cfg.configure("/hibernate1.cfg.xml").buildSessionFactory();
            }
            catch (Throwable ex)
            {
                log.error("初始化 SessionFactory creation failed.", ex);
                System.out.println("初始化失败");
                throw new ExceptionInInitializerError(ex);
            }
        }    public static final ThreadLocal session = new ThreadLocal();    public static SessionFactory getSessionFactory()
        {
                return sessionFactory;
        }    @SuppressWarnings("unchecked")
    public static Session currentSession() throws HibernateException
        {
            Session s = (Session)session.get();
            // Open a new Session, if this Thread has none yet
            if (s == null) {
                s = sessionFactory.openSession();
                session.set(s);
            }
            return s;
        }    @SuppressWarnings("unchecked")
    public static void closeSession() throws HibernateException
        {
            Session s = (Session)session.get();
            session.set(null);
            if (s != null)
                s.close();
        }    @SuppressWarnings("unchecked")
    public static void setSession( Object obj ) throws HibernateException
        {
           session.set(obj);
        }    public static Object getSession() throws HibernateException
        {
           return session.get() ;
        }}
    报得无法初始化的错误,我该怎么改呢。在网上搜说是只能有一个sessionfactory,不知道怎么改,大伙给指点一下
      

  2.   

    哎解决了,这样写能实现,刚才部署没成功。重新部署了,就好了。用两个sessionfactory是不是很占资源呀?总感觉这样写程序不好,没办法知识太少了
      

  3.   

    呵呵,你修改sessionFactory,那么不是耗费资源啊,更换一次连接就要重新加载一遍
      

  4.   

    能不能在hibernateUtil.java中写两个sessionFactory,那跟我现在再写个hibernateUtil1.java有什么不同吗?以前很少考虑效率方面的问题。