简体   繁体   English

与 Maven 的雪花连接,类路径上的 jar

[英]Snowflake connection with Maven, jar on classpath

I'm currently trying to make a simple connection to our Snowflake database.我目前正在尝试与我们的 Snowflake 数据库建立简单的连接。 I followed the documentation on their site: https://docs.snowflake.net/manuals/user-guide/jdbc-configure.html and verified that the connection string is correct.我遵循了他们网站上的文档: https : //docs.snowflake.net/manuals/user-guide/jdbc-configure.html并验证了连接字符串是否正确。

Everytime I launch the program, however, I get an SQLException stating it can't find a jar, or that there is no driver found:但是,每次启动该程序时,都会收到一个 SQLException,指出它找不到 jar,或者找不到驱动程序:

[19:28:54] [Server thread/WARN]: driver not found
[19:28:54] [Server thread/WARN]: java.sql.SQLException: No suitable driver found for jdbc:snowflake://d9022.east-us-2.azure.snowflakecomputing.com/
[19:28:54] [Server thread/WARN]:        at java.sql.DriverManager.getConnection(Unknown Source)
[19:28:54] [Server thread/WARN]:        at java.sql.DriverManager.getConnection(Unknown Source)
[19:28:54] [Server thread/WARN]:        at com.mcnations.nationsatwar.jdbc.DatabaseManager.getConnection(DatabaseManager.java:44)
[19:28:54] [Server thread/WARN]:        at com.mcnations.nationsatwar.jdbc.DatabaseManager.init(DatabaseManager.java:50)
[19:28:54] [Server thread/WARN]:        at com.mcnations.nationsatwar.jdbc.DatabaseManager.<init>(DatabaseManager.java:19)
[19:28:54] [Server thread/WARN]:        at net.mcnations.nationsatwar.Player.NationPlayer.<init>(NationPlayer.java:54)
[19:28:54] [Server thread/WARN]:        at net.mcnations.nationsatwar.NationsInitializer.playerLogin(NationsInitializer.java:78)

I use Maven for my dependencies.我使用 Maven 作为我的依赖项。 In my POM, I simply have:在我的 POM 中,我只有:

<!-- https://mvnrepository.com/artifact/net.snowflake/snowflake-jdbc -->
        <dependency>
            <groupId>net.snowflake</groupId>
            <artifactId>snowflake-jdbc</artifactId>
            <version>3.9.2</version>
        </dependency>

Even with the Maven dependency stated (in accordance with Snowflake's documentation) I still got the no driver exception.即使声明了 Maven 依赖项(根据 Snowflake 的文档),我仍然没有驱动程序异常。 I then added the jar into my build path as well hoping that (maybe) you needed both a maven dependency and an actual .jar file on your build path.然后我将 jar 添加到我的构建路径中,并希望(也许)您在构建路径上同时需要一个 Maven 依赖项和一个实际的 .jar 文件。 This did not fix the problem.这并没有解决问题。

At this point I'm at a loss.在这一点上,我不知所措。 I do not know what I am doing wrong, nor do I know how to address it.我不知道自己做错了什么,也不知道如何解决。

My connection class:我的连接类:

package com.mcnations.nationsatwar.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import net.mcnations.nationsatwar.Player.NationPlayer;

public class DatabaseManager {

    private NationPlayer player;

    public DatabaseManager(NationPlayer player) throws SQLException{
        this.player = player;

        init();

    }

    private static Connection getConnection() throws SQLException{


        try {

            Class.forName("net.snowflake.client.jdbc.SnowflakeDriver");

        }catch(ClassNotFoundException ex) {

            System.err.println("driver not found");
        }

        Properties properties = new Properties();

        properties.put("user", "NationsUser");
        properties.put("password", "//myPassword");
        properties.put("db", "//myDB");
        properties.put("role", "SYSADMIN");

        String connectStr = "jdbc:snowflake://9022.east-us-2.azure.snowflakecomputing.com/";

        return DriverManager.getConnection(connectStr, properties);

    }

    private static void init() throws SQLException{

        Connection connObject = getConnection();

        Statement statement = connObject.createStatement();

        ResultSet rSet = statement.executeQuery("SELECT * FROM PlayerData");

        if(rSet == null) {
            System.out.println("rSet is null");
        }
        else {
            System.out.println(rSet.next());
        }

    }

}

As I understand, you do not deploy the required JAR (snowflake-jdbc-3.9.2.jar) with your application, so it can not find the driver in classpath.据我了解,您没有在您的应用程序中部署所需的 JAR (snowflake-jdbc-3.9.2.jar),因此它无法在类路径中找到驱动程序。 Here's similar output when I try to run my application without correct classpath:当我尝试在没有正确类路径的情况下运行我的应用程序时,有类似的输出:

java -cp snowflaketest01-1.0-SNAPSHOT.jar com.gokhanatil.snowflaketest01.Main
driver not found
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:snowflake://xxxxxxxxxx.snowflakecomputing.com/
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:189)
    at com.gokhanatil.snowflaketest01.Main.getConnection(Main.java:28)
    at com.gokhanatil.snowflaketest01.Main.main(Main.java:36)

