我在连接到我的项目时发现了ojdbc drivrr的问题。我把ojdbc14.jar放在classpath上,但它仍然不适用于我。我必须提前感谢让我摆脱这个。这是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));
}
}
答案 0 :(得分:1)
使用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操作加载的正确类。