简体   繁体   中英

Calling SQL server 2008 Stored Procedure from Java

I have created the following stored procedure in SQL server 2008

Create PROCEDURE countInfected @infected int out
AS
Select @infected = COUNT(*) from userInfo where userID NOT IN (Select userID from deletedInfo);

My java Code is as follow

CallableStatement infected = con.prepareCall("exec countInfected()");
infected.registerOutParameter(1, java.sql.Types.INTEGER);
infected.execute();
    System.out.println("Infected"+ infected.getInt(1));

but it generate the following error

java.lang.NullPointerException at core.dtable.Dbase.stored(Dbase.java:51)

kindly guide me where i am wrong

     try {
        conn     = DBConnection.SQLDB();
        conn.setAutoCommit(false) ;
        CallableStatement cs = null;
        cs = conn.prepareCall("{call dbo.xxCount(?)}");
        cs.registerOutParameter(1, java.sql.Types.INTEGER);
        cs.execute() ;
        System.out.println("INFECTED "+cs.getInt(1));
    }catch (Exception err){

Put out parameter in your call to your stored proc.

As there is not full code posted, I assume nonInfected.registerOutParameter(1, java.sql.Types.INTEGER); is the line 51. What is nonInfected , is that required to call the given procedure.

@infected is not used in SQL query, should that not be just out type paramenter instead of in/out.

Try removing nonInfected.registerOutParameter(1, java.sql.Types.INTEGER); completely and run.

I have found the solution, i just add a statment to the sql code

Create PROCEDURE countInfected @infected int out
AS
Select @infected = COUNT(*) from userInfo where userID NOT IN (Select userID from deletedInfo);
return @infected;

and this work for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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