【问题标题】:JDBC cannot be resolved to a variable in EclipseJDBC 无法解析为 Eclipse 中的变量
【发布时间】:2015-04-29 02:55:33
【问题描述】:

目前正在尝试使用作为作业的一部分提供给我的sqlite-dbc4-3.8.2-SNAPSHOT.jar。我尝试运行我的主文件,但出现以下错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    org.sqlite.JDBC cannot be resolved to a variable

    at DbBasic.open(DbBasic.java:54)
    at DbBasic.<init>(DbBasic.java:67)
    at DbUser.<init>(DbUser.java:40)
    at Main.go(Main.java:12)
    at Main.main(Main.java:65)

这是DbBasic 类的一部分,它尝试使用 JDBC 连接和打开数据库:

  private Connection getConnection()
  // get the connection
  {
    Connection con = null;
    try {

        con = DriverManager.getConnection(SQLITE_DATABASE_LOCATION+dbName);
        con.setAutoCommit(false);
    }
    catch (SQLException sqle)
    {
      notify("Db.getConnection database location ["+SQLITE_DATABASE_LOCATION+"] db name["+dbName+"]", sqle);
    };
    return con;
  } // end of method "getConnection"

  private void open()
  // "open" the database : actually really setting up the connection and obtaining the metadata about the server
    // makes sure that database file is present before trying to establish connection
    // otherwise SQLite will create a new, empty database with the name provided
  {
    File dbf = new File(dbName);
    if (dbf.exists() == false)
    {
        System.out.println("SQLite database file ["+dbName+"] does not exist");
        System.exit(0);
    };

    try {

        Class.forName(org.sqlite.JDBC);
        con  = getConnection();
    } catch ( ClassNotFoundException cnfe ) {
      notify("Db.Open",cnfe);
    };
    if (debug) System.out.println("Db.Open : leaving");
  } // end of constructor "Open"

我已经尝试添加外部 JAR,然后将 .jar 文件添加到我在 Eclipse 中的“引用库”中。

我无法理解 Class.forName(org.sqlite.JDBC) 以及如何使其与我的 .jar 文件一起使用

【问题讨论】:

  • 你是如何构建你的项目的?您需要使用 maven 或 ant 构建来使用您的依赖项设置您的项目。
  • 我已将 Eclipse 设置为“自动构建”
  • 这不是我在 maven build 上阅读的问题的答案。您需要使用它来构建您的项目。
  • Class.forName() 接受一个字符串参数,试试 Class.forName("org.sqlite.JDBC"); (带引号)

标签: java eclipse sqlite jdbc classpath


【解决方案1】:

虽然帖子相对较旧,并且 OP 现在可能不再对这个问题的解决方案感兴趣,但还是想把它作为答案。它可能会帮助遇到这种愚蠢问题的人。这是人们在使用 Eclipse 作为 IDE 时常犯的错误之一,尝试运行甚至无法编译的代码。

您可以检查 Eclipse 中的“问题”视图并修复编译错误,然后尝试编译您的程序。这个问题的明显错误是使用驱动程序名称时缺少双引号""

Class.forName("org.sqlite.JDBC");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    • 2014-12-01
    • 2014-08-09
    相关资源
    最近更新 更多