Web服务操作中的变量未更新

时间:2013-04-05 11:22:20

标签: java web-services rest variables exception

我正在使用java创建一个非常简单的Web服务示例。我有一个初始化为0的变量num,但我无法在结束时保留其值。 根据我的数据库,它的值应该增加1。

以下是Web服务操作:

@WebMethod(operationName = "getNewID")
public String getNewID(@WebParam(name = "newID") String txt2) 
{
Connection connection = null;  
ResultSet resultSet = null;  
Statement statement = null;
int num=0;
    try 
        {  
            Class.forName("org.sqlite.JDBC");  
            connection = DriverManager.getConnection("jdbc:sqlite:src/java/db/test.sqlite");  
            statement = connection.createStatement();
            resultSet = statement.executeQuery("SELECT ID FROM LOGINDETAILS where ID = '"+txt2+"'");

            while (resultSet.next()) 
                {  
                    //System.out.println("Your ID "+ resultSet.getString("ID"));
                    num++;
                }

        } 

    catch (Exception e) 
        {  
            System.out.println("You had an exception");
            e.printStackTrace();  
        }

    finally 
        {  
        try 
            {  
                resultSet.close();  
                statement.close();  
                connection.close();  
            } 
        catch (Exception e) 
            {  
                e.printStackTrace();  
            }  
        } 
    if(num==0)
        { 
            return "Your ID does not match";
        }

    else
        {
            return "Welcome "+txt2;
        }
}

在输出中我得到了

返回方法 java.lang.String:“你的ID不匹配”

对于所有情况,只有if部分正在工作而不是其他部分。当我在类中尝试相同的方法时,它的工作正常,但不是在Web服务操作的情况下

新代码

@WebMethod(operationName = "getNewID")
public String getNewID(@WebParam(name = "newID") String txt2) 
{
String str = null;
Connection connection = null;  
ResultSet resultSet = null;  
Statement statement = null;
    int num=0;
    try 
        {  
            Class.forName("org.sqlite.JDBC");  
            connection = DriverManager.getConnection("jdbc:sqlite:src/java/db/test.sqlite");  
            statement = connection.createStatement();
            resultSet = statement.executeQuery("SELECT ID FROM LOGINDETAILS where ID = '"+txt2+"'");

            while (resultSet.next()) 
                {  
                    //System.out.println("Your ID "+ resultSet.getString("ID"));
                    num++;
                }

        } 

    catch (Exception e) 
        {  
            System.out.println("You had an exception");
            e.printStackTrace();  
        }

    finally 
        {  
        try 
            {  
                resultSet.close();  
                statement.close();  
                connection.close();  
            } 
        catch (Exception e) 
            {  
                e.printStackTrace();  
            }  
        } 
    assert num == 0 || txt2 == null : str = "num "+num+"txt2 "+txt2;
    return str;

    /*if(num==0)
        { 
            return "Your ID does not match Please Try Again Or if new user then please register";
        }

    else
        {
            return "Welcome "+txt2;
        }*/
}

在结果中我得到str的值为null。

好的我现在试过了..

@WebMethod(operationName = "getNewID")

public String getNewID(@WebParam(name = "newID") String txt2) 
{
Connection connection = null;  
ResultSet resultSet = null;  
Statement statement = null;
int num=0;
    try 
        {  
            Class.forName("org.sqlite.JDBC");  
            connection = DriverManager.getConnection("jdbc:sqlite:src/java/db/test.sqlite");  
            statement = connection.createStatement();
            resultSet = statement.executeQuery("SELECT ID FROM LOGINDETAILS where ID = '"+txt2+"'");

            while (resultSet.next()) 
                {  
                    num++;
                }
            return resultSet.toString();

        } 

    catch (Exception e) 
        {  
            System.out.println("You had an exception");
            e.printStackTrace();
            return "You had an exception";
        }


}

我正在获取输出

java.lang.String : "You had an exception"

当它在Web服务操作之外工作正常时,为什么它在这里显示异常......我该如何纠正它?

1 个答案:

答案 0 :(得分:1)

  1. 检查例外情况
  2. 通过调试器,断言语句或如果您确实需要System.out.println语句来调试代码。
  3. 如果能够,请先对db执行查询,看看是否得到了预期的结果。
  4. 确保正在加载的驱动程序的类正确加载。确保lib在类路径中。
  5. 关心Rentius