You may include the JAR by yourself, or you can let Maven pack required the JARs to your final jar.您可以自己包含 JAR,也可以让 Maven 将所需的 JAR 打包到您的最终 jar 中。 You may include the following to your pom file:您可以在 pom 文件中包含以下内容:

...
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
    </plugin>
...

So it produces one big JAR file which includes all required JAR (snowflake driver etc):所以它会生成一个大的 JAR 文件,其中包含所有必需的 JAR(雪花驱动程序等):

java -cp snowflaketest01-1.0-SNAPSHOT-jar-with-dependencies.jar com.gokhanatil.snowflaketest01.Main

Please check java classpath: https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html请检查java类路径: https : //docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html

and including the dependencies in a JAR with Maven: Including dependencies in a jar with Maven并将依赖项包含在带有 Maven 的 JAR 中: Include dependencies in a jar with Maven

The error "java.sql.SQLException: No suitable driver found for java.sql.SQLException: No suitable driver found for jdbc:snowflake://.snowflakecommputing.com" occurs when you try to connect Snowflake database using Java program but either you don't have Snowflake JDBC driver {ex .当您尝试使用 Java 程序连接 Snowflake 数据库时,会出现错误“java.sql.SQLException:找不到适合 java.sql.SQLException 的驱动程序:找不到适合 jdbc:snowflake://.snowflakecommputing.com 的驱动程序”没有 Snowflake JDBC 驱动程序 {ex . snowflake-jdbc-3.11.0.jar} in your classpath or driver is not registered before calling the getConnection() method.在调用 getConnection() 方法之前,您的类路径或驱动程序中的 snowflake-jdbc-3.11.0.jar} 未注册。

Could you please make sure you have included the Snowflake JDBC Driver "snowflake-jdbc-3.11.0.jar" into your Build library path.请确保您已将 Snowflake JDBC 驱动程序“snowflake-jdbc-3.11.0.jar”包含到您的构建库路径中。

This error is caused by the application not being able to find the Snowflake jdbc driver.此错误是由应用程序无法找到 Snowflake jdbc 驱动程序引起的。 This can happen when jar files have required parameters (ie. config.yml).当 jar 文件具有必需的参数(即 config.yml)时,就会发生这种情况。

Because the Snowflake JDBC driver does not have these required files, I told the main class to launch both itself, and the Snowflake JDBC jar file together via:因为 Snowflake JDBC 驱动程序没有这些必需的文件,所以我告诉主类通过以下方式同时启动它本身和 Snowflake JDBC jar 文件:

@echo off
title Server Console
java -classpath "spigot-1.15.1.jar;lib/*" org.bukkit.craftbukkit.Main
PAUSE

Where the line java -classpath "spigot-1.15.1.jar;lib/*" org.bukkit.craftbukkit.Main statesjava -classpath "spigot-1.15.1.jar;lib/*" org.bukkit.craftbukkit.Main状态

  1. Start a java application启动一个 Java 应用程序
  2. Down the classpath of "my_main_jar.jar;"在“my_main_jar.jar;”的类路径下
  3. launch with all files in the /lib directory使用 /lib 目录中的所有文件启动
  4. and point to the main class (in my case, org.bukkit.craftbukkit.Main)并指向主类(在我的例子中,org.bukkit.craftbukkit.Main)

These are command-line arguments that are essentially "handed off" to the application.这些是基本上“传递”给应用程序的命令行参数。

I am using Eclipse and i never faced any such issue .
Create a Maven Project , put dependencies and you are good .

File->New->Project->Maven Project .Give a name to the project ->Finish .

Now add the below dependency.

    <dependency>
            <groupId>net.snowflake</groupId>
            <artifactId>snowflake-jdbc</artifactId>
            <version>3.9.0</version>
        </dependency>

Can you check in your ~/.m2 folder if you are getting the jar or not ?如果你得到 jar,你能检查你的 ~/.m2 文件夹吗?

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

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