使用hibernate连接oracle数据库,SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用hibernate连接oracle数据库,SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".相关的知识,希望对你有一定的参考价值。

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
at org.slf4j.LoggerFactory.<clinit>(LoggerFactory.java:60)
at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:152)
at Test.main(Test.java:12)
Caused by: java.lang.ClassNotFoundException: org.slf4j.impl.StaticLoggerBinder
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
... 3 more

参考技术A 缺少slf4j的 jar 包 参考技术B 你是用什么连接方式连接的?
OCI还是 JDBC
然后你连接的数据库是什么版本,10G对应驱动
classes12.jar 适用于JDK 1.2 and JDK 1.3.
ojdbc14.jar 适用于 JDK 1.4 and 5.0
11G对应驱动ojdbc5.jar: 适用于jdk5 ,ojdbc6.jar: 适用于jdk6

具体jdbc包可以在数据库安装目录下的jdbc/lib/下发现。如果是OCI连接,则驱动版本要和客户端一致,驱动也是在客户端安装目录下jdbc/lib下拷贝。
参考技术C 百度一下:
org.slf4j.impl.StaticLoggerBinder

能找到解决办法,就是没有 这个类,加载进来就可以了。

看看这个:

http://hxwang.blog.51cto.com/1015873/444770

NHibernate4使用Oracle.ManagedDataAccess.dll连接oracle及配置多个数据库连接

NHibernate数据库配置参数在hibernate.cfg.xml中

<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory name="ora10gFactory">
    <!--<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider, NHibernate</property>-->
    <!--property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property-->
    <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
    <property name="connection.driver_class">NHibernate.Driver.OracleManagedDataClientDriver</property>
    <property name="connection.connection_string">
      User ID=jg122;Password=jg122;Data Source=127.0.0.1:1521/orcl
    </property>
    <property name="show_sql">true</property>
    <property name="query.substitutions">true 1, false 0, yes \'Y\', no \'N\'</property>
    <!--<property name="current_session_context_class">managed_web</property>
    <property name="proxyfactory.factory_class">NHibernate.Bytecode.DefaultProxyFactoryFactory,NHibernate</property>-->
    <property name="hbm2ddl.keywords">none</property>
    <!--加载映射-->
    <mapping assembly="Spring.NHibernate.Model"/>
  </session-factory>
</hibernate-configuration>

其中dialect和driver值,可以从对象浏览器中查看NHibernate.dll的结构进行选择。

    

oracle连接使用的是Oracle.ManagedDataAccess.dll,因此driver选择OracleManagedDataClientDriver,Dialect选择最新的Oracle10gDialect

创建数据库实例类代码:

public class NHibernateHelper
    {
        private static ISessionFactory _sessionFactory;

        /// <summary>
        /// 创建ISessionFactory
        /// </summary>
        public static ISessionFactory SessionFactory
        {
            get
            {
                // 默认从 App.config,web.config或者hibernate.cfg.xml查找配置文件;
                //var cfg = new Configuration().Configure("");
                //// 如果配置文件不是以上“App.config,web.config或者hibernate.cfg.xml”,是自定义的配置文件名称;
                //// 要注意使用MSTest进行单元测试时,请写配置文件的绝对路径,否则找不到;
                ////var cfg = new NHibernate.Cfg.Configuration().Configure("D:/ProjectStudyTest/NHibernate/NHibernateStudy/Lesson3.Dao/Config/hibernate.cfg.xml");

                ////如果用户Nunit进行单元测试,写相对路径即可。
                ////var cfg = new NHibernate.Cfg.Configuration().Configure("Config/hibernate.cfg.xml");

                //sessionFactory = cfg.BuildSessionFactory();


                //配置ISessionFactory
                var cfg = (new Configuration()).Configure();
                return _sessionFactory == null ? cfg.BuildSessionFactory() : _sessionFactory;
            }
        }
    }

 

系统默认会在部署根目录下从App.config,web.config或者hibernate.cfg.xml查找配置参数;

       如果要指定nhibernate数据库配置文件,可以使用Configure的重载方法,传入路径参数。

