首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找不到symbol DataSource

找不到symbol DataSource
EN

Stack Overflow用户
提问于 2013-02-11 13:46:43
回答 2查看 4.2K关注 0票数 1

我不能运行下面的代码,因为我想我缺少一个依赖项(jar文件),当我运行代码时,它显示以下错误

严重: java.lang.RuntimeException:无法编译的源代码-找不到符号符号:类DataSource位置:类com.myproject.model

代码语言:javascript
复制
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);
                  .....

依赖关系

代码语言:javascript
复制
 <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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-12 08:03:36

我不能解决DataSource的问题,但我跟随1使用BasicDataSource解决了这个问题,现在它完美地工作了。

票数 0
EN

Stack Overflow用户

发布于 2013-02-11 14:05:12

我在这里推测,但这看起来像是类路径问题。我看到您正在导入javax.sql。您是否需要导入javax.sql.*或特定的javax.sql.DataSource?此外,请确保您确实有必要的jar。

您是否尝试过将变量与包路径一起重命名。又名

代码语言:javascript
复制
public final static String MY_DATABASE = "mydatabase"
...
javax.sql.DataSource dataSource = new (javax.sql.DataSource)context.lookup(MY_DATABASE);

另外,顺便说一句,你能不能不使用con,ctx和AuthModel这样的变量,而使用全名,也就是AuthentionModel,XProject,connection,context,dataSource等等。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14806565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档