4 Replies - 11598 Views - Last Post: 02 May 2011 - 03:14 PM

#1 mil1234   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 110
  • Joined: 01-February 09

insert data to db using jsp with mvc

Posted 01 May 2011 - 07:14 AM

Hi to all,

currently i am building a web application and using mvc as structure. I was able to use mvc(jsp page + class + databaseMgr) to select data from db and display it on the jsp page. Now i need to create an admin function to be able to insert data to db and edit it. Can you guide me or give me some hints on how to accomplish this?My problem is that i don't know how to connect the jsp page text boxes values to the class(e.g student) and finally to the databasemgr class. With regards to the viewing of data from db i imported the classes to the jsp page and connected the result set i brand from db to student class and finally displayed it in jsp page. The problem is with the inserting now?Hope i made myself clear...tnx to all

Is This A Good Question/Topic? 0
  • +

Replies To: insert data to db using jsp with mvc

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3207
  • View blog
  • Posts: 19,239
  • Joined: 14-September 07

Re: insert data to db using jsp with mvc

Posted 01 May 2011 - 07:56 AM

Generally you'll have your servlet handle this.


Your form takes input, POSTs . Then the servlet catches the POST and fills a Student object (a JavaBean usually), which then gets written to your database.

Pulling information from the db works in reverse order: pull, fill Student, display on page.

You can do all of that without a Student object per se, but it makes your design (and ultimately debugging) cleaner/easier.
Was This Post Helpful? 2
  • +
  • -

#3 mil1234   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 110
  • Joined: 01-February 09

Re: insert data to db using jsp with mvc

Posted 01 May 2011 - 03:17 PM

I did some coding to show where my problem lies and to know if i'm making use of the class i designed for this task.

1)i have a addnewuser.jsp which is the form were admin will enter details of new user to be stored in db with action to direct to doAddNewUser.jsp
2)as said i have a doAddNewUser.jsp which consists of:
  <%@page import="Managers.DatabaseMgr"%>
        <% int id  = Integer.parseInt(request.getParameter("userid"));
           String name = request.getParameter("name");
           String surname = request.getParameter("surname");
           String password = request.getParameter("userpswd");
           String loginname = request.getParameter("userlgnname");
           String email = request.getParameter("email");
           long phoneno =Long.parseLong(request.getParameter("phoneno"));
           int usercode = Integer.parseInt(request.getParameter("usercode"));
           
           DatabaseMgr.addNewUser(id, name, surname, password, loginname, email, phoneno, usercode);
           

3)the addNewUser method in DatabaseMgr.java is this:
public static void addNewUser(int id,String name, String surname, String password, String loginname, String email, long phoneno, int usercode ) throws SQLException{
        
        makeConnection();
        PreparedStatement pst = null;
        int updateQuery = 0;
        
        String insertstring = "INSERT INTO `users`(`UserId`,`UserName`,`UserSurname`,`UserPassword`,`UserLoginName`,`UserEmailAdd`,`UserContactNo`,`UserCode`) VALUES ( id,name,surname,password,loginname,email,phoneno,usercode)";
        pst = myConnection.prepareStatement(insertstring);              
        pst.setInt(1, id);          
        pst.setString(2, name);                   
        pst.setString(3, surname); 
        pst.setString(4, password);
        pst.setString(5, loginname);
        pst.setString(6, email);
        pst.setLong(7, phoneno);
        pst.setInt(8, usercode);
        
        
        updateQuery = pst.executeUpdate();
    }


---which i guess isn't correct at all!!!!

so my main questions now are:
1)what is wrong with method above and
2)with this format am i making use of the user.java class i created?

thanks guys ,i really appreciate any type of help or guidance
Was This Post Helpful? 0
  • +
  • -

#4 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3207
  • View blog
  • Posts: 19,239
  • Joined: 14-September 07

Re: insert data to db using jsp with mvc

Posted 01 May 2011 - 03:33 PM

Business logic code in the presentation layer like that violates MVC. The data should be passed to the controller (read: servlet) for processing and eventual propagation to your database.
Was This Post Helpful? 0
  • +
  • -

#5 mil1234   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 110
  • Joined: 01-February 09

Re: insert data to db using jsp with mvc

Posted 02 May 2011 - 03:14 PM

Hi to all,

i have 1 jsp which is the <form> in which my administator will have the ability to add new users to my db table. this jsp directs to doAddNewUser.jsp which is shown below:
<%@page import="Classes.User"%>
        <%@page import="Managers.DatabaseMgr" %>
        <% User adduser = new User();
           adduser.setUserId(Integer.parseInt(request.getParameter("userid")));
           adduser.setName(request.getParameter("name"));
           adduser.setSurname(request.getParameter("surname"));
           adduser.setPassword(request.getParameter("userpswd"));
           adduser.setUsername(request.getParameter("userlgnname"));
           adduser.setEmail(request.getParameter("email"));
           adduser.setContactNo(Long.parseLong(request.getParameter("phoneno")));
           adduser.setUsercode(Integer.parseInt(request.getParameter("usercode")));
           
         
           
           DatabaseMgr.addNewUser();




i have also a class User.java which contains getters,setters, etc and finally a databaseMgr.java which in it i added the method which is supposed to get the users particulars and insert them to database table, which method is the one pasted below:
public static void addNewUser() throws SQLException {

        makeConnection();
        User addusr = new User();

        PreparedStatement psadduser = null;

        String sqlInsertuser = null;
        sqlInsertuser = "insert into users (UserId,UserName,UserSurname,UserPassword,UserLoginName,UserEmailAdd,UserContactNo,UserCode)"
                + "values(?,?,?,?,?,?,?,?)";
        psadduser = myConnection.prepareStatement(sqlInsertuser);
        psadduser.setInt(1, addusr.getUserId());
        psadduser.setString(2, addusr.getName());
        psadduser.setString(3, addusr.getSurname());
        psadduser.setString(4, addusr.getPassword());
        psadduser.setString(5, addusr.getUsername());
        psadduser.setString(6, addusr.getEmail());
        psadduser.setLong(7, addusr.getContactNo());
        psadduser.setInt(8, addusr.getUsercode());

        psadduser.executeUpdate();
    }



Most probably the problems i have in not writing to db come all from this methos.can u pls guide me on what i am doing wrong.Is it the sql statement or s'thing else????

Thanks in advance...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1