我是JDBC新手,我正在尝试连接MySQL数据库.我正在使用Connector/J驱动程序,但我找不到我的Class.forName()方法的JDBC连接字符串.
我是Hibernate的新手,阅读本书"使用Hibernate进行Java持久化",我试图从那里实现这个例子.到目前为止,我的Ant构建是成功的,但是当我尝试执行包含main方法的类时,我收到此错误消息:
19-Nov-2011 18:40:09 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.3
19-Nov-2011 18:40:09 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
19-Nov-2011 18:40:09 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
19-Nov-2011 18:40:09 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
19-Nov-2011 18:40:09 org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
19-Nov-2011 18:40:09 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
at persistence.HibernateUtil.<clinit>(Unknown Source)
at hello.Driver.main(Unknown Source)
Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1405)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1427)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
... 2 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用spring 3 mvc的hibernate但是此刻我抛出了这个异常.我想我需要在hibernate.cfg.xml某个地方定义,但不确定在哪里?
我基本上按照这个例子http://www.nabeelalimemon.com/blog/2010/05/spring-3-integrated-with-hibernate-part-a/并特别看到这行代码,假设"神奇地"使用这个找到我的hibernate.cfg文件:
return new Configuration().configure().buildSessionFactory();
Run Code Online (Sandbox Code Playgroud)
我猜这不对吗?我目前有我的hibernate.cfg文件src/com/jr/hibernate/
下面是我的cfg文件:
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/racingleague</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<property name="hibernate.format_sql">true</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Hibernate和Maven开始项目.
我有这样的例外:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2176)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2157)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
at FirstHibernate.com.myhib.CRUDS.CrudsOps.main(CrudsOps.java:15)
Run Code Online (Sandbox Code Playgroud)
这是我的项目结构的截图,(hibernate.cfg.xml在src /中):http: //imageshack.us/photo/my-images/692/screenshotxba.jpg/
CrudsOps.java
package FirstHibernate.com.myhib.CRUDS;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class CrudsOps {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SessionFactory sf = new Configuration().configure().buildSessionFactory();
System.out.println("Cfg and …Run Code Online (Sandbox Code Playgroud)