【问题标题】:SQL Server stored procedure and jdbcSQL Server 存储过程和 jdbc
【发布时间】:2012-12-29 06:30:06
【问题描述】:

我正在努力使用 SQL Server 2005 和 JDBC。我有一个存储过程:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER proc [dbo].[sp_logincheck]
    @username as nvarchar(50),
    @password as nvarchar(50)
as
begin
    select * 
    from users 
    where user_name = @username 
      and password = @password 
      and is_active = 'Yes'
end

而我的User 班级是:

import java.sql.*;

public class User {
    private String username;

    private User(String username){
        this.username = username;
    }

    public static User login(String username, char [] password) throws SQLException{
        if(username == null || password == null){
            throw new IllegalArgumentException("Illegal arguments passed to method");
        }
        if(login1(username, password)){
            return new User(username);
        }
        return null;
    }

    private static boolean login1(String username, char [] password) throws SQLException{
        Connection connection = null;
        CallableStatement statement = null;
        try{
            connection = DriverManager.getConnection(AppParameters.dbURL, AppParameters.dbUsername, AppParameters.dbPassword);
        }catch(SQLException e){
            throw e;
        }
        try{
            statement = connection.prepareCall("{ ? = call dbo.sp_logincheck(?,?) }");
            statement.registerOutParameter(1, Types.INTEGER);
            statement.setString(2, username);
            statement.setString(3, new String(password));
            statement.execute();
            if(statement.getInt(1) == 1){
                System.out.println("Login Successfull");
                return true;
            }
            System.out.println("Login Failed");
            return false;
        }catch(SQLException sqle){
            sqle.printStackTrace();
            throw sqle;
        }finally{
            try{
                statement.close();
            }catch(Exception e){
            }
            try{
                connection.close();
            }catch(Exception e){
            }
        }
    }

    public  String getUsername(){
        return username;
    }
}

调用login() 方法总是打印Login Failed。如何在 SQL Server 中使用存储过程并使用 JDBC 执行用户登录?还是使用原始 SQL 语句更好?请指导我。

编辑:

我还想知道如何从上述存储过程中获取ResultSet,因为我在存储过程中有一个select 查询。

【问题讨论】:

    标签: java sql-server-2005 stored-procedures jdbc


    【解决方案1】:

    尝试将您的sql语句中的select * from users...更改为存储过程中的select count(*) from users...

    否则你的表达if(statement.getInt(1) == 1){没有任何意义。

    【讨论】:

    • 我这样做了,但输出相同:(
    • 您是否在更改存储过程后重新启动了应用程序?
    • 请在statement.execute(); 之后添加System.out.println(statement.getInt(1)); 并向我们展示它打印出来的内容。
    • @Meraman 你试过ResultSet rs = statement.executeQuery();rs.next() - 如果你有行,密码匹配。另外,我建议只选择计数,并使用 int count = rs.getInt("count");
    • @Andremoniy statement.getInt(1) 始终为有效或无效用户返回 0。
    【解决方案2】:

    尝试以下方法:

    ResultSet rs = statement.executeQuery();
    if (! rs.isLast()) {
        // match
    } else {
        // no match
    }
    

    另外,我建议只选择计数 (select count(*) as cnt from ...),并使用 int count = rs.next().getInt("cnt");

    【讨论】:

    • 它的工作.. 但我没有使用 if(!rs.isLast()),而是使用 return rs.next();会好的,对吧?
    【解决方案3】:

    如果

    statement.getInt(1) 
    等于结果行数或至少有一行。那么这可能是一个数据不匹配的情况。没有找到数据

    【讨论】:

    • 从语句中读取没有意义,他可以得到他为查询的第一个参数设置的值,如果是int。您甚至可以在调用查询之前执行此操作,这肯定不会包含任何结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    • 2011-09-22
    • 2016-09-30
    相关资源
    最近更新 更多