Re: Can not connect to database using jdbc
Posted in 1999
Topics: Connectivity: ODBC / JDBC / .NET, Security, Permissions & Auditing, Networking & sqlhosts Configuration, Java & JDBC Development
Bert
Shouldn't your /etc/services file read:
onlinenet 1550/tcp
?
HTH
Sujit
Bert van der Leest <b.vd.leest@rf.roccadefinance.nl> on 06/02/99 07:00:24
AM
Please respond to Bert van der Leest <b.vd.leest@rf.roccadefinance.nl>
To: informix-list@iiug.org
cc: (bcc: Sujit Pal)
Subject: Can not connect to database using jdbc
I am trying to make a connection with an informix database (running on a
informix dynamic server) through jdbc (version 1.4) but I can't get it to
work. What am I doing wrong?
The entries in the sqlhosts file look like this:
(dbservername:) congo_net (nettype:) ontlitcp (hostname:) congo
(servicename:) onlinenet.
Because the hosts and services file didn't exist I created them myself with
the following entries:
hosts: (ip-adress:) 192.168.1.3 (hostname:) congo;
services: (servicename:) onlinenet (port number and protocol:) tcp/1550.
I set the owner and group-id of these files to informix.
My source looks like this: (see below)
The problem is the getConnection part; this throws an exception and the
stacktrace looks like this:
java.sql.SQLException: Attempt to connect to database server (Java socket
exception encountered:Connection refused) failed.
at java.lang.Throwable.<init>(Compiled Code)
at java.lang.Exception.<init>(Compiled Code)
at java.sql.SQLException.<init>(Compiled Code)
at com.informix.jdbc.IfxSqliConnect.<init>(Compiled Code)
at com.informix.jdbc.IfxDriver.connect(Compiled Code)
at java.sql.DriverManager.getConnection(Compiled Code)
at java.sql.DriverManager.getConnection(Compiled Code)
at db.main(Compiled Code)
import com.informix.jdbc.*;
import java.sql.*;
import java.lang.*;
public class db {
public db () {
}
private static Connection conn=null;
private static final String
url =
"jdbc:informix-sqli://congo:1550/rox_db:
INFORMIXSERVER=congo_net;user=informix;password=informix";
public static void main(String args[]) {
try
Class.forName("com.informix.jdbc.IfxDriver");
System.out.println("Ok!");
}
catch (Exception e){
e.printStackTrace();
System.out.println(e.toString());
}
try
{
conn = DriverManager.getConnection(url);
}
catch (Exception e){
System.out.println("Something went wrong");
e.printStackTrace();
}
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("Select * from rox_user");
while (rs.next())
String address = rs.getString(1);
System.out.println(address);
}
}
catch (Exception e){
System.out.println("?");
};
}
}
I already solved the problem myself .The problem was that I thought that the hosts and services files were to be placed in the informix/etc directory. After I moved those to /etc everything went well!