I
I
iAlex1952021-04-29 19:34:13
Java
iAlex195, 2021-04-29 19:34:13

Why doesn't JDBC 4.2 work without Class.forName?

I work in Eclipse. I add dependency to pom.xml PostgreSQL JDBC Driver.

<?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>

  <name>Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>14</maven.compiler.source>
    <maven.compiler.target>14</maven.compiler.target>
  </properties>

  <dependencies>
  <dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>42.2.20</version>
  </dependency>
  </dependencies>
</project>


When running in Eclipse, No suitable driver found for jdbc:postgresql://localhost/
crashes. In IDEA, everything works fine.

Properties connProps = new Properties();
        connProps.put("user", user);
        connProps.put("password",  password);
               try {
      Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost/", connProps);
    } catch (SQLException e) {
      e.printStackTrace();
    }


Updated pom.xml, ran clean install command, but still the same error.
When I add Class.forName("org.postgresql.Driver") everything starts working.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BorLaze, 2021-04-29
@BorLaze

Compare the launch string from Idea and Eclipse. Maybe the last one is missing something.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question