无法在JSON Java Restful中将用户插入数据库

时间:2016-12-18 17:24:51

标签: java json rest

我遇到了关于在数据库中插入用户的问题。我使用 Restful JDBC 来解析数据到android,我有两个类来执行插入用户跟随:

Register.java

@Path("/register")
public class Register {
@GET 
@Path("/doregister")  
@Produces(MediaType.APPLICATION_JSON) 
public String doLogin(@QueryParam("name") String name, @QueryParam("username") String uname, @QueryParam("password") String pwd){
    String response = "";
    int retCode = registerUser(name, uname, pwd);
    if(retCode == 0){
        response = Utitlity.constructJSON("register",true);
    }else if(retCode == 1){
        response = Utitlity.constructJSON("register",false, "You are already registered");
    }else if(retCode == 2){
        response = Utitlity.constructJSON("register",false, "Special Characters are not allowed in Username and Password");
    }else if(retCode == 3){
        response = Utitlity.constructJSON("register",false, "Error occured");
    }
    return response;

}

private int registerUser(String name, String uname, String pwd){
    System.out.println("Inside check registerUser method() ");
    int result = 3;
    if(Utitlity.isNotNull(uname) && Utitlity.isNotNull(pwd)){
        try {
            if(DBConnection.insertUser(name, uname, pwd)){
                System.out.println("RegisterUSer if");
                result = 0;
            }
        } catch(SQLException sqle){
            System.out.println("RegisterUSer catch sqle");
            //When Primary key violation occurs that means user is already registered
            if(sqle.getErrorCode() == 1062){
                result = 1;
            } 
            else if(sqle.getErrorCode() == 1064){
                System.out.println(sqle.getErrorCode());
                result = 2;
            }
        }
        catch (Exception e) {
            System.out.println("Inside checkCredentials catch e ");
            result = 3;
        }
    }else{
        System.out.println("Inside checkCredentials else");
        result = 3;
    }

    return result;
  } 
}

DBConnect.java

public static boolean insertUser(String name, String uname, String pwd) throws SQLException, Exception {
    boolean insertStatus = false;
    Connection dbConn = null;
    try {
        try {
            dbConn = DBConnection.createConnection();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Statement stmt = dbConn.createStatement();
        String query = "INSERT into ACCOUNT(name, username, password) values('"+name+ "',"+"'"
                + uname + "','" + pwd + "')";
        //System.out.println(query);
        int records = stmt.executeUpdate(query);
        //System.out.println(records);
        //When record is successfully inserted
        if (records > 0) {
            insertStatus = true;
        }
    } catch (SQLException sqle) {
        //sqle.printStackTrace();
        throw sqle;
    } catch (Exception e) {
        //e.printStackTrace();
        // TODO Auto-generated catch block
        if (dbConn != null) {
            dbConn.close();
        }
        throw e;
    } finally {
        if (dbConn != null) {
            dbConn.close();
        }
    }
    return insertStatus;
}

我的表帐号: enter image description here

当我在 Eclipse 上调试时,我看到结果返回没问题,但如果我使用高级rest客户端工具来获取数据,则发生异常:

URL Json:

http://localhost:9999/webserver/register/doregister?name=tester&username=tester@gmail.com&password=test12345

结果回复状态:

{
   "tag": "register",
   "status": false,
   "error_msg": "Error occured"
}

我找到并尝试了很多方法,但没找到原因

如何解决问题并将用户插入数据库?非常感谢!

0 个答案:

没有答案