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() ;
}}
这是之前连接一个服务器上的数据库时的代码,可以执行
开始是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() ;
}}
这是之前连接一个服务器上的数据库时的代码,可以执行
<!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,不知道怎么改,大伙给指点一下