1

我需要帮助才能在 netbeans 中调用 sqlserver 函数。首先,对不起,我的英语太差了,而且是个新手。

我在 netbeans 中使用了这段代码:

public void generateNoOrder()
{
    ResultSet rset = null;
    con = conf.makeConnection();
    //String result="";
    try{
        CallableStatement cs = con.prepareCall("{call get_date()}");
        cs.registerOutParameter(1, java.sql.Types.CHAR);
        rset = cs.executeQuery();
        //while(rset.next())
        //{
         String result = cs.getString(1);
        //}
        System.out.println(result);
    }
    catch (Exception ex){
        System.out.println(ex.getMessage());
        //return null;
    }
    finally {
        conf.closeConnection(con, s, rset);
    }
}

此函数不需要输入参数,并且此函数返回类型 char[12]

错误消息是:参数索引 1 无效。

请帮助我,谢谢!

4

1 回答 1

0

如果不需要输入参数并且它是您正在调用的函数,请尝试不使用 ()

CallableStatement cs = con.prepareCall("{call get_date}")

http://www.java2s.com/Code/JavaAPI/java.sql/ConnectionprepareCallStringsql.htm所示

并且因为您正在尝试检索输出参数,所以您还应该注册输出参数,就像这样

// Call a function with no parameters; the function returns a VARCHAR
// Prepare the callable statement
cs = connection.prepareCall("{? = call get_date}");

// Register the type of the return value
cs.registerOutParameter(1, java.sql.Types.CHAR);

// Execute and retrieve the returned value
cs.execute();
String result = cs.getString(1);

它在调用 MySQL 函数时对我有用,它应该与 SQL Server IMAO 一起使用。

于 2012-05-18T16:46:14.090 回答