首页 > 解决方案 > Webservice 返回空字符串而不是指定的字符串

问题描述

您好,我在 Java 中创建了返回有关注册用户的信息的 Web 服务。这是我的代码:

    public String addUser(User user) {


    List<String> initialValues = new ArrayList<String>();
    try {
        PreparedStatement prePreparedStatement = connection.prepareStatement("select count(*) as total from users where name='" + user.getName() + "'");
        ResultSet rs = prePreparedStatement.executeQuery();
        if (rs.next())
            if (rs.getInt("total") > 0) {
                return "user already exists";
            }


        PreparedStatement ps = connection.prepareStatement("insert into users(name,pass,level,exp,gold,session_id,reg_date,email) values (?, ?, ?, ?, ?, ?, ?, ?)");
        ps.setString(1, user.getName());
        ps.setString(2, user.getPass());
        ps.setInt(3, 1);
        ps.setInt(4, 1);
        ps.setInt(5, 100);
        ps.setString(6, "offline");
        ps.setTimestamp(7, new Timestamp(System.currentTimeMillis()));
        ps.setString(8, user.getEmail());
        ps.executeUpdate();

        initialValues.add(getUserIdByNameAndPass(user.getName(), user.getPass()));
        initialValues.add("1");
        initialValues.add("0");
        initialValues.add("5");
        initialValues.add("100");
        usersBasicInfoDao.addUsersBasicInfo(initialValues);

    } catch (SQLException e) {
        e.printStackTrace();
        return e.getMessage();
    }
    return "added successfully";
}

但是每次添加用户时,它似乎return e.getMessage();都是用空字符串调用而不是return "added successfully";为什么会发生?

标签: javaweb-services

解决方案


推荐阅读