Code example for JsonObject

Methods: toStringaddProperty

0
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		String first_name = request.getParameter("first_name");
		String last_name = request.getParameter("last_name");
		 
		JsonObject js = new JsonObject();
 
		//***REQUIRED PARAMETER 
		if (username == null || password == null || email == null)
		{ 
			js.addProperty("result", false);
			js.addProperty("message", "Missing parameter");
			out.println(js.toString());
			return; 
		} 
		//******* 
		 
		// Get the hash of their password and store this instead 
		String hash = "";
		try { 
			hash = Password.getSaltedHash(password);
		} catch (Exception e1) {
			e1.printStackTrace();
		} 
		 
		if (first_name == null)
		{ 
			first_name ="";
		} 
		 
		if (last_name == null)
		{ 
			last_name = "";
		} 
		 
		Connection conn = null;
		PreparedStatement stmt = null;
		try{ 
			//STEP 2: Register JDBC driver 
			    
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(DB_URL,USER,PASS);
			 
			String sql = "SELECT username, email FROM user WHERE username = ? OR email = ?";
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, username);
			stmt.setString(2, email);
			 
			ResultSet rs = stmt.executeQuery();
				 
			rs.next();
			if(rs.getRow() == 0)
			{ 
				sql = "INSERT INTO user(username, password, email, first_name, last_name) VALUES(?, ?, ?, ?, ?)";
				stmt = conn.prepareStatement(sql);
				stmt.setString(1, username);
				stmt.setString(2, hash);
				stmt.setString(3, email);
				stmt.setString(4, first_name);
				stmt.setString(5, last_name);
				stmt.executeUpdate();
				js.addProperty("result", true);
				js.addProperty("message", "User register success");
			} 
			else 
			{ 
				js.addProperty("result", false);
				if(email.equals(rs.getString("email")))
				{ 
					js.addProperty("message", "Email already exists");
				} 
				else if(username.equals(rs.getString("username")))
				{ 
					js.addProperty("message", "Username already exists");
				} 
				else 
				{ 
					js.addProperty("message", "Unknown error");
				} 
			} 
			out.println(js.toString());
 
			//STEP 6: Clean-up environment 
			stmt.close();
			conn.close();
		}catch(SQLException se){
Connect your IDE to all the code out there  Get Codota for Java