F
F
fife2021-03-28 21:02:32
PostgreSQL
fife, 2021-03-28 21:02:32

When adding JDBC dependencies to maven, the version is red. How to decide?

Good evening, I decided to study Sql in practice along with the code. It does not come out to start work due to an error that it cannot find the driver.
I also can not understand the reason why in maven when adding a dependency, the version is highlighted in red.
I created the database through PGadmin, so it definitely exists.

Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432/intellijdb
  at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
  at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
  at Main.main(Main.java:10)


<?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>org.example</groupId>
    <artifactId>maven jdbc</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.1</version>
        </dependency>
    </dependencies>
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
</project>


import java.sql.*;

public class Main {
    public static void main(String[] args) throws SQLException {
        final String user = "postgres";
        final String pass = "admin";
        final String url = "jdbc:postgresql://localhost:5432/intellijdb";

        final Connection connection = DriverManager.getConnection(url,user,pass);

        try(PreparedStatement statement = connection.prepareStatement("SELECT * from Phones WHERE id = (?)")){
            statement.setInt(1,1);
            final ResultSet resultSet = statement.executeQuery();
            if(resultSet.next()){
                String name = resultSet.getString("UserLogin");
                System.out.println(name);
            }
        }finally {
            connection.close();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2021-03-28
@fife

mvn clean installIn console.
And a button like "refresh" in the Maven tab

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question