问题描述
我写了以下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 static void main(String [] args)抛出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();
连接con = null;
语句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(通过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(连接失败!检查输出控制台);
e.printStackTrace();
}
}
}
我编译了这个程序并创建了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有以下清单文件
清单 - 版本:1.0
创建者:1.7.0_06(Oracle Corporation)
Main-Class:示例
Class-Path:/ folder / postgresql -8.4-703.jdbc3.jar
还包含文件/folder/postgresql-8.4-703.jdbc3 。罐。我通过命令启动了Sample.jar
./ java -jar -Djava.library.path = / opt / jdk1 .7.0_06 / lib /Samplejavaprogram/Sample.jar
因此我收到以下消息
连接失败!检查输出控制台
java.sql.SQLException:没有为jdbc找到合适的驱动程序:postgresql://192.168.1.8:5432 / NexentaSearch
at java.sql.DriverManager.getConnection(DriverManager.java:604) java.sql.DriverManager.getConnection($(((((((((((( / pre>
我从地址为192.168.1.10的主机启动了该文件,并在主机192.168.1.8上正常完成。
帮助消除错误。
解决方案您正在使用JDBC 3驱动程序。 JDBC 4驱动程序由 DriverManager
自动加载,而JDBC 3驱动程序则不加载。因此,您需要调用
Class.forName(org.postgresql.Driver);
一次在您的应用程序中,(在调用 DriverManager#getConnection $之前C $ C>)。
或者,您可以使用不需要上述方法调用。
I wrote following the java program
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
./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
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
./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.
解决方案 You are using a JDBC 3 driver. JDBC 4 drivers are loaded automatically loaded by the DriverManager
whereas JDBC 3 drivers are not. Therefore you need to invoke
Class.forName("org.postgresql.Driver");
once in your application, (prior to invoking DriverManager#getConnection
).
Alternatively, you can use the JDBC 4 PostgreSQL driver from here which will not require the above method call.
这篇关于没有为jdbc找到合适的驱动程序:postgresql://192.168.1.8:5432 / NexentaSearch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!