2

SQL Server からストアド プロシージャを呼び出してログイン ページを操作したいのですが、次のエラーが発生し続けます。

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class Sp
{
    public static void main(String[] args)
    {

         String dbName = "My_database";
         String serverip="192.168.5.10";
         String serverport="1433";

         String url = "jdbc:sqlserver://"+serverip+"\\SQLEXPRESS:"+serverport+";databaseName="+dbName+"";


         try

        {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        }

        catch(ClassNotFoundException ex)
        {
            ex.printStackTrace();
        }

        Connection con=null;
        CallableStatement cstmt=null;
        try
        {

             String databaseUserName = "sa";
             String databasePassword = "a123";

            con= DriverManager.getConnection(url, databaseUserName, databasePassword);


            cstmt=con.prepareCall("{? = call spLoginvalidate(@CO_ID)}");//called the procedure
                                        //how to create procedure had writen in  bellow
            cstmt.executeUpdate();
            System.out.println("done");
        } 

        catch(SQLException ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            try
            {
                if(cstmt!=null) //close the callablestatement
                {
                    cstmt.close();
                    cstmt=null;
                }
            }
            catch(SQLException ex)
            {
                ex.printStackTrace();
            }
            try
            {
                if(cstmt!=null)  //close the connection
                {
                    cstmt.close();
                    cstmt=null;
                }
            }
            catch(SQLException ex)
            {
                ex.printStackTrace();
            }
        }
    }
}

私のエラーメッセージは次のとおりです。

com.microsoft.sqlserver.jdbc.SQLServerException: The value is not set for the parameter number 1.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.buildParamTypeDefinitions(SQLServerPreparedStatement.java:260)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.buildPreparedStrings(SQLServerPreparedStatement.java:219)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doPrepExec(SQLServerPreparedStatement.java:612)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:400)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:314)
    at com.first.mainapp.Sp.main(Sp.java:45)

ストアド プロシージャを使用してログインを検証する場合に必要なこと

4

1 に答える 1

0

それ以外の

        cstmt=con.prepareCall("{? = call spLoginvalidate(@CO_ID)}");//called the procedure

試す

        cstmt=con.prepareCall("{EXEC ? = spLoginvalidate(?)}");//called the procedure

        cs.registerOutParameter(1, Types.INT);
        cs.setInt(2, 1000);//your @CO_ID value here

ここで取得できる詳細情報: http://www.xyzws.com/javafaq/how-to-call-a-stored-procedure-by-jdbc-java-class/169

callSQL Server はステートメントを認識していません。EXEC代わりに使用してください。

于 2013-01-15T15:17:55.747 に答える