hibernate测试报错No suitable driver found for jdbc:oracle:thin:@localhost:1521:orcl

J2EE 码拜 10年前 (2015-04-20) 1740次浏览 0个评论

学习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数据库

hibernate测试报错No suitable driver found for jdbc:oracle:thin:@localhost:1521:orcl
配置文件放在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);//报错
}
}

hibernate测试报错No suitable driver found for jdbc:oracle:thin:@localhost:1521:orcl
解决了是oracle  jdbc驱动包不对,换成oracle_client安装的ojdbc6.jar
就可以了。没想到 csdn现在这么冷清了,一天了 都没有一个人回答。
hibernate测试报错No suitable driver found for jdbc:oracle:thin:@localhost:1521:orcl
10分
大家都变懒了
hibernate测试报错No suitable driver found for jdbc:oracle:thin:@localhost:1521:orcl
30分
驱动 啊,这个你自己也找到了
hibernate测试报错No suitable driver found for jdbc:oracle:thin:@localhost:1521:orcl
恩,下载的驱动包用不了,以前用过10g啊,同样的包,没有问题,可能是安装的Oracle11g 版本不同吧,感觉Oracle 驱动也是各种版本

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明hibernate测试报错No suitable driver found for jdbc:oracle:thin:@localhost:1521:orcl
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!