英文:
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("org.postgresql.Driver");
c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/gis");
}
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:
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.1</version>
</dependency>
</dependencies>
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
:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath> <-- don't know if this is needed -->
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
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:
<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>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论