No suitable driver found for jdbc:postgresql://localhost:5432/gis

huangapple go评论122阅读模式
英文:

No suitable driver found for jdbc:postgresql://localhost:5432/gis

问题

我已经花了最近的3个小时尝试让我的Java程序与Postgres服务器进行接口交互。我无法解决错误信息"找不到适合的驱动程序以用于jdbc:postgresql://localhost:5432/gis"。这是一个Bukkit插件,我正在使用IntelliJ IDEA。

代码:

try
{
	//Class.forName("org.postgresql.Driver");
	c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/gis");
}
catch(Exception e)
{
	getLogger().info(e.getMessage());
}

我尝试过的事项:

  • 使用java -cp ./path/to/postgresjdbc.jar -jar spigot-1.15.2.jar

  • 将jdbc文件内部直接添加到我的jar文件中

  • 将jdbc文件作为IntelliJ项目中的依赖项添加

  • 切换到Maven,并将以下内容放入pom.xml:

<dependencies>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.2.1</version>
    </dependency>
</dependencies>

我无法解决我发布的错误。在这一点上,它已经占据了我整个晚上。

英文:

I've spent the last 3 hours trying to get my Java program to interface with my Postgres server. I cannot get past the error message "No suitable driver found for jdbc:postgresql://localhost:5432/gis". It is a Bukkit plugin, and I am using IntelliJ IDEA.

The code:

try
{
	//Class.forName(&quot;org.postgresql.Driver&quot;);
	c = DriverManager.getConnection(&quot;jdbc:postgresql://localhost:5432/gis&quot;);
}
catch(Exception e)
{
		getLogger().info(e.getMessage());
}

Things I have tried:

  • java -cp ./path/to/postgresjdbc.jar -jar spigot-1.15.2.jar

  • adding the jdbc file internals directly to my jar file

  • adding the jdbc file as a dependency within the IntelliJ project

  • switching to maven, and putting the following in pom.xml:

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
            &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
            &lt;version&gt;42.2.1&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;

I am unable to get past the error I posted. At this point, it has taken over my entire evening.

答案1

得分: 0

我在开发使用MySQL数据库的Bukkit/Spigot插件时,多次遇到了这个问题。对于使用PostgreSQL的过程应该是相同的。

通常,当你的插件无法找到PostgresqlJDBC驱动程序时,会出现这个错误。你有两个解决方法:

选项1. 将.jar添加到插件的类路径:

建议将库文件设置在plugins/lib目录下,这样服务器就不会尝试将库文件作为Bukkit插件加载。通过在你的pom.xml中添加以下配置,为你的类路径添加前缀lib/

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath> <!-- 不确定是否需要这一行 -->
                <classpathPrefix>lib/</classpathPrefix>
            </manifest>
        </archive>
    </configuration>
</plugin>

然后确保将你的postgresjdbc.jar放入插件文件夹中名为lib的文件夹内。

选项2. 直接在你的jar中添加依赖:

请注意,这个选项会增加你插件的jar文件大小。

你可以通过Maven的assembly插件来完成:

<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>
        <archive>
            <manifest>
                <mainClass>your.main.class</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

如果你使用类似于7-zip这样的文件压缩工具打开插件的jar文件,除了插件本身的类,你应该能看到驱动程序的类。

如果这解决了你的问题,请告诉我。

英文:

I've stumbled with this issue several times when developing Bukkit/Spigot plugins that make use of MySQL databases. The process for Postgres should be the same.

Usually, this error happens when your plugin can't find the PostgresqlJDBC driver. You have two workarounds:

Option 1. Adding the .jar to the plugin's classpath:

It's recommended that you set the libraries inside plugins/lib as then your server won't try to load the libraries as a Bukkit plugin. Add the prefix lib/ to your classpath by adding this configuration in your pom.xml:

&lt;plugin&gt;
    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
    &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
    &lt;configuration&gt;
        &lt;archive&gt;
            &lt;manifest&gt;
                &lt;addClasspath&gt;true&lt;/addClasspath&gt; &lt;-- don&#39;t know if this is needed --&gt;
                &lt;classpathPrefix&gt;lib/&lt;/classpathPrefix&gt;
            &lt;/manifest&gt;
        &lt;/archive&gt;
    &lt;/configuration&gt;
&lt;/plugin&gt;

Then make sure to put your postgresjdbc.jar inside a folder called lib inside your plugin's folder.

Option 2. Add dependencies directly in your jar:

Note that this option will increase your plugin's jar size.

This can be done via Maven's assembly plugin:

&lt;plugin&gt;
	&lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
	&lt;executions&gt;
		&lt;execution&gt;
			&lt;phase&gt;package&lt;/phase&gt;
			&lt;goals&gt;
				&lt;goal&gt;single&lt;/goal&gt;
			&lt;/goals&gt;
		&lt;/execution&gt;
	&lt;/executions&gt;
	&lt;configuration&gt;
		&lt;descriptorRefs&gt;
			&lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;
		&lt;/descriptorRefs&gt;
		&lt;archive&gt;
			&lt;manifest&gt;
				&lt;mainClass&gt;your.main.class&lt;/mainClass&gt;
			&lt;/manifest&gt;
		&lt;/archive&gt;
	&lt;/configuration&gt;
&lt;/plugin&gt;

If you open your plugin's jar with a file compressor like 7-zip you should there should the driver's classes in it apart from your plugin ones.

Let me know if this solved your issue.

huangapple
  • 本文由 发表于 2020年4月4日 08:15:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61022244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定