方法一:创建多个cfg.xml配置文件,实现连接多个数据库

 1 public class NHibernateHelper
 2     {
 3         private static ISessionFactory _sessionFactory;
 4 
 5         /// <summary>
 6         /// 创建ISessionFactory
 7         /// </summary>
 8         public static ISessionFactory SessionFactory
 9         {
10             get
11             {
12                 // 默认从 App.config,web.config或者hibernate.cfg.xml查找配置文件;
13                 //var cfg = new Configuration().Configure("");
14                 //// 如果配置文件不是以上“App.config,web.config或者hibernate.cfg.xml”,是自定义的配置文件名称;
15                 //// 要注意使用MSTest进行单元测试时,请写配置文件的绝对路径,否则找不到;
16                 ////var cfg = new NHibernate.Cfg.Configuration().Configure("D:/ProjectStudyTest/NHibernate/NHibernateStudy/Lesson3.Dao/Config/hibernate.cfg.xml");
17 
18                 ////如果用户Nunit进行单元测试,写相对路径即可。
19                 ////var cfg = new NHibernate.Cfg.Configuration().Configure("Config/hibernate.cfg.xml");
20 
21                 //sessionFactory = cfg.BuildSessionFactory();
22 
23                 string path = AppDomain.CurrentDomain.BaseDirectory + "hibernate2.cfg.xml";
24                 //配置ISessionFactory
25                 var cfg = (new Configuration()).Configure(path);
26                 return _sessionFactory == null ? cfg.BuildSessionFactory() : _sessionFactory;
27             }
28         }
29     }

 

方法二:在hibernate.cfg.xml中配置多个数据库连接参数

<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">

  <session-factory name="sqlite">
    <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
    <property name="connection.connection_string">
      Data Source=D:\\code\\SummaryTool\\CSPlugin\\bin\\Debug\\test.db;Version=3;New=False;
    </property>
    <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
    <property name="query.substitutions">true=1;false=0</property>
    <property name="show_sql">true</property>
    <property name="use_outer_join">true</property>
  </session-factory>

  <session-factory name="sqlserver">
   <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider, NHibernate</property>
      <property name="prepare_sql">false</property>
  
   <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
   <property name="connection.connection_string">
        Server=(local);initial catalog=***;Integrated Security=SSPI;User ID=***;Password=***
      </property>
   <property name="show_sql">true</property>
    
   <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
  </session-factory>

</hibernate-configuration>

创建数据库实例代码:

 1 public static ISession GetSession(string strkey)
 2 {
 3     if (!mdic_sessionFactory.ContainsKey(strkey))
 4     {
 5         lock (m_lockHelper)
 6         {
 7             if (!mdic_sessionFactory.ContainsKey(strkey))
 8             {
 9 
10                 string path = AppDomain.CurrentDomain.BaseDirectory + "hibernate.cfg.xml";
11                 //
12                 Configuration m_configurationtmp = new Configuration();
13                 XmlDocument xdoc = new XmlDocument();
14                 xdoc.Load(path);
15 
16 
17                 XmlNode xn = null;
18                 foreach (XmlNode xnsub in xdoc.DocumentElement.ChildNodes)
19                 {
20 
21                     if (xnsub.Attributes["name"].Value == strkey)
22                     {
23 
24                         xn = xnsub;
25                         break;
26                     }
27 
28                 }
29 
30                 XmlTextReader xtr = new XmlTextReader(new StringReader(xn.OuterXml));
31                 m_configurationtmp.Configure(xtr);
32                 //m_configurationtmp.AddAssembly("ServiceCoreModel");
33 
34                 mdic_sessionFactory[strkey] = m_configurationtmp.BuildSessionFactory();
35             }
36         }
37     }
38     return mdic_sessionFactory[strkey].OpenSession();
39 }

 

以上是关于使用hibernate连接oracle数据库,SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".的主要内容,如果未能解决你的问题,请参考以下文章

hibernate和tomcat的连接池问题

NHibernate4使用Oracle.ManagedDataAccess.dll连接oracle及配置多个数据库连接

连接oracle和mysql数据库

Hibernate + Oracle 结合

我在hibernate里配置的数据类型为java。util.Date,数据库连接的是oracle,创建表时老生成的是datetime

使用hibernate连接oracle数据库,SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".