学习hibernate,执行save方法报错。
org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:97)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1353)
at com.bjpowernode.hibernate.Client.main(Client.java:24)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@localhost:1521:orcl
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
… 5 more
Exception in thread “main” org.hibernate.TransactionException: Transaction not successfully started
at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:183)
at com.bjpowernode.hibernate.Client.main(Client.java:39)
使用 hibernate3.6 + oracle11g数据库
配置文件放在src目录下
试过用classes12.jar,报同样错误,附上代码及配置,麻烦高手帮看看
hibernate.cfg.xml:
<!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=”hibernate.connection.driver_class”>org.hibernate.dialect.Oracle10gDialect</property>
<property name=”hibernate.connection.url”>jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name=”hibernate.connection.username”>system</property>
<property name=”hibernate.connection.password”>system</property>
<property name=”hibernate.dialect”>org.hibernate.dialect.Oracle10gDialect</property>
<property name=”hibernate.show_sql”>true</property>
<!– <property name=”hibernate.format_sql”>true</property>–>
<mapping resource=”com/bjpowernode/hibernate/User.hbm.xml”/>
</session-factory>
</hibernate-configuration>
User.java
package com.bjpowernode.hibernate;
import java.util.Date;
public class User {
private String id;
private String name;
private String password;
private Date createTime;
private Date expireTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getExpireTime() {
return expireTime;
}
public void setExpireTime(Date expireTime) {
this.expireTime = expireTime;
}
}
测试类:
package com.bjpowernode.hibernate;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Client {//测试类2
public static void main(String[] args) {
//读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
//建立SessionFactory
SessionFactory factory = cfg.buildSessionFactory();
//取得session
Session session = null;
try {
session = factory.openSession();
//开启事务
session.beginTransaction();//报错
User user = new User();
user.setName(“张三”);
user.setPassword(“123”);
user.setCreateTime(new Date());
user.setExpireTime(new Date());
//保存User对象
session.save(user);//报错
//提交事务
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
//回滚事务
session.getTransaction().rollback();
}finally {
if (session != null) {
if (session.isOpen()) {
//关闭session
session.close();
}
}
}
}
}
package com.bjpowernode.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
* 将hbm生成ddl
* @author Administrator
*
*/
public class ExportDB {//测试类1
public static void main(String[] args) {
//默认读取类路径hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();//configure(resource)
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);//报错
}
}