简体   繁体   English

在Java中调用Informix存储过程

[英]Call Informix stored procedure in java

I have a stored procedure.. I want to call the procedure in java. 我有一个存储过程。我想在Java中调用该过程。 However, the result did not come out.. Error 但是,结果没有出来。

java.lang.NullPointerException 显示java.lang.NullPointerException

How actually to call informix stored procedure in java. 如何在Java中实际调用notifyix存储过程。 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 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());

}

You have not included stacktrace so it is hard to say what happened. 您尚未包括stacktrace,因此很难说出发生了什么。

From your comment it seems that you suspect Informix JDBC driver. 从您的评论看来,您似乎怀疑Informix JDBC驱动程序。 I have tried similar code with JDBC 4.10.JC2DE and it seems to work. 我已经在JDBC 4.10.JC2DE中尝试过类似的代码,但它似乎可以工作。 There is Jython code: 有Jython代码:

"""
    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. 它按预期打印10

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. 因此,也许您使用的Informix JDBC驱动程序版本较旧,但存在一些错误,但是我认为dbConnconn变量可能存在问题。 Are you sure you have connection to database? 您确定您已连接到数据库?


Edited: 编辑:

If you want to have only one connection (Singleton) then try something like: 如果您只想建立一个连接(Singleton),请尝试以下方法:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM