2013-02-11 20 views
1

,因爲我想我缺少的依賴(jar文件)我不能運行下面的代碼, 當我運行的代碼它顯示了以下錯誤無法查找符號的DataSource

重度:了java.lang.RuntimeException:不可編譯的源代碼 - 找不到符號 符號:類數據源 位置:類com.myproject.model

import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import javax.naming.Context; 
import javax.naming.InitialContext; 
import sun.jdbc.odbc.ee.DataSource; //and import javax.sql.DataSource both does not work 

public class AuthModel { 

    public Connection DbConnection(){ 
      Connection con = null; 
     try { 
         Context ctx = new InitialContext(); 
cant find symbol Error >> DataSource ds = new (DataSource)ctx.lookup("mydatabase"); 
         con = ds.getConnection("root", ""); 
         con.setAutoCommit(false); 
        ..... 

依賴

<dependencies> 
     <dependency> 
      <groupId>org.apache.struts</groupId> 
      <artifactId>struts2-core</artifactId> 
      <version>2.3.8</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.struts</groupId> 
      <artifactId>struts2-tiles-plugin</artifactId> 
      <version>2.3.8</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.struts</groupId> 
      <artifactId>struts2-convention-plugin</artifactId> 
      <version>2.3.8</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.struts</groupId> 
      <artifactId>struts2-dojo-plugin</artifactId> 
      <version>2.3.8</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.struts</groupId> 
      <artifactId>struts-taglib</artifactId> 
      <version>1.3.10</version> 
     </dependency> 
     <dependency> 
      <groupId>jdbc</groupId> 
      <artifactId>jdbc-stdext</artifactId> 
      <version>2.0</version> 
      <type>pom</type> 
     </dependency> 
     <dependency> 
      <groupId>javax.sql</groupId> 
      <artifactId>jdbc-stdext</artifactId> 
      <version>2.0</version> 
      <type>pom</type> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.3.2</version> 
       <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
        <compilerArguments> 
         <endorseddirs>${endorsed.dir}</endorseddirs> 
        </compilerArguments> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-war-plugin</artifactId> 
       <version>2.1.1</version> 
       <configuration> 
        <failOnMissingWebXml>false</failOnMissingWebXml> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-dependency-plugin</artifactId> 
       <version>2.1</version> 
       <executions> 
        <execution> 
         <phase>validate</phase> 
         <goals> 
          <goal>copy</goal> 
         </goals> 
         <configuration> 
          <outputDirectory>${endorsed.dir}</outputDirectory> 
          <silent>true</silent> 
          <artifactItems> 
           <artifactItem> 
            <groupId>javax</groupId> 
            <artifactId>javaee-endorsed-api</artifactId> 
            <version>6.0</version> 
            <type>jar</type> 
           </artifactItem> 
          </artifactItems> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 

</project> 
+0

http://docs.oracle.com/javase/7/docs/api/javax/sql/package-summary.html – 2013-02-11 06:04:36

+0

@布萊恩羅奇,我看到,問題應該是依賴。 – 2013-02-11 22:38:33

回答

0

我無法解決DataSource的問題,但我跟着1來解決使用BasicDataSource的問題,現在它完美的工作。

1

我在這裏猜測,但這看起來像一個類路徑問題。我看到你正在導入javax.sql。您是否需要導入javax.sql.*或特別是javax.sql.DataSource?另外,請確保你有必要的罐子。

此外,如果您有兩個不同的包裝具有相同的類,則存在類衝突的可能性。您是否嘗試過重新命名變量與包路徑..又名

public final static String MY_DATABASE = "mydatabase" 
... 
javax.sql.DataSource dataSource = new (javax.sql.DataSource)context.lookup(MY_DATABASE); 

而且,一個一個側面說明一起,你能不能使用像騙子,CTX和AuthModel變量和使用fullnames又名AuthentionModel,XProject,連接,上下文,數據源等。

+0

它沒有工作,當我使用javax.sql。*它顯示導入是未使用的,當我使用javax.sql.DataSource它要求依賴性,但我已經添加了依賴maven – 2013-02-11 22:33:49

+0

你需要添加' javax.sql.DataSource'在你的依賴列表中? – rishi 2013-02-11 23:46:23

相關問題