Linux 找不到适合 jdbc 的驱动程序:postgresql://192.168.1.8:5432/NexentaSearch

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16696688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:58:48  来源:igfitidea点击:

No suitable driver found for jdbc:postgresql://192.168.1.8:5432/NexentaSearch

javalinuxpostgresqljdbcdriver

提问by user2328488

I wrote following the java program

我是按照java程序写的

import java.io.*;
import java.util.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.*;

public class Sample {
    public static void main (String[] args) throws IOException  {
                    int CountComputers;
            FileInputStream fstream = new FileInputStream(
                    "/export/hadoop-1.0.1/bin/countcomputers.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String result=br.readLine();
            CountComputers=Integer.parseInt(result);
            input.close();
            fstream.close();
            Connection con = null;
            Statement st = null;
                ResultSet rs = null;    
               String url = "jdbc:postgresql://192.168.1.8:5432/NexentaSearch";
                String user = "postgres";
                String password = "valter89";
            ArrayList<String> paths = new ArrayList<String>();
            try
            {
                con = DriverManager.getConnection(url, user, password);
                        st = con.createStatement();
                        rs = st.executeQuery("select path from tasks order by id");
                while (rs.next()) { paths.add(rs.getString(1)); };
                PrintWriter zzz = null;
                    try
                    {
                            zzz = new PrintWriter(new FileOutputStream("/export/hadoop-1.0.1/bin/readwaysfromdatabase.txt"));
                    }
                    catch(FileNotFoundException e)
                    {
                            System.out.println("Error");
                            System.exit(0);
                    }
                    for (int i=0; i<paths.size(); i++)
                {
                    zzz.println("paths[i]=" + paths.get(i) + "\n");
                    }
                    zzz.close();

            }
            catch (SQLException e) 
            {
                System.out.println("Connection Failed! Check output console");
                e.printStackTrace();
            }
        }
}

I compiled this program and created the jar file

我编译了这个程序并创建了 jar 文件

./javac -classpath /folder/postgresql-8.4-703.jdbc3.jar -d /Samplejavaprogram/classes /Samplejavaprogram/src/Sample.java
./jar -cvf /Samplejavaprogram/Sample.jar -C /Samplejavaprogram/classes/ .

Jar has the following manifest file

Jar 有以下清单文件

Manifest-Version: 1.0
Created-By: 1.7.0_06 (Oracle Corporation)
Main-Class: Sample
Class-Path: /folder/postgresql-8.4-703.jdbc3.jar

also contains file /folder/postgresql-8.4-703.jdbc3.jar. I launched Sample.jar by means of a command

还包含文件 /folder/postgresql-8.4-703.jdbc3.jar。我通过命令启动了 Sample.jar

./java -jar -Djava.library.path=/opt/jdk1.7.0_06/lib /Samplejavaprogram/Sample.jar

and as a result I received the following messages

结果我收到了以下消息

Connection Failed! Check output console
java.sql.SQLException: No suitable driver found for jdbc:postgresql://192.168.1.8:5432/NexentaSearch
    at java.sql.DriverManager.getConnection(DriverManager.java:604)
    at java.sql.DriverManager.getConnection(DriverManager.java:221)
    at org.myorg.Sample.main(Sample.java:33)

I launched the file from a host with the address 192.168.1.10, and on a host 192.168.1.8 it fulfilled normally. Help to eliminate an error.

我从地址为 192.168.1.10 的主机启动了该文件,并在主机 192.168.1.8 上正常执行。帮助消除错误。

采纳答案by Reimeus

You are using a JDBC 3 driver. JDBC 4 drivers are loaded automatically loaded by the DriverManagerwhereas JDBC 3 drivers are not. Therefore you need to invoke

您正在使用 JDBC 3 驱动程序。JDBC 4 驱动程序由DriverManagerJDBC自动加载,而 JDBC 3 驱动程序则不是。因此你需要调用

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

once in your application, (prior to invoking DriverManager#getConnection).

一旦在您的应用程序中,(在调用之前DriverManager#getConnection)。



Alternatively, you can use the JDBC 4 PostgreSQL driver from herewhich will not require the above method call.

或者,您可以使用此处的 JDBC 4 PostgreSQL 驱动程序,它不需要上述方法调用。

回答by user4064927

Just use the jar command to extract the files your application needs to make the connection with the database server. e.g. jar -xf jdbc_file. Compile it with the javac -cp . and run it with the java -cp .

只需使用 jar 命令提取应用程序与数据库服务器建立连接所需的文件。例如 jar -xf jdbc_file。使用 javac -cp 编译它。并使用 java -cp 运行它。

And that's it. It will work.

就是这样。它会起作用。

回答by pedram bashiri

Use DriverManager.registerDriver(new org.postgresql.Driver());

DriverManager.registerDriver(new org.postgresql.Driver());