我的hibernate4.1.2和oracle连接出现问题,帮忙看下,我用的oracle驱动包是ojdbc14.jar
hibernate.cfg.xml配置:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:WJLMGQS</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<property name="format_sql">true</property>
<mapping resource="org/wjlmgqs/person/person.hbm.xml"/>
</session-factory>
</hibernate-configuration>上面配的是oracle,如果换成mysql的就没问题了,person.hbm.xml配置:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping>
<class name="org.wjlmgqs.person.Person">
<id name="id" >
<generator class="native"></generator>
</id>
<property name="name" />
<property name="age" />
</class>
</hibernate-mapping>
Person对象属性:
int id;
String name ;
int age ;
测试代码: public static void main(String[] args) {
Session session = new Configuration().configure().buildSessionFactory().openSession();
Person p = new Person();
session.beginTransaction();
p.setAge(3);
p.setName("wjl");
session.save(p);
session.getTransaction().commit();
session.close();
}
出现错误:
19:57:58,424 WARN org.hibernate.internal.util.xml.DTDEntityResolver:74 - HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
19:58:02,184 WARN org.hibernate.engine.jdbc.internal.JdbcServicesImpl:169 - HHH000342: Could not obtain connection to query metadata : No suitable driver found for jdbc:oracle:thin:@localhost:1521:WJLMGQS
19:58:06,287 INFO org.hibernate.tool.hbm2ddl.SchemaExport:343 - HHH000227: Running hbm2ddl schema export
19:58:06,289 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:353 - Import file not found: /import.sql
19:58:06,359 ERROR org.hibernate.tool.hbm2ddl.SchemaExport:385 - HHH000231: Schema export unsuccessful
java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@localhost:1521:WJLMGQS
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:192)
at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
at org.hibernate.tool.hbm2ddl.DatabaseExporter.<init>(DatabaseExporter.java:52)
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:367)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:304)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:293)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:490)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1741)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1779)
at org.wjlmgqs.person.Test.main(Test.java:18)
19:58:06,363 INFO org.hibernate.tool.hbm2ddl.SchemaExport:405 - HHH000230: Schema export complete
19:58:06,941 WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper:143 - SQL Error: 0, SQLState: 08001
19:58:06,942 ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper:144 - No suitable driver found for jdbc:oracle:thin:@localhost:1521:WJLMGQS
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Could not open connection
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert
。
Caused by: java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@localhost:1521:WJLMGQS
hibernate.cfg.xml配置:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:WJLMGQS</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<property name="format_sql">true</property>
<mapping resource="org/wjlmgqs/person/person.hbm.xml"/>
</session-factory>
</hibernate-configuration>上面配的是oracle,如果换成mysql的就没问题了,person.hbm.xml配置:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping>
<class name="org.wjlmgqs.person.Person">
<id name="id" >
<generator class="native"></generator>
</id>
<property name="name" />
<property name="age" />
</class>
</hibernate-mapping>
Person对象属性:
int id;
String name ;
int age ;
测试代码: public static void main(String[] args) {
Session session = new Configuration().configure().buildSessionFactory().openSession();
Person p = new Person();
session.beginTransaction();
p.setAge(3);
p.setName("wjl");
session.save(p);
session.getTransaction().commit();
session.close();
}
出现错误:
19:57:58,424 WARN org.hibernate.internal.util.xml.DTDEntityResolver:74 - HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
19:58:02,184 WARN org.hibernate.engine.jdbc.internal.JdbcServicesImpl:169 - HHH000342: Could not obtain connection to query metadata : No suitable driver found for jdbc:oracle:thin:@localhost:1521:WJLMGQS
19:58:06,287 INFO org.hibernate.tool.hbm2ddl.SchemaExport:343 - HHH000227: Running hbm2ddl schema export
19:58:06,289 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:353 - Import file not found: /import.sql
19:58:06,359 ERROR org.hibernate.tool.hbm2ddl.SchemaExport:385 - HHH000231: Schema export unsuccessful
java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@localhost:1521:WJLMGQS
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:192)
at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
at org.hibernate.tool.hbm2ddl.DatabaseExporter.<init>(DatabaseExporter.java:52)
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:367)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:304)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:293)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:490)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1741)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1779)
at org.wjlmgqs.person.Test.main(Test.java:18)
19:58:06,363 INFO org.hibernate.tool.hbm2ddl.SchemaExport:405 - HHH000230: Schema export complete
19:58:06,941 WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper:143 - SQL Error: 0, SQLState: 08001
19:58:06,942 ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper:144 - No suitable driver found for jdbc:oracle:thin:@localhost:1521:WJLMGQS
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Could not open connection
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert
。
Caused by: java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@localhost:1521:WJLMGQS
Class.forName("").newInstance();
DriverManager.getConnection(jdbc:oracle:thin:@localhost:1521:wjlmgqs, "sccott", "tiger");
客观地说,没感觉你的配置存在啥问题。
我用mysql,今天将hibernate从4.1.1换成4.1.2就开始报错:java.sql.SQLException: No suitable driver found for jdbc后来查了一下代码,我觉得是hibernate4.1.2的一个bug:
org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl
这个类的configure方法被修改了,原来是直接用Class.forName加载驱动,现在改成用ClassLoaderService加载驱动。ClassLoaderService是org.hibernate.service.classloading.internal.ClassLoaderServiceImpl类的一个实例,它通过ClassLoader.loadClass加载驱动ClassLoader.loadClass只会加载类,Class.forName除了加载类还会对类做做初始化。JDBC规范要求Dirver在初始化时注册到DriverManager。由于用ClassLoader驱动没有注册,所以就会出现找不到驱动的错误。
解决方法:换成hibernate4.1.1,或者自己用Class.forName注册驱动。
原因我都写了,源码也有下载,你可以自己试试看。Hibernate4 bug确实挺多的,我用了4天已经发现2个了,还有一个是使用Proxool的bug:
org.hibernate.service.jdbc.connections.internal.ProxoolConnectionProvider这个类忘了实现Configurable接口,导致无法加载Proxool的配置文件,无法Proxool使用连接池。
rem but allow them to be specified in setenv.bat, in rare case when it is needed.
set CLASSPATH=把classpath给清空了,如果需要填加自己的路径需要在setenv.bat里设置classpath
你写一个jsp,输出一下运行环境下的classpath:<%= System.getProperty("java.class.path") %>
检查一下是否包含了oracle驱动的路径