PostgreSQL 和 JDBC 的 ClassNotFoundException [英] ClassNotFoundException with PostgreSQL and JDBC

查看:23
本文介绍了PostgreSQL 和 JDBC 的 ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在连接 Java 和 PostgreSQL 数据库时遇到了一些困难.我已经下载了 .

但是,如果我这样做

C:Program Files (x86)Javajdk1.7.0in>java -cp C:Userspostgresql-9.2-1002.jdbc4.jar;JavaPostGreSQL 连接连接的

它有效.为什么当我已经正确地将它放在类路径中时我需要再次明确提及驱动程序?有没有其他方法(我只想将 JAR 文件放在 Classpath 中,程序应该从那里读取)?

提前致谢

解决方案

驱动名称没问题.它与驱动程序的官方文档中提到的相同.因此驱动程序不在类路径中.

你说:

<块引用>

我 [...] 正确设置了应用程序类路径

另一方面,您只需调用:

java JavaPostGreSQLConnectivity

在这种情况下,类路径上没有 PG 驱动程序.你必须使用类似的东西手动添加它

java -cp postgresql-jdbc4.jar JavaPostGreSQLConnectivity

EDIT 输入时问题已更改,因此出现重复.

您仅在 IDE 中添加了 jar.这有助于 IDE 编译您的代码.如果您使用 IDE 启动程序,则 IDE 还会为您设置类路径.但是,如果您不通过 IDE 启动,那么没有人知道正确的类路径,必须手动设置.

您的选择是:

  • 始终通过 IDE 启动
  • 制作一些隐藏类路径设置的批处理脚本(常见解决方案)
  • 设置 CLASSPATH 环境变量(不与其他 Java 应用程序一起扩展)
  • 制作一个可执行 Jar"并在那里设置类路径.(使用该词搜索本网站).
  • 将 jar 放到 JVM 自动提取的位置(例如,在 JRE 的 lib/ext 目录中).但污染 JRE/JDK 库是最糟糕的选择.

注意:以上都是Java基础知识,与PostgreSQL无关.

I am having some difficulty in making connectivity with Java and PostgreSQL Database.I have download the JDBC4 Postgresql Driver, Version 9.2-1002 driver and properly set the application ClassPath. My code is as 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();
        }
    }
};

Upon running I am getting the below error

Is it complaining about

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

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

However, If I do

C:Program Files (x86)Javajdk1.7.0in>java -cp C:Userspos
tgresql-9.2-1002.jdbc4.jar; JavaPostGreSQLConnectivity

connected

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

Thanks in advance

解决方案

The driver name is OK. It is the same as mentioned in the official docs of the driver. Therefore the driver is just not in the classpath.

You say:

I [...] properly set the application ClassPath

On the other hand you start the program by just calling:

java JavaPostGreSQLConnectivity

In that case no PG driver is on the classpath. You have to add it by hand using someting like

java -cp postgresql-jdbc4.jar JavaPostGreSQLConnectivity

EDIT The question has been changed while typing, hence the duplication.

You added the jar only in you IDE. This helps the IDE to compile your code. If you start the program using you IDE then the IDE will also set the classpath for you. But if you don't start via the IDE then nobody knows the correct classpath and it has to be set by hand.

Your options are:

  • start always via IDE
  • make some batch script which hides the setting of the classpath (common solution)
  • set the CLASSPATH environment variable (does not scale with other Java applications)
  • make an "Executable Jar" and set the classpath there. (Search this site using that term).
  • put the jar into a place where the JVM picks it up automatically (e.g. in the lib/ext directory of the JRE). But polluting the JRE/JDK libs is the worst option.

Note: This is all basic Java knowledge and has nothing to do with PostgreSQL.

这篇关于PostgreSQL 和 JDBC 的 ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