如何从Android应用程序调用存储过程?

时间:2013-01-15 14:19:26

标签: android sql-server-2008 stored-procedures

我想从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)

如果我想使用存储过程验证登录

,需要做什么

1 个答案:

答案 0 :(得分: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