Getting the following error - No suitable driver found for jdbc:postgresql://localhost: 5432/testDBMS

前端 未结 4 1022
野趣味
野趣味 2021-01-29 13:07

I get back an error indicating java.sql.SQLException: No suitable driver found for
jdbc:postgresql://localhost: 5432/testDBMS

import java.sql.*;
impo         


        
相关标签:
4条回答
  • 2021-01-29 13:48

    Did you not load the driver in your code? Either define the jdbc.drivers property setting it to org.postgresql.Driver or add Class.forName("org.postgresql.Driver") to your code.

    0 讨论(0)
  • 2021-01-29 14:07

    The syntax to compile was - java -cp .;"C:\Program Files\PostgreSQL\9.3\lib\postgresql-9.3-1102.jdbc4.jar" JdbcPostgresqlConnection. Note 2 things; the quotes around the jar specification and the jar files cannot be in a folder with a space in the name. This is normally not the case on *nix system, but is often encountered in Windows systems. Note too, that when I put the jar file in the same folder with the java program I could eliminate the double quotes - java -cp .;C:\AZ_Fantasy5\postgresql-9.3-1102.jdbc4.jar JdbcPostgresqlConnection. Special thanks to JB Nizet for pointing out this situation.

    0 讨论(0)
  • 2021-01-29 14:08
    As the error says java -cp . JdbcPostgresqlConnection java.sql.SQLException: 
    No suitable driver found for jdbc:postgresql://localhost: 5432/testDBMS
    

    Which means you didnot include the postgresql.jar in your classpath

    Try executing like this and I'm assuming it is Windows OS by seeing your error

    java -cp .;pathOfYourDriverjar/postgresql.jar JdbcPostgresqlConnection
    
    0 讨论(0)
  • 2021-01-29 14:10
    java -cp . JdbcPostgresqlConnection
    

    So, clearly, the only thing that is in the classpath is the current directory (.). The postgresql driver jar is not. You need to add it to the classpath:

    java -cp .:/path/to/driver.jar JdbcPostgresqlConnection
    

    on Linux/MacOS, or

    java -cp .;c:\path\to\driver.jar JdbcPostgresqlConnection
    

    on Windows.

    0 讨论(0)
提交回复
热议问题