java - 找不到适用于 jdbc :oracle:thin:@localhost:xe 的合适驱动程序

标签 java spring jdbc

我在连接到我的项目时发现 ojdbc drivrr 存在问题。即使我将 ojdbc14.jar 放在类路径上,但它仍然对我不起作用。我必须提前感谢让我摆脱这个。这是myspringcfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="fetchBean" class="com.prime.datasource.FetchBean">
    <property name="DataSource" ref ="ds" />
</bean>

<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />  
    <property name="url" value="jdbc:oracle:thin:@192.168.3.44:1521:xe" />  
    <property name="username" value="harish" />  
    <property name="password" value="password" />  
</bean>  

</beans>

这是fetchbean.java

package com.prime.datasource;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

/**
 * @author agubbala
 */
public class FetchBean implements Fetch {

    //Constants for Queries
    String NAMESQL = "SELECT NAME FROM EMP WHERE EMPID = ?";
    String SALSQL = "SELECT SAL FROM EMP WHERE EMPID = ?";
    String DEPTSQL = "SELECT DEPTNO FROM ENO WHERE EMPID = ?";

    private DataSource dataSource;

    public void setDataSource (org.springframework.jdbc.datasource.DriverManagerDataSource dataSource) {
        this.dataSource = dataSource;
    }

    /* (non-Javadoc)
     * @see com.prime.datasource.Fetch#fetchEmpName(int)
     */
    @Override
    public String fetchEmpName(int empId) {
        return getData(NAMESQL, empId);
    }

    /* (non-Javadoc)
     * @see com.prime.datasource.Fetch#fetchEmpSal(int)
     */
    @Override
    public String fetchEmpSal(int empId) {
        return getData(SALSQL, empId);
    }

    /* (non-Javadoc)
     * @see com.prime.datasource.Fetch#fetchEmpDeptNo(int)
     */
    @Override
    public String fetchEmpDeptNo(int empId) {
        return getData(DEPTSQL, empId);
    }

    //My Bussiness Logic
    public String getData(String sqlQuery, int empId) {
        if (dataSource != null) {
            try {
                Connection conn = dataSource.getConnection();
                PreparedStatement stmt = conn.prepareStatement(sqlQuery);
                stmt.setInt(1, empId);
                ResultSet rs = stmt.executeQuery();
                if (rs.next())
                    return rs.getString(0);
                else
                    return "";

            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
        } else
            return null;
    }

}

这是fetch.java

public interface Fetch {
    public String fetchEmpName(int empId);
    public String fetchEmpSal(int empId);
    public String fetchEmpDeptNo(int empID);
}

这是fetchclient.java

public class FetchClient {
    public static void main(String[] args) {
        FileSystemResource cfg = new FileSystemResource(".\\src\\SpringCfg.xml");
        XmlBeanFactory factory = new XmlBeanFactory(cfg);
        Fetch fetchObj = (Fetch) factory.getBean("fetchBean");

        System.out.println("Name of EMP: " + fetchObj.fetchEmpName(100));
        System.out.println("Name of EMP: " + fetchObj.fetchEmpSal(100));
        System.out.println("Name of EMP: " + fetchObj.fetchEmpDeptNo(100));


    }

}

最佳答案

使用oracle 10g和jdk 1.7,您需要有ojdbc7您可以找到的驱动程序 here 。您需要将它放在您的类路径中。还有一件事,替换<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />

<property name="driverClassName" value="oracle.jdbc.OracleDriver" />

这是使用瘦客户端在 Oracle 10g 上进行 JDBC 操作时必须加载的正确类。

关于java - 找不到适用于 jdbc :oracle:thin:@localhost:xe 的合适驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23192849/

相关文章:

java - 如何自动从 IntelliJ 添加测试方法的注释部分?

java - 如何在 Eclipse 中将 Java 项目更改为 gwt Web 应用程序?

Java、Spring 国际化(I18N)不工作

java - 是否有直接 FileDescriptor 实例化的用例?

java - 检查 spring mvc 注册表中的两个密码条目是否相同?

java - Spring 启动错误: Use of @OneToMany or @ManyToMany targeting an unmapped class

sql - PgPreparedStatement 和缓存

java.sql.SQLException : No suitable driver found for jdbc:microsoft

java - Spring Hibernate - 查询以将附加列映射到我的实体 - java.sq.SQLException : Column not found

java - 我们可以在Elasticsearch的独立节点上安装插件来进行测试吗?