我无法运行以下代码,因为我想我缺少一个依赖项(jar文件), 当我运行代码时,它显示以下错误
SEVERE:java.lang.RuntimeException:无法编译的源代码 - 找不到符号 符号:类DataSource location:class 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 :(得分: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等。
答案 1 :(得分:0)
我无法解决DataSource的问题,但是我跟着1使用BasicDataSource来解决问题,现在它完全有效。