The method getConnection() is undefined for the type DataSource

Deadly 提交于 2019-12-13 07:16:58

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!