Обсуждение: Problem with Jdk1.4 and Linux
When i try to execute this code:
Start code ------------------------------------------------------
package untitled1;
/**
* A demo program to show how jdbc works with postgresql
* Nick Fankhauser 10/25/01
* nickf@ontko.com or nick@fankhausers.com
* This program may be freely copied and modified
* Please keep this header intact on unmodified versions
* The rest of the documentation that came with this demo program
* may be found at http://www.fankhausers.com/postgresql/jdbc
*/
import java.sql.*; // All we need for JDBC
import java.text.*;
import java.io.*;
public class HelloPostgresql
{
Connection db; // A connection to the database
Statement sql; // Our statement to run queries with
DatabaseMetaData dbmd; // This is basically info the driver delivers
// about the DB it just connected to. I use
// it to get the DB version to confirm the
// connection in this example.
public HelloPostgresql(String argv[])
throws ClassNotFoundException, SQLException
{
String database = argv[0];
String username = argv[1];
String password = argv[2];
String ip = argv[3];
Class.forName("org.postgresql.Driver"); //load the driver
db = DriverManager.getConnection("jdbc:postgresql://"+ ip + "/"+database,
username,
password); //connect to the db
dbmd = db.getMetaData(); //get MetaData to confirm connection
System.out.println("Connection to "+dbmd.getDatabaseProductName()+" "+
dbmd.getDatabaseProductVersion()+" successful.\n");
sql = db.createStatement(); //create a statement that we can use later
String sqlText = "create table jdbc_demo (code int, text varchar(20))";
System.out.println("Executing this command: "+sqlText+"\n");
sql.executeUpdate(sqlText);
sqlText = "insert into jdbc_demo values (1,'One')";
System.out.println("Executing this command: "+sqlText+"\n");
sql.executeUpdate(sqlText);
sqlText = "insert into jdbc_demo values (3,'Four')";
System.out.println("Executing this command twice: "+sqlText+"\n");
sql.executeUpdate(sqlText);
sql.executeUpdate(sqlText);
sqlText = "update jdbc_demo set text = 'Three' where code = 3";
System.out.println("Executing this command: "+sqlText+"\n");
sql.executeUpdate(sqlText);
System.out.println (sql.getUpdateCount()+
" rows were update by this statement\n");
System.out.println("\n\nNow demostrating a prepared statement...");
sqlText = "insert into jdbc_demo values (?,?)";
System.out.println("The Statement looks like this: "+sqlText+"\n");
System.out.println("Looping three times filling in the fields...\n");
PreparedStatement ps = db.prepareStatement(sqlText);
for (int i=10;i<13;i++)
{
System.out.println(i+"...\n");
ps.setInt(1,i); //set column one (code) to i
ps.setString(2,"HiHo"); //Column two gets a string
ps.executeUpdate();
}
ps.close();
System.out.println("Now executing the command: "+
"select * from jdbc_demo");
ResultSet results = sql.executeQuery("select * from jdbc_demo");
if (results != null)
{
while (results.next())
{
System.out.println("code = "+results.getInt("code")+
"; text = "+results.getString(2)+"\n");
}
}
results.close();
sqlText = "drop table jdbc_demo";
System.out.println("Executing this command: "+sqlText+"\n");
sql.executeUpdate(sqlText);
db.close();
}
public static void correctUsage()
{
System.out.println("\nIncorrect number of arguments.\nUsage:\n "+
"java \n");
System.exit(1);
}
public static void main (String args[])
{
if (args.length != 4) correctUsage();
try
{
HelloPostgresql demo = new HelloPostgresql(args);
}
catch (Exception ex)
{
System.out.println("***Exception:\n"+ex);
ex.printStackTrace();
}
}
}
end code ------------------------------------------------------
I get the following error:
Start error report ------------------------------------------------------
org.postgresql.util.PSQLException: Il tentativo di connessione è fallito perché Exception: java.net.SocketException: Invalid argument or cannot assign requested address
Stack Trace:
java.net.SocketException: Invalid argument or cannot assign requested address
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:305)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:171)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:158)
at java.net.Socket.connect(Socket.java:452)
at java.net.Socket.connect(Socket.java:402)
at java.net.Socket.<init>(Socket.java:309)
at java.net.Socket.<init>(Socket.java:124)
at org.postgresql.core.PGStream.<init>(PGStream.java:47)
at org.postgresql.jdbc1.AbstractJdbc1Connection.openConnection(AbstractJdbc1Connection.java:197)
at org.postgresql.Driver.connect(Driver.java:139)
at java.sql.DriverManager.getConnection(DriverManager.java:512)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at untitled1.HelloPostgresql.<init>(HelloPostgresql.java:36)
at untitled1.HelloPostgresql.main(HelloPostgresql.java:118)
End of Stack Trace
at org.postgresql.jdbc1.AbstractJdbc1Connection.openConnection(AbstractJdbc1Connection.java:208)
at org.postgresql.Driver.connect(Driver.java:139)
at java.sql.DriverManager.getConnection(DriverManager.java:512)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at untitled1.HelloPostgresql.<init>(HelloPostgresql.java:36)
at untitled1.HelloPostgresql.main(HelloPostgresql.java:118)
***Exception:
org.postgresql.util.PSQLException: Il tentativo di connessione è fallito perché Exception: java.net.SocketException: Invalid argument or cannot assign requested address
Stack Trace:
java.net.SocketException: Invalid argument or cannot assign requested address
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:305)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:171)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:158)
at java.net.Socket.connect(Socket.java:452)
at java.net.Socket.connect(Socket.java:402)
at java.net.Socket.<init>(Socket.java:309)
at java.net.Socket.<init>(Socket.java:124)
at org.postgresql.core.PGStream.<init>(PGStream.java:47)
at org.postgresql.jdbc1.AbstractJdbc1Connection.openConnection(AbstractJdbc1Connection.java:197)
at org.postgresql.Driver.connect(Driver.java:139)
at java.sql.DriverManager.getConnection(DriverManager.java:512)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at untitled1.HelloPostgresql.<init>(HelloPostgresql.java:36)
at untitled1.HelloPostgresql.main(HelloPostgresql.java:118)
End of Stack Trace
end error report ------------------------------------------------------
I am sure that this code is working because same code and library used to connect to the same server but with windowsxp work perfectly.
I have tried a lot of combination:
Windows:
Work with jdk1.4 and 1.5 without any problem
Linux:
jdk 1.4.2_04-b05 do not work
jdk1.5.0_04 all works perfect
I have tried to use the jdbc driver 8.1dev, 8.0, 7.x but with all version the same error if i use the jdk1.4.
I have also tried to change the machine but the problem do not solve:
64bit AMD64 Fedora core 4 kernel-2.6.11-1.1369_FC4
32bit Pentium-M Fedora core 4 kernel-2.6.11-1.1369_FC4
Thank you very much
(and sorry for the bad english :)
Daniele Panozzo
PANOZZ00USER0002 wrote: > java.net.SocketException: Invalid argument or cannot assign requested > address What is the command line you're running that code with? Is the server or client using IPv6 at all? If you specify the server by IP address rather than name, does it work? -O