1

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

严重:java.lang.RuntimeException:无法编译的源代码 - 找不到符号符号:类 DataSource 位置:类 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>
4

2 回答 2

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);

另外,附带说明一下,您能否不使用 con、ctx 和 AuthModel 等变量,而使用全名,即 AuthentionModel、XProject、connection、context、dataSource 等。

于 2013-02-11T06:05:12.120 回答
0

我无法解决DataSource的问题,但我按照1使用BasicDataSource解决了问题,现在它完美运行。

于 2013-02-12T00:03:36.277 回答