Hello. There is a servlet in which I try to connect to the DB. Here is the code.

try { this.getClass().getClassLoader().loadClass("org.postgresql.Driver");} catch (ClassNotFoundException e) {e.printStackTrace();} try { connection = DriverManager.getConnection("jdbc: postgresql://127.0.0.1:5432 ", "postgres","923506"); } catch (SQLException e) {e.printStackTrace();} 

Gives an error message

java.sql.SQLException: No suitable driver found for jdbc: postgresql: //127.0.0.1: 5432

A little background and description of the current settings.

  • A bunch of the current version of the database and jdbc driver works on ordinary (not web applications).
  • The jar jdbc driver file is in% CATALINA_HOME% / lib. In the project, this file is specified in this directory (before that, it was lying elsewhere and the error occurred even at the time the class was loaded, then an error was issued at the time of connecting to the database).

Attention, the question: What is wrong here?

  • So, more precisely. I went jdbc.postgresql.org/download.html and downloaded 8.3-607 JDBC 2EE poyuzal, all the same - alex91
  • Additive: found tomcat - jdbc driver in libs in% CATALINA_HOME% Same problem) - alex91
  • Is the problem still relevant? :) Your code does not see the driver, that is, loadClass ("org.postgresql.Driver") does not work, this is mainly due to the fact that the jar file is not added to the project classpath - sonniy
  • Actual) I decided in this way, I made a separate file for working with the database, not a very crutch method, so I didn’t search why I didn’t connect their servlet directly :) By the way, it seemed to add everything. like it - alex91

2 answers 2

It seems that he doesn’t like the connection :

 connection = DriverManager.getConnection("jdbc: postgresql://127.0.0.1:5432 ", "postgres","923506"); 

The driver description includes such variations:

Try this:

 connection = DriverManager.getConnection("jdbc: postgresql://127.0.0.1:5432/DataBaseName", "postgres","923506"); 

    You can of course "head-on" as you do, but you can also be more elegant: through the JNDI DataSource in the Tomcat configuration.

    This is a more systematic, safer and more correct solution and, by the way, with this method, you will immediately get another JDBC connection pool

    Example of connecting to Postgres

    Well, if you want to continue to "head on," then the recipe for the solution is known and already described.