eclipse 编译错误 - com.mysql.jdbc.Driver 无法解析为变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13670536/
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-09-19 19:32:06  来源:igfitidea点击:

Compilation error - com.mysql.jdbc.Driver cannot be resolved to a variable

javaeclipsejdbc

提问by rpat

I am barely familiar with pre-bundled STS. I am trying out JDBC for MySQL in Eclipse/Spring on Fedora 17

我对预捆绑的 STS 几乎不熟悉。我正在 Fedora 17 上的 Eclipse/Spring 中试用 MySQL 的 JDBC

Downloaded the mysql JDBC drivers (mysql-connector-java-5.1.22-bin.jar). Then in the IDE

下载了 mysql JDBC 驱动程序 (mysql-connector-java-5.1.22-bin.jar)。然后在IDE中

  1. Created a project and named it JDBC
  2. Created a folder called 'lib' under the project
  3. Went to Project > Properties . Selected the Java Build Path, then selected the 'Libraries' tab. clicked on 'Add Jars' Selected JDBC > lib > mysql---.jar and clicked OK. This added under the Project Explorer pane a 'Referenced Libraries', under which I see the mysql jar file.
  4. Wrote the following code under the project
  1. 创建了一个项目并将其命名为JDBC
  2. 在项目下创建了一个名为“lib”的文件夹
  3. 转到 Project > Properties 。选择 Java 构建路径,然后选择“库”选项卡。单击“添加罐子”选定的 JDBC > lib > mysql---.jar 并单击确定。这在项目资源管理器窗格下添加了一个“参考库”,我在其中看到了 mysql jar 文件。
  4. 在项目下写了如下代码

I kind of thought I had done what was needed to compile. However, I am seeing

我有点以为我已经完成了编译所需的工作。然而,我看到

Class.forName(com.mysql.jdbc.Driver);

Class.forName(com.mysql.jdbc.Driver);

com.mysql.jdbc.Driver cannot be resolved to a variable

com.mysql.jdbc.Driver 无法解析为变量

Can you tell me what's wrong here.

你能告诉我这里有什么问题吗?

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;


    public class TestJDBC {

        /**
         * @param args
         */
        public static void main(String[] args)  throws Exception{
            // TODO Auto-generated method stub
            Connection connection = null;
            Statement statement = null;
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            try {
                Class.forName(com.mysql.jdbc.Driver);
                connection = DriverManager.getConnection("jdbc:mysql://localhost/testdb?" + 
                             "user=myuser&password=mypwd");
                if (connection != null) {
                    System.out.println ("Connected may be?");
                    connection.close();
                }
                else {
                    System.out.println ("Not connected?");
                }
            }
            catch (Exception e) {
                connection.close();
            }
        }
    }

采纳答案by mazaneicha

Class.forName("com.mysql.jdbc.Driver");

or

或者

Class.forName(com.mysql.jdbc.Driver.class.getName());

will work better. (The 2nd one compiles but doesn't really make sense cause it assumes that class is already loaded :)

会工作得更好。(第二个编译但实际上没有意义,因为它假设类已经加载:)

回答by Chris Gerken

You want to use:

你想使用:

Class.forName("com.mysql.jdbc.Driver");

In the future, note that the compiler was telling you what it was expecting: a variable. You just have to figure out how to modify or convert what you have into what it wants. In this case, anything that converts to a value (a constant, a variable or a method returning a value) can be used.

将来,请注意编译器会告诉您它期望的是什么:一个变量。你只需要弄清楚如何修改或转换你拥有的东西。在这种情况下,可以使用任何转换为​​值的东西(常量、变量或返回值的方法)。