如何执行Transact-SQL函数

时间:2022-03-09 12:52:38

How can we execute Transact-SQL functions using JAVA.

我们如何使用JAVA执行Transact-SQL函数。

Transact-SQL function :

Transact-SQL功能:

SELECT SUSER_ID('sa'); 

We have to execute the above statement using java. Can we achieve this using JAVA ?

我们必须使用java执行上面的语句。我们可以使用JAVA实现这一目标吗?

Thanks for your responses. Hi, I'm trying like below, getting "com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '{' " Error when I'm using below.

谢谢你的回复。嗨,我正在尝试如下,得到“com.microsoft.sqlserver.jdbc.SQLServerException:'{'”错误,当我在下面使用时出现错误语法。

     String loginName = "domain\\geethap";
     String selectQuery = "{ EXEC SUSER_SID(?) }";

    CallableStatement cs = connection.prepareCall(selectQuery);
    cs.setNString(1, loginName);
    cs.registerOutParameter(1,java.sql.Types.VARBINARY);
    cs.execute();

    ResultSet resSet = cs.getResultSet();
logger.error("** got resultset **");    
    while(resSet.next()){
        byte[] result = resSet.getBytes(1);
        log.error("ByteSet"+result.length);
        log.error("** ResultSet object:"+result);

    }

1 个解决方案

#1


1  

use a callable statement:

使用可调用语句:

String loginName = "domain\\geethap";
String selectQuery = "EXEC SUSER_ID ?";

CallableStatement cs = connection.prepareCall(selectQuery);
cs.setNString(1, loginName);
cs.registerOutParameter(1,java.sql.Types.VARBINARY);
cs.execute();

ResultSet resSet = cs.getResultSet();
logger.error("** got resultset **");    
while(resSet.next()){
    byte[] result = resSet.getBytes(1);
    log.error("ByteSet"+result.length);
    log.error("** ResultSet object:"+result);
}

#1


1  

use a callable statement:

使用可调用语句:

String loginName = "domain\\geethap";
String selectQuery = "EXEC SUSER_ID ?";

CallableStatement cs = connection.prepareCall(selectQuery);
cs.setNString(1, loginName);
cs.registerOutParameter(1,java.sql.Types.VARBINARY);
cs.execute();

ResultSet resSet = cs.getResultSet();
logger.error("** got resultset **");    
while(resSet.next()){
    byte[] result = resSet.getBytes(1);
    log.error("ByteSet"+result.length);
    log.error("** ResultSet object:"+result);
}