Postgres:没有为jdbc找到合适的驱动程序

Abh*_*ngh 5 java postgresql jdbc classpath eclipse-classpath

我知道这可能是这里发布的问题的重复.对不起.这是我为数据库连接编写的代码

 try{
            Class.forName("org.postgresql.Driver");     
       }

       catch(ClassNotFoundException e)
       {
          e.printStackTrace();
       }

       try{
           String URL = "jdbc:posgresql://localhost:5432/postgres";
           String USER = "postgres";
           String PASS = "postgres";
           Connection conn = DriverManager.getConnection(URL, USER, PASS);
           Statement st = conn.createStatement();
           ResultSet rs = st.executeQuery("Select * from employee");
           while(rs.next()){
               System.out.println(rs.getString(1));
           }

       }

       catch(SQLException es){
           es.printStackTrace();
       }
Run Code Online (Sandbox Code Playgroud)

运行此代码时,我得到以下异常:

java.sql.SQLException: No suitable driver found for jdbc:posgresql://localhost:5432/postgres
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at ManageEmployee.main(ManageEmployee.java:60)
Run Code Online (Sandbox Code Playgroud)

我在eclipse中构建了这个项目并添加了外部jar的postgres

如果它可能有帮助,这是.classpath文件

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
        <attributes>
            <attribute name="owner.project.facets" value="java"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
    <classpathentry kind="lib" path="//10.202.6.95/kavitha_share/jars/postgresql-8.2-504.jdbc2ee.jar"/>
    <classpathentry kind="output" path="build/classes"/>
</classpath>
Run Code Online (Sandbox Code Playgroud)

可能的原因是因为我已经添加了罐子?

ihs*_*cak 18

更改posgresqlpostgresql.

jdbc:posgresql://localhost:5432/postgres
Run Code Online (Sandbox Code Playgroud)

变为:

jdbc:postgresql://localhost:5432/postgres
Run Code Online (Sandbox Code Playgroud)

请注意pos t gresql中的字符' t '

  • 这些灵活的可插拔API的缺点 - 他们永远不会说"哎呀,你做了一个错字" (15认同)