Script to test direct connection to the database server host

Technote (FAQ)


Question

This java program will create a test table x and writes a row in x. It will connect directly to the database server host. Direct connection to the database server host can tell if the backend server has any issue or not. If the listener is slow in accepting, then we will be able to affirm that. This is to test whether you are able to connect directly from client to database server.

Answer

Run below script "test_con.java"(modify the name of the database and table to your needs).

Make sure CLASSPATH is set.

CLASSPATH=./:$INFORMIXDIR/jdbc/lib/ifxjdbc.jar

Run this way:

$ java test_con "jdbc:informix-sqli://<host_name>:<service_port>/<database_name>:informixserver=<dbservername>;user=<username>;password=<password-string> "
>>>Simple Select Statement test.
URL = "jdbc:informix-sqli://<host_name>:<service_port>/<database_name>:informixserver=<dbservername>;user=<username>;password=<password-string>"
testing
OK
>>>End of Simple Select Statement test.

---------------------
test_con.java
---------------------
import java.sql.*;
import java.util.*;

public class test_con {

public static void main(String[] args)
{
//String url = "jdbc:informix-sqli://<host_name>:<service_port>/<database_name>:informixserver=<dbservername>;user=<username>;password=<password-string>;";


Connection conn = null;
int rc;
String cmd=null;
Statement stmt = null;


String testName = "Simple Select Statement";

if (args.length == 0)
{
System.out.println("FAILED: connection URL must be provided in " +
"order to run the demo!");
return;
}

String url = args[0];


System.out.println(">>>" + testName + " test.");
System.out.println("URL = \"" + url + "\"");

try
{
Class.forName("com.informix.jdbc.IfxDriver");
}
catch (Exception e)
{
System.out.println("ERROR: failed to load Informix JDBC driver.");
}

try
{
conn = DriverManager.getConnection(url);
}
catch (SQLException e)
{
System.out.println("ERROR: failed to connect!");
}

try {
stmt=conn.createStatement();
cmd="drop table x;";
rc=stmt.executeUpdate(cmd);
}
catch (SQLException e)
{
System.out.println("Error:"+e.getMessage());
}
try
{
stmt = conn.createStatement();
cmd = "create table x (a char(50));";
rc = stmt.executeUpdate(cmd);
}
catch (SQLException e)
{
System.out.println("ERROR: execution failed - statement: " + cmd);
System.out.println("ERROR: " + e.getMessage());
}

try
{
String name="testing";

cmd = "insert into x values (?);";
PreparedStatement pstmt = conn.prepareStatement(cmd);
pstmt.setString(1, name);

rc = pstmt.executeUpdate();

}
catch (SQLException e)
{
System.out.println("ERROR: execution failed - statement: " + cmd);
System.out.println("ERROR: " + e.getMessage());
}

try{
String select="select a from x";
//System.out.println(select);

PreparedStatement pstmt = conn.prepareStatement(select);
ResultSet rset = pstmt.executeQuery();
while(rset.next()){
System.out.println(rset.getString(1));
System.out.println("OK");
}
rset.close();
pstmt.close();
}catch(SQLException e){
System.out.println("Failed to connect!");
}

try
{
conn.close();
}
catch (SQLException e)
{
System.out.println("ERROR: failed to close the connection!");
}

System.out.println(">>>End of " + testName + " test.");
}
}


Rate this page:

(0 users)Average rating

Document information


More support for:

Informix Servers

Software version:

11.5, 11.7, 12.1

Operating system(s):

AIX, HP-UX, Linux, Solaris, Windows

Software edition:

Developer, Enterprise, Express, Growth, Innovator, Ultimate

Reference #:

1502070

Modified date:

2013-08-01

Translate my page

Machine Translation

Content navigation