Java 类连接和类语句的错误“找不到符号”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24398953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:59:29  来源:igfitidea点击:

Error "cannot find symbol" for Class Connection and Class Statement

javamavenjakarta-eejdbcglassfish

提问by Angelo Rivera

I'm using GlassFish, Maven and Commmand Line to host my Project.

我正在使用 GlassFish、Maven 和 Command Line 来托管我的项目。

I created a JDBC database in Glassfish and now I'm trying to create a Table in it with this Java-EE file:

我在 Glassfish 中创建了一个 JDBC 数据库,现在我试图用这个 Java-EE 文件在其中创建一个表:

package edu.ndnu.arivera;

import javax.sql.*;
import javax.annotation.Resource;

public class dbCreate{
    @Resource(name="jdbc/arivera") DataSource ds;

    public void connectAndQueryDB(String username, String password)
    {
        Connection con = ds.getConnection();
        Statement stmt = con.createStatement();
        stmt.executeQuery("CREATE TABLE Voter (firstName varchar(30),lastName varchar(30), address varchar(30), city varchar(30), state varchar(30), zip varchar(30), phone varchar(30), affil varchar(30))");
        con.close();
    }
}

However I get this error:

但是我收到这个错误:

[ERROR] symbol:   class Connection
[ERROR] location: class edu.ndnu.arivera.dbCreate
[ERROR] /home/student/ContestedCounty/src/main/java/edu/ndnu/arivera/dbCreate.java:[12,1] cannot find symbol
[ERROR] symbol:   class Statement
[ERROR] location: class edu.ndnu.arivera.dbCreate

I'm not sure what I'm doing wrong.

我不确定我做错了什么。

回答by Mark Rotteveel

The classes are java.sql.Connectionand java.sql.Statement. You are currently only importing javax.sql.*, so it cannot find these classes.

类是java.sql.Connectionjava.sql.Statement。您目前仅导入javax.sql.*,因此无法找到这些类。

If you want to use classes from a package other than the current package or java.lang, you need to import the classes (eg import java.sql.Connectionand import java.sql.Statement). Any IDE would have suggested this to you.

如果要使用当前包或 之外的包中java.lang的类,则需要导入类(例如import java.sql.Connectionimport java.sql.Statement)。任何 IDE 都会向您建议这个。

回答by Vishal

import this two packages:-

导入这两个包:-

import java.sql.SQLException; import java.sql.Statement;

导入 java.sql.SQLException; 导入 java.sql.Statement;

The reason it shows an error because the class connection is not available in javax.sql package. Thanks,

之所以显示错误,是因为类连接在 javax.sql 包中不可用。谢谢,

回答by SparkOn

If you referthe javax.sqlpackage you will find it does not contain thc Connectionand Statementclass. But in your example you are using both these classes so obviously you need to import them. So just import them from java.sqlpackage. You can do so by adding these import statements java.sql.Connectionand java.sql.Statement.

如果你参考这个 javax.sql包,你会发现它不包含 thcConnectionStatementclass。但是在您的示例中,您同时使用了这两个类,因此显然您需要导入它们。所以只需从java.sql包中导入它们。您可以通过添加这些导入语句 java.sql.Connectionjava.sql.Statement.

Try to handle the SQLExceptionby surrounding your code with tryand catchand close the connection in finallyblock.

尝试SQLException通过用try和包围您的代码catch并关闭finally块中的连接来处理。