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

SQL Server 不知道该call语句。改为使用EXEC

于 2013-01-15T15:17:55.747 回答