Can not connect to database using jdbc
Posted in 1999
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("?");
};
}
}