加载postgreSQL JDBC驱动程序 [英] Loading the postgreSQL JDBC driver

查看:239
本文介绍了加载postgreSQL JDBC驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Java程序加载JDBC postgreSQL驱动程序。我知道这是在互联网上。我尝试了很多解决方案,但没有一个能为我工作。

I am trying to load a JDBC postgreSQL driver for a Java program. I know this is all over the Internet. I have tried many solutions, but none of them have worked for me.

问题是我收到了这个错误:

The problem is that I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError:    
classes/com/freire/test/JDBCExample/class
Caused by: java.lang.ClassNotFoundException: classes.com.freire.test.JDBCExample.class
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

我的代码如下:

package com.freire.test;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class JDBCExample 
{
    public static void main(String[] argv) 
    {
        System.out.println("JDBC Connection Testing");
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("PostgreSQL JDBC Driver not included!");
        }
    }
}

我项目的结构看起来像这样:

And the structure of my project looks like this:

myProject
 src
   com
     freire
       test
         JDBCExample.java
 classes
   com
     freire
       test
         JDBCExample.class
 lib
   postgresql-9.2-1003.jdbc3.jar

编译工作正常:

java -d classes/ src/com/freire/test/JDBCExample.java

但是执行会产生错误:

java classes/com/freire/test/JDBCExample

值得一提的是我正在开发OS X Mountain Lion。

Worth to say that I am working on a OS X Mountain Lion.

任何帮助将不胜感激。

推荐答案

首先,您需要使用提及包名称。而不是 /

Firstly you need to mention the package names using . instead of / while running the java program:

转到你的cla sses目录并以下列方式运行JDBCExample:

Go to your classes directory and run JDBCExample as :

java com.freire.test.JDBCExample

但是现在它会因为没有找到postgres驱动程序类而哭泣,因为类路径中缺少postgres jar。所以你需要在运行时使用classpath选项程序并将你的postgres jar添加到类路径:

But it will now cry for the postgres driver class not found because postgres jar is missing in the classpath.So you need to use the classpath option while running the program and add your postgres jar to the classpath:

for windows:

for windows:

java -cp .;../lib/postgresql-9.2-1003.jdbc3.jar com.freire.test.JDBCExample

for linux:

for linux:

java -cp .:../lib/postgresql-9.2-1003.jdbc3.jar com.freire.test.JDBCExample

这篇关于加载postgreSQL JDBC驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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