简体   繁体   English

JDBC 驱动程序和终端类路径

[英]JDBC driver and terminal classpath

I'm trying to connect to a sql database made through mysql_workbench on a linux machine.我正在尝试连接到通过 mysql_workbench 在 linux 机器上创建的 sql 数据库。 I currently code through terminal using text editors because that's what I feel comfortable in.我目前使用文本编辑器通过终端进行编码,因为这让我感觉很舒服。

My DatabaseConnection class has the following code, mostly with System.out.println 's to try and find the problem cause:我的 DatabaseConnection 类具有以下代码,主要是使用System.out.println来尝试查找问题原因:

final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
final String DB_URL = "jdbc:mysql://localhost:3306/mydb";

final String user = "root";
final String pass = "pass";

Connection connection;
Statement statement;

public DatabaseConnection(){

}

public void connect(){
    System.out.println("Entering method");
    try {
        System.out.println("Attempting to load driver");
        Class.forName(JDBC_DRIVER); 
        System.out.println("Driver Loaded");        
        connection = DriverManager.getConnection(DB_URL, user, pass);
        System.out.println("Connection established");
        statement = connection.createStatement();
        System.out.println(" statement made");
        ResultSet resultSet = statement.executeQuery("show databases");
        System.out.println("statement executed");

        while(resultSet.next()){
            System.out.println(resultSet.next());
        }
        System.out.println("finished printing result set");

        statement.close();
    } catch(ClassNotFoundException ex) {
        System.out.println("Error: unable to load driver class!");
        System.exit(1);
    } catch (SQLException sqle) {
        System.out.println("Connection Error");         
        sqle.getErrorCode();
    }

}
}

I also have a Test class to test this:我还有一个Test类来测试这个:

class Test {

    public static void main(String[] args){
        DatabaseConnection connection = new DatabaseConnection();
        connection.connect();
    }
}

Currently when running it in terminal my commands are:目前在终端中运行它时,我的命令是:

javac -cp .:/lib/mysql-connector-java-5.1.38-bin.jar Test.java

(In my top folder containing the .java files, I have a lib subfolder containing my jdbc driver) (在包含 .java 文件的顶级文件夹中,我有一个包含 jdbc 驱动程序的 lib 子文件夹)

That compiles fine.那编译得很好。 Next I run it using接下来我运行它

java -cp .:/lib/mysql-connector-java-5.1.38-bin.jar Test

And I get the following error:我收到以下错误:

java -cp .:/lib/mysql-connector-java-5.1.38-bin.jar Test
Entering method
Attempting to load driver
Error: unable to load driver class!

So it looks to be it isn't loading the driver in the Class.forName() method and is failing.所以看起来它没有在Class.forName()方法中加载驱动程序并且失败了。 Could somebody possibly help with explaining why and possible solutions?有人可以帮助解释原因和可能的解决方案吗?

In mysql_workbench, I think the database is up and running since I am able to query it in the workbench query command line to show the databases and tables, which are currently empty.在 mysql_workbench 中,我认为数据库已启动并正在运行,因为我可以在工作台查询命令行中查询它以显示当前为空的数据库和表。

Java was unable to find the JAR file for the MySQL JDBC driver because the -cp argument in Java是无法找到MySQL JDBC驱动程序的JAR文件,因为-cp论点

java -cp .:/lib/mysql-connector-java-5.1.38-bin.jar Test

specifies two (colon-separated) locations:指定两个(以冒号分隔的)位置:

  1. .
  2. /lib/mysql-connector-java-5.1.38-bin.jar

The second location is interpreted as an absolute path .第二个位置被解释为绝对路径 To have it interpreted as a relative path it should start with a period ( . ), ie,要将其解释为相对路径,它应该以句点 ( . ) 开头,即,

java -cp .:./lib/mysql-connector-java-5.1.38-bin.jar Test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM