2

我正在尝试使用 JDBC 驱动程序连接到 Netezza 数据库并执行简单的 SELECT 查询。我的项目(使用 Eclipse)的 lib 文件夹中有 nzjdbc.jar 文件,但出现以下错误。

 java.lang.ClassNotFoundException: org.netezza.Driver
 at java.net.URLClassLoader$1.run(Unknown Source)
 at java.net.URLClassLoader$1.run(Unknown Source)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 at java.lang.Class.forName0(Native Method)
 at java.lang.Class.forName(Unknown Source)

这是我的代码:

 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 public class Tests {


public static void main(String[] args) 
{
    String server = "myhost.com";
    String port = "5480";
    String dbName = "dbname";
    String url = "jdbc:netezza://" + server + "/" + dbName ;
    String user = "user";
    String pwd = "pwd";
    Connection conn = null;
    Statement st = null;
    ResultSet rs = null;
    try {
        Class.forName("org.netezza.Driver");
        System.out.println(" Connecting ... ");
        conn = DriverManager.getConnection(url, user, pwd);
        System.out.println(" Connected "+conn);

        String sql = "SELECT COUNT(*) FROM TABLE";
        st = conn.createStatement();
        rs = st.executeQuery(sql);
        if(rs.next()) {
            System.out.println(rs.getString(1));
        } else {
            System.out.println(" No data found");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if( rs != null)
                rs.close();
            if( st!= null)
                st.close();
            if( conn != null)
                conn.close();
        } catch (SQLException e1) {
                e1.printStackTrace();
            }
    }

}

 }
4

1 回答 1

1

在 Eclipse 中,您需要将 jar 添加到“构建路径”中的库中

Eclipse 文档

教程

于 2012-05-29T14:50:08.450 回答