ClassNotFoundException with PostgreSQL and JDBC

I'm having difficulty connecting to Java and PostgreSQL Database. I downloaded the JDBC4 Postgresql Driver driver, version 9.2-1002, and correctly install the ClassPath application. My code is under

import java.sql.*;

public class JavaPostGreSQLConnectivity
{
    public static void main(String[] args) 
    {
        DB db = new DB();        
        db.dbConnect("jdbc:postgresql://127.0.0.1:5432/TestDB", "postgres","pwd");
    }
}

class DB
{
    public DB() {}

    public void dbConnect(String db_connect_string, String db_userid, String db_password)
    {
        try
        { 
            Class.forName("org.postgresql.Driver");
            Connection conn = DriverManager.getConnection(db_connect_string, db_userid, db_password);
            System.out.println("connected");

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

When I start, I get the following error

enter image description here

Does he complain about

Class.forName ("org.postgresql.Driver");

If so, what will be the name of the driver? However, I followed this for my learning purpose.

However, if I do

C:\Program Files (x86)\Java\jdk1.7.0\bin>java -cp C:\Users\pos
tgresql-9.2-1002.jdbc4.jar; JavaPostGreSQLConnectivity

connected

It works. Why do I need to explicitly specify the driver again when I have correctly placed it in the classpath? Is there an alternative way (I just want to put the JAR file in the Classpath and the program should read from there)?

Thank you in advance

0
1

. , . .

:

I [...] ClassPath

, , :

java JavaPostGreSQLConnectivity

PG . ,

java -cp postgresql-jdbc4.jar JavaPostGreSQLConnectivity

, , .

IDE. IDE . , IDE, IDE . IDE, , .

:

  • IDE
  • script, ( )
  • CLASSPATH ( Java)
  • " " . ( ).
  • , JVM (, lib/ext JRE). JRE/JDK libs.

. Java PostgreSQL.

+2

All Articles