Home > Enterprise >  How to separate this 2 users with their own content or todo list I hope you help me
How to separate this 2 users with their own content or todo list I hope you help me

Time:06-11

I'm developing a TODO list like add the data and display in table but when I log in other account I got the same content with other account how to separate the pages with thier own content.

Please check the picture below. thank you

enter image description here

enter image description here

My code is:

Dao.java

public class Dao {
    private final static String url = "jdbc:mysql://localhost:3306/todolist";
    private final static String user = "root";
    private final static String password = "";
    private final static String dbloader = "com.mysql.jdbc.Driver";
    private final static String ADD = "INSERT INTO `todo`(`TODO`) VALUES (?)";

    public static void Driver(String dbloader) {
        try {
            Class.forName(dbloader);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        Connection con = null;
        try {
            con = DriverManager.getConnection(url, user, password);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;

    }

    public static int ADDTODO(Model mod) {
        int i = 0;
        try {
            Driver(dbloader);
            Connection con = getConnection();
            PreparedStatement pst = con.prepareStatement(ADD);
            pst.setString(1, mod.getTODO());

            i = pst.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;

    }
}

**Addtodo**
protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String todo = request.getParameter("todo");

        Model mod = new Model();

        mod.setTODO(todo);

        Dao dao = new Dao();

        int i = dao.ADDTODO(mod);

        if (i > 0) {
            response.sendRedirect("Userpage.jsp");
        }

    }

Model.java

public class Model {
    private int ID;
    private String TODO;

    public Model() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Model(int iD, String tODO) {
        super();
        ID = iD;
        TODO = tODO;
    }

    public Model(String tODO) {
        super();
        TODO = tODO;
    }

    public int getID() {
        return ID;
    }

    public void setID(int iD) {
        ID = iD;
    }

    public String getTODO() {
        return TODO;
    }

    public void setTODO(String tODO) {
        TODO = tODO;
    }

}

Userpage.jsp

 <form action="Addtodo" method="post">
                        
                        <div > <input tenter code hereype="text"  name="todo" placeholder="What do you need to do today?"> <button type="submit"  value="add" >Add</button></div>
                        </form>

CodePudding user response:

You have to store the user's ID/name in each of the Todo item, otherwise you cannot differentiate which To Do item is owned/related to which user. You have to modify your Model class for TODO, add a int userId or string userName to the model class. And when the user creates a new To do item, you need to set its value as that of the logged in user's ID or name.

CodePudding user response:

Ebenezer, right now, you only have one table - todo - that stores the todo items. But since there is no user-specific data in the table, you are bound to get the same data for all users.

Currently, you do not have anything in your table that would differentiate between users.

You need to have 2 tables in your database.

  1. Todo
  • id
  • todo
  • userid
  1. User
  • id
  • name
  • ..(other relevant fields)

The userid field in Todo table is a foreign key to the id field in the User table. It acts as a link between the two tables.

So now to get a specific user's todos list, you will run a query like SELECT * from todo where userid = user.id, where user.id is the current user's id.

  • SQL foreign key
  • You could follow this tutorial - which does something similar to what you are doing. 1, 2.
  • Related