我正在使用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服务操作之外工作正常时,为什么它在这里显示异常......我该如何纠正它?
答案 0 :(得分:1)
System.out.println
语句来调试代码。关心Rentius