Connexion à informix

Résolu/Fermé
amina_n - 20 juil. 2007 à 08:18
 amina_n - 26 juil. 2007 à 07:57
Bonjour.Pouvez vous m'aider à trouver un code java permettant la connexion à une base de données informix?

2 réponses

Donald Payne
25 juil. 2007 à 04:32
Ici un code qui fait une connexion a informix. Pardon à mon francais. J'ai emprunté le code de "demo" trouvé a C:\Program Files\IBM\Informix_JDBC_Driver\demo\basic\DBConnection.java.

/**************************************************************************
 *
 * Licensed Materials - Property of IBM Corporation
 *
 * Restricted Materials of IBM Corporation
 *
 * IBM Informix JDBC Driver
 * (c) Copyright IBM Corporation 1998, 2004 All rights reserved.
 *
 ****************************************************************************/
/***************************************************************************
 *
 *  Title:         DBConnectSample.java
 *
 *  Description:    Demo a connection to a database of a particular server
 *
 *  An example of running the program: 
 *
 *   java DBConnectSample 
 *      'jdbc:informix-sqli://myhost:1533/myDBname:informixserver=myserver;user=<username>;password=<password>'
 *
 *   Expected result:
 * 
 * >>>Database Connection Direct test.
 * URL = "jdbc:informix-sqli://myhost:1533/myDBname:informixserver=myserver;user=<username>;password=<password>"
 * >>>End of Database Connection Direct test.
 * >>>Database Connection Indirect test.
 * URL = "jdbc:informix-sqli://myhost:1533:informixserver=myserver;user=<username>;password=<password>"
 * >>>End of Database Connection Indirect test.
 * 
 ***************************************************************************
 */

 

import java.sql.*;
import java.util.*;

public class DBConnectSample
{

    public static void main(String[] args)
    {
        String usage = "Example: 'jdbc:informix-sqli://myhost:1533/myDBname:informixserver=myserver;user=<username>;password=<password>'";
        if (args.length == 0)
        {
            System.out.println("FAILED: connection URL must be provided in order to run the demo!");
            System.out.println(usage);
            return;
        }

        String url = args[0];
        StringTokenizer st = new StringTokenizer(url, ":");
        String token;
        String tokens[] = new String[5];
        StringTokenizer portDbSt;
        String portDbTokens[] = new String [2];
        String newUrl = "";

        for (int i = 0; i < 5; ++i)
        { 
            if (!st.hasMoreTokens())
            {
                System.out.println("FAILED: incorrect URL format!");
                System.out.println("URL ,'" + url + "', should have 5 parts separated by colons ':', but has only " + i + " parts");
                System.out.println(usage);
                return;
            }
            tokens[i] = st.nextToken();
            // Handle database name in port-DB part of URL, e.g. "1533/myDBname"
            if (i == 3)
            {
                portDbSt = new StringTokenizer(tokens[i], "/");
                for (int j = 0; j < 2; ++j)
                {
                    if (!portDbSt.hasMoreTokens())
                    {
                        System.out.println("FAILED: incorrect URL format!");
                        System.out.println("port-DB part of URL, '" + tokens[i] + "', should have 2 parts, port and database name, separated by forward slash '/', but has only " + j + " parts");
                        System.out.println(usage);
                        return;
                    }
                    portDbTokens[j] = portDbSt.nextToken();
                }
            }
        
            if (newUrl != "")
                newUrl += ":";
            newUrl += tokens[i];
        }

        if (st.hasMoreTokens())
        { 
            System.out.println("URL ,'" + url + "', should have only 5 parts separated by colons ':', but has a 6th part, '" + st.nextToken() + "'");
            return;
        }
                        
//INFORMIX_EXTEXT_BEGIN DBConn1.jav
        String cmd = null;
        int rc;
//INFORMIX_EXTEXT_END DBConn1.jav

        String testName = "Database Connection Direct";

//INFORMIX_EXTEXT_BEGIN DBConn2.jav
        Connection conn = null;
//INFORMIX_EXTEXT_END DBConn2.jav

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

//INFORMIX_EXTEXT_BEGIN DBConn3.jav
        try 
        {
            Class.forName("com.informix.jdbc.IfxDriver");
        } 
        catch (Exception e)
        {
            System.out.println("FAILED: failed to load Informix JDBC driver.");
            System.out.println("FAILED: " + e.getMessage());
            return;
        }

        try 
        {
            conn = DriverManager.getConnection(newUrl);
        } 
        catch (SQLException e) 
        {
            System.out.println("FAILED: failed to connect!");
            System.out.println("FAILED: " + e.getMessage());
            return;
        }
//INFORMIX_EXTEXT_END DBConn3.jav

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

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

        newUrl = tokens[0] + ":" + tokens[1] + ":" + tokens[2] + ":" + portDbTokens[0] + ":" + tokens[4];
        testName = "Database Connection Indirect";

        conn = null;

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

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

        try 
        {
            conn = DriverManager.getConnection(newUrl);
        } 
        catch (SQLException e) 
        {
            System.out.println("FAILED: failed to connect!");
            System.out.println("FAILED: " + e.getMessage());
            return;
        }

//INFORMIX_EXTEXT_BEGIN DBConn4.jav
        try
        {
            Statement stmt = conn.createStatement();
            cmd = "database " + portDbTokens[1]  + ";";
            System.out.println("Execute statement \"" + cmd + "\"...");
            rc = stmt.executeUpdate(cmd);
            stmt.close();
        }
        catch (SQLException e)
        {
            System.out.println("FAILED: execution failed - statement: " + cmd);
            System.out.println("FAILED: " + e.getMessage());
            return;
        }
//INFORMIX_EXTEXT_END DBConn4.jav

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

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

        return;
    }
}



Le code exige que CLASSPATH qui contient le "jar" ifxjdbc.jar. Ou, on peut ajoute un parameter "-cp" ou "-classpath" et le nom de ifxjdbc.jar, comme l'example suivante.

Le code exige en tout cas un parameter, le URL de connexion. Par example:
java -cp ..\..\lib\ifxjdbc.jar;..\..\..\Java50\jre\lib\core.jar DBConnectSample.class jdbc:informix-sqli://myhost:9088/mydb:informixserver=ol_svr_custom;user=informix;password=in4mix

myhost        == hostname
9088          == port auquel informix ecoute
mydb          == nom de base de donnés
ol_svr_custom == INFORMIXSERVER (dans onconfig ou SetNet32)
informix      == nom do utilisateur de base de donnés
in4mix        == password ou clé


J'espere que cela vous aide,
- Donald
1
Bonjour.

Merci beaucoup pour votre aide.
1