2

I am developing a GIS project with the help of Geo Tool on maven. In that project I need to connect a PostgreSQL database to my maven project. If I connect a normal java project to PostgreSQL in NetBeans then there is no problem because I am adding a jar file of jdbc-postgresql driver to the library of the project. But in case of maven project there is no library folder in which I could add the jdbc-postgresql driver, and the dependency folder of the maven project does not allow to add any jar or any library in it.

How we can use postgresql database in a maven project?

3
  • You need a maven dependency containing the postgresql driver. You should not manually work with jar files when doing maven projects. Commented Jul 22, 2016 at 13:44
  • Share your pom.xml file you using Commented Jul 22, 2016 at 13:44
  • try this <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.3-1102-jdbc41</version> </dependency> Commented Jul 22, 2016 at 13:47

1 Answer 1

1

Unlike a normal java project that you copy or just throw the jar of the driver in the lib file, in Maven you use dependecies for that.

When you create a new Maven project you're going to notice there's a file called pom.xml and that's where you will throw your dependecies.

Here's the PostgreSQL driver dependency for Maven, add this in your <dependencies></dependencies> tag:

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.0-801.jdbc4</version>

Here's a link to the page in case you want a different version: https://mvnrepository.com/artifact/postgresql/postgresql

Here's an example of how a pom.xml looks like, this is from a Spring Starter Project:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much Julian.... I have done it.... I am making a GIS maven project in Geotool so how we can display a shape file from the postgresql database in geotool JMapFroam or JMapPane. Presently I can display the shape file from the file path, but I want to display this shape file from the postgresql database. So I can run spatial queries here.
No problem man. If you could evaluate the question, please do it. I'm glad to hear that you got it done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.