如何使用Maven在Java中添加PostgreSQL?

发布于 2025-02-12 16:50:29 字数 6933 浏览 0 评论 0原文

我想在使用Maven的Java中使用PostgreSQL进行一个应用程序。当添加到依赖关系后Ql时,我无法使用SQL的类时出现问题,尽管在pom.xml中是依赖性。

这是pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.hah</groupId>
    <artifactId>EjemploSimplePersonas</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>13</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>13</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.4.0</version>
            <type>xml</type>
            <classifier>features</classifier>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.4</version>
                <configuration>
                    <mainClass>com.hah.ejemplosimplepersonas.Ejecutable</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <!-- Default configuration for running -->
                        <!-- Usage: mvn clean javafx:run -->
                        <id>default-cli</id>
                    </execution>
                    <execution>
                        <!-- Configuration for manual attach debugging -->
                        <!-- Usage: mvn clean javafx:run@debug -->
                        <id>debug</id>
                        <configuration>
                            <options>
                                <option>-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=localhost:8000</option>
                            </options>
                        </configuration>
                    </execution>
                    <execution>
                        <!-- Configuration for automatic IDE debugging -->
                        <id>ide-debug</id>
                        <configuration>
                            <options>
                                <option>-agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address}</option>
                            </options>
                        </configuration>
                    </execution>
                    <execution>
                        <!-- Configuration for automatic IDE profiling -->
                        <id>ide-profile</id>
                        <configuration>
                            <options>
                <option>${profiler.jvmargs.arg1}</option>
                <option>${profiler.jvmargs.arg2}</option>
                <option>${profiler.jvmargs.arg3}</option>
                <option>${profiler.jvmargs.arg4}</option>
                <option>${profiler.jvmargs.arg5}</option>
                            </options>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            
            <!-- Copia las dependencias librerias javafx .jar a la carpeta "lib" de tu proyecto -->
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <configuration>
        <outputDirectory>${project.build.directory}/lib</outputDirectory>
        <excludeTransitive>false</excludeTransitive> 
        <stripVersion>false</stripVersion>
    </configuration>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
        </execution>
    </executions>
    </plugin>
    
    <!-- Agrega la referencia del contenido de la carpeta "lib" en el classpath de tu proyecto-->
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>com.hah.ejemplosimplepersonas.Ejecutable</mainClass>
            </manifest>
        </archive>
    </configuration>
    </plugin>
        </plugins>
    </build>
</project>

这是问题: https://i.sstatic.net/ybpw5.jpg

注:我尝试使用Postgreslsl作为org.postgresql,当我在maven中添加依赖项时,什么都没有。

注2:我下载了版本“ poptresql:postgresql”和“ org.postgresql:postgresql”。

编辑:我使用NetBeans创建,并具有此规格: https://i.sstatic.net/esrba.jpg

陌生的事情是,当我使用简单的规范创建时,它可以很好地工作,我的意思是,这是 https://i.sstatic.net/b1cju.jpg 奇怪的是,它可以在简单规范中与任何问题一起使用,但否则不会以其他方式使用。

I want to make an application where it uses PostgreSQL in Java using Maven. The problem appears when, adding to the dependency PostgreSQL, I cannot use the SQL's classes, although in pom.xml be the dependency.

Here's the pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.hah</groupId>
    <artifactId>EjemploSimplePersonas</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>13</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>13</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.4.0</version>
            <type>xml</type>
            <classifier>features</classifier>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.4</version>
                <configuration>
                    <mainClass>com.hah.ejemplosimplepersonas.Ejecutable</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <!-- Default configuration for running -->
                        <!-- Usage: mvn clean javafx:run -->
                        <id>default-cli</id>
                    </execution>
                    <execution>
                        <!-- Configuration for manual attach debugging -->
                        <!-- Usage: mvn clean javafx:run@debug -->
                        <id>debug</id>
                        <configuration>
                            <options>
                                <option>-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=localhost:8000</option>
                            </options>
                        </configuration>
                    </execution>
                    <execution>
                        <!-- Configuration for automatic IDE debugging -->
                        <id>ide-debug</id>
                        <configuration>
                            <options>
                                <option>-agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address}</option>
                            </options>
                        </configuration>
                    </execution>
                    <execution>
                        <!-- Configuration for automatic IDE profiling -->
                        <id>ide-profile</id>
                        <configuration>
                            <options>
                <option>${profiler.jvmargs.arg1}</option>
                <option>${profiler.jvmargs.arg2}</option>
                <option>${profiler.jvmargs.arg3}</option>
                <option>${profiler.jvmargs.arg4}</option>
                <option>${profiler.jvmargs.arg5}</option>
                            </options>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            
            <!-- Copia las dependencias librerias javafx .jar a la carpeta "lib" de tu proyecto -->
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <configuration>
        <outputDirectory>${project.build.directory}/lib</outputDirectory>
        <excludeTransitive>false</excludeTransitive> 
        <stripVersion>false</stripVersion>
    </configuration>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
        </execution>
    </executions>
    </plugin>
    
    <!-- Agrega la referencia del contenido de la carpeta "lib" en el classpath de tu proyecto-->
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>com.hah.ejemplosimplepersonas.Ejecutable</mainClass>
            </manifest>
        </archive>
    </configuration>
    </plugin>
        </plugins>
    </build>
</project>

And this is the problem:
https://i.sstatic.net/YBPw5.jpg

Note: I tried to use postgresl as org.postgresql when I added dependencies in Maven, nothing worked.

Note 2: I downloaded the version "postresql:postgresql" and "org.postgresql:postgresql".

Edit: I created using NetBeans, with this specifications: https://i.sstatic.net/EsRba.jpg

The stranger thing, is that it works perfectly when I create using the simple specification, I mean, whith this: https://i.sstatic.net/b1cjU.jpg
It's strange that it works with any problem in simple specification, but no when the other way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榕城若虚 2025-02-19 16:50:29

我认为我能够根据对我的解释来解决它。
一旦下载了PostgreSQL依赖项,我们就可以在module-info.java类中添加上述库,因此可以使用从maven中的依赖项下载的库。
可能会感到困惑,因为习惯使用Java中的SQL类是很困惑的,但是由于您使用的是Maven,因此您必须从那里导入它们(这就是为什么显示路径很重要):
路径

,这就是代码在module-info.java :

module com.proyecto.zzzeliminar {
    requires javafx.controls;
    requires javafx.fxml;
    requires org.postgresql.jdbc;
    opens com.proyecto.zzzeliminar to javafx.fxml;
    exports com.proyecto.zzzeliminar;
}

这样,我们可以使用类与数据库连接和交互。
photo

I think I was able to solve it based on what has been explained to me.
Once the postgresql dependency has been downloaded, we proceed to add said library in the module-info.java class, so that in this way the libraries downloaded from the dependencies in Maven can be used.
It is possible to get confused because it is customary to use SQL classes from Java, but since you are using Maven, you have to import them from there (that is why it is important to show the Path):
Path

And this is how the code looked in module-info.java:

module com.proyecto.zzzeliminar {
    requires javafx.controls;
    requires javafx.fxml;
    requires org.postgresql.jdbc;
    opens com.proyecto.zzzeliminar to javafx.fxml;
    exports com.proyecto.zzzeliminar;
}

With that we can use the classes to connect and interact with the data base.
photo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文