Question

I have a stored procedure.. I want to call the procedure in java. However, the result did not come out.. Error

java.lang.NullPointerException

How actually to call informix stored procedure in java. Please help me.. This is my code examples..

Stored procedure

create procedure tryBaru_Procedure(v_name varchar(50),v_city varchar(20),out v_id int)

select id
into v_id
from tryBaru
where name=v_name;

update tryBaru
set city=v_city
where id=v_id;
end procedure;

Java

public class TryBaru_Controller {


Connection conn;
DBConnection dbConn = new DBConnection();

public tryBaru p(tryBaru tryje) throws Exception
{
    conn = dbConn.getConnection();

    //prepare call store procedure
    CallableStatement cs = conn.prepareCall("{ call tryBaru_Procedure(?,?,?) }");


    cs.setString(1, tryje.getName());
    cs.setString(2, tryje.getCity());
    cs.registerOutParameter(3, Types.INTEGER);


     cs.executeQuery();
     tryje.setId(cs.getInt(3));
    cs.close();
    conn.close();

    return tryje;
}
}

Main

public static void main(String[] args){
    // TODO Auto-generated method stub

    TryBaru_Controller tbc = new TryBaru_Controller();
    tryBaru tb = new tryBaru();

    String name1 = "Faridah";
    String city1 = "Johor";

    tb.setName(name1);
    tb.setCity(city1);
    try {
        tbc.p(tb);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println(tb.getName());
    System.out.println(tb.getCity());
    System.out.println(tb.getId());

}
Was it helpful?

Solution

You have not included stacktrace so it is hard to say what happened.

From your comment it seems that you suspect Informix JDBC driver. I have tried similar code with JDBC 4.10.JC2DE and it seems to work. There is Jython code:

"""
    create procedure tryBaru_Procedure(v_name varchar(50),v_city varchar(20),out v_id int)
        let v_id = 10;
    end procedure;
"""

def test_call(db_url, usr, passwd):
    try:
        print("\n\n%s\n--------------\ncall..." % (db_url))
        db = DriverManager.getConnection(db_url, usr, passwd)
        proc = db.prepareCall("{ call tryBaru_Procedure(?, ?, ?) }");
        proc.setString(1, 'v_name')
        proc.setString(2, 'v_city')
        proc.registerOutParameter(3, Types.VARCHAR)
        proc.executeUpdate();
        v_id = proc.getInt(3)
        print('v_id: %s' % (v_id))
        db.close()
    except:
        print("there were errors!")
        s = traceback.format_exc()
        sys.stderr.write("%s\n" % (s))

It prints 10 as expected.

So maybe you use older version of Informix JDBC driver with some bugs, but I think there may be problem with dbConn or conn variable. Are you sure you have connection to database?


Edited:

If you want to have only one connection (Singleton) then try something like:

public Connection getConnection() throws Exception {
    /* Define the database to be used, the driver,
    * the connection url, and the username and
    * password to connect the database
    */
    if (connection == null) {
        driver = "com.informix.jdbc.IfxDriver";
        Class.forName(driver);
        connectionURL = "jdbc:informix-sqli://127.0.0.1:9090/barubuat:informixserver=lo_informix1210;user=informix;password=abc123";
        connection = DriverManager.getConnection(connectionURL);
    }
    return connection;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top