问题
I am connecting to an oracle database using a connection pool. I have set up my datasource file but when I try to get a connection I get the error
The method getConnection() is undefined for the type DataSource
I have searched the internet and it doesn't seem to be a common problem. I am not sure what I have done wrong.
The code is:
<datasources>
<local-tx-datasource>
<jndi-name>DefaultDS</jndi-name>
<connection-url>jdbc:oracle:thin:@ED.OPTO:1521:OPY>
<driver-class>oracle.driver.OracleDriver</driver-class>
<user-name>temiuser</user-name>
<password>password</password>
</local-tx-datasource>
import javax.activation.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.sql.*;
import javax.sql.*;
public class dataSource {
// load the driver
Class.forName("oracle.jdbc.OracleDriver");
String sourceFile = "java:/TVEContent";
InitialContext ic = new InitialContext();
DataSource ds = (DataSource)ic.lookup(sourceFile);
Connection conn = ds.getConnection();
Statement stmnt = conn.createStatement("select * from tve");
}
Please help!!!!
回答1:
Name clash: import javax.sql.DataSource;
BTW should it not be ic.lookup("DefaultDS")
(unsure at the moment)?
回答2:
The problem seem to be this library:
import javax.activation.DataSource;
Try this instead:
javax.sql.DataSource ds = (javax.sql.DataSource)ic.lookup(sourceFile);
回答3:
My guess is that your driver does not match your runtime, check that your oracle jar file matches your jdk version
Because the error "The method getConnection() is undefined for the type DataSource" means : method getConnection() of Datasouce version "1.foo" not found. Your driver method getConnection() implements Datasource version "1.bar"
来源:https://stackoverflow.com/questions/9403593/the-method-getconnection-is-undefined-for-the-type-datasource