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

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

问题描述

我在与Java和PostgreSQL数据库建立连接时遇到一些困难。我已下载。



但是,如果我这样做

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

connected

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



提前感谢

解决方案

驱动程序名称正确。它与驱动程序的官方文档中提到的相同。因此,驱动程序不在类路径中。



您说:


我正确设置应用程序ClassPath


另一方面,您只需调用:

  java JavaPostGreSQLConnectivity 

在这种情况下,类路径上没有PG驱动程序。您必须使用类似

手动添加它。

  java -cp postgresql-jdbc4.jar JavaPostGreSQLConnectivity 



EDIT 键入时问题已更改,因此重复。



您已将jar添加到IDE中。这有助于IDE编译您的代码。如果你使用IDE启动程序,IDE也将为你设置类路径。但是如果你不通过IDE启动,那么没有人知道正确的类路径,它必须手动设置。



你的选项是:




  • 始终通过IDE启动

  • 创建隐藏类路径设置的批处理脚本

  • 设置 CLASSPATH 环境变量(不与其他Java应用程序扩展)

  • 创建一个可执行jar 并设置类路径。

  • 将jar放入一个JVM自动选择的地方(例如在 lib / ext JRE的目录)。但是污染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)\Java\jdk1.7.0\bin>java -cp C:\Users\pos
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.

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

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