错误:没有为jdbc找到合适的驱动程序:oracle:thin:@localhost:1521:xe

时间:2017-12-09 18:36:38

标签: java oracle hibernate jdbc

这是我在Hibernate中的第一个试用版 我收到了以下错误

  

错误:没有为jdbc找到合适的驱动程序:oracle:thin:@localhost:1521:xe   线程“main”中的异常org.hibernate.exception.JDBCConnectionException:无法打开连接       在org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:131)       在org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)       在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)       在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)       at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:304)       at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169)       在org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)       at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)       在org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1363)       在com.model.HibernetTest.main(HibernetTest.java:24)   引起:java.sql.SQLException:没有为jdbc找到合适的驱动程序:oracle:thin:@localhost:1521:xe       at java.sql / java.sql.DriverManager.getConnection(DriverManager.java:703)       at java.sql / java.sql.DriverManager.getConnection(DriverManager.java:190)       at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:192)       at org.hibernate.internal.AbstractSessionImpl $ NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:278)       在org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)       ......还有5个

这是我的hibernate.cfg.xml

<?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>
    <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

    <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
    <property name="hibernate.connection.username">sample</property>
    <property name="hinernate.connection.password">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>

    <property name="show_sql">true</property>

    <mapping class="com.model.Student" resource="com/model/Student.hbm.xml" />
</session-factory>

我在我的类路径中添加了odbc jar但仍然收到错误,说找不到Driver。我试过遵循JDBC连接类,它运行得很好。

public class OracleJDBCExample {

public static void main(String[] args) {

    try {

        Class.forName("oracle.jdbc.driver.OracleDriver");

    } catch (ClassNotFoundException e) {

        System.out.println("Where is your Oracle JDBC Driver?");
        e.printStackTrace();
        return;

    }

    System.out.println("Oracle JDBC Driver Registered!");

    Connection connection = null;

    try {

        connection = DriverManager.getConnection(
                "jdbc:oracle:thin:@localhost:1521:xe", "sample", "root");

        PreparedStatement ps=connection.prepareStatement("select * from Employee");
        System.out.println("*******");
        ResultSet rs=ps.executeQuery();
        System.out.println("--->"+rs.toString());
        if(rs.next()) {
            System.out.println("---> "+rs.getString(1));
        }
        rs.close();
        ps.close();

    } catch (SQLException e) {

        System.out.println("Connection Failed! Check output console");
        e.printStackTrace();
        return;

    }

    if (connection != null) {
        System.out.println("You made it, take control your database now!");
    } else {
        System.out.println("Failed to make connection!");
    }

}

}

请帮我理解代码有什么问题?

2 个答案:

答案 0 :(得分:0)

按代码查看并没有错,但我认为您的应用服务器中找不到 odbc.jar 。你能说出你使用的服务器吗?

答案 1 :(得分:0)

您需要下载Oracle's JDBC driver并将其添加到CLASSPATH env变量