1 Replies - 934 Views - Last Post: 27 December 2009 - 05:50 PM Rate Topic: -----

#1 poodle123m   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-February 09

java sql update problem

Posted 27 December 2009 - 04:32 PM

i need to update the records of the users of a blog using java and all the data saves on mysql the only thing i can't do is update details and i don't know what i have wrong here's the update code: this one is the jsp:
i<%--
	Document   : updateUser
	Created on : 11-Dec-2009, 13:48:16
	Author	 : markcass
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="data.*"%>
<%@page import="database.*"%>
<%@page import="java.util.*"%>
<%@page import="servlets.*"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
	<%
DBConn myConn = new DBConn();

ArrayList<User> PenUsers = myConn.getPenUsers();


%>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Update</title>
	</head>
	<body>
		<h1>Modify</h1>
		<h5>Username</h5>
		<h5>Password</h5>
		<h5>Name</h5>
		<h5>Surname</h5>
		<h5>E-Mail</h5>
		<form name="updform" method="post" action="UpdateUserServlet">
			<table align="center" width="400px">
		   <%
				for(User usf:PenUsers)
				{

		  %>
		
				<tr>
					<td>
				   <p>
						Userid: <%=usf.getUserid()%>
					</p>
					</td>
</tr>
	<tr>
					<td>Username:</td>
					<td><input type="text" name="username" value="<%=usf.getUsername()%>"></td>
</tr>

				<tr>
					<td>Password:</td>
					<td><input type="text" name="password"value="<%=usf.getPassword()%>"></td>
				</tr>
				<tr>
					<td>Name:</td>
					<td><input type="text" name="userfirstname"value=<%=usf.getUserfirstname()%>></td>
				</tr>
				<tr>
					<td>Surname:</td>
					<td><input type="text" name="usersecondname"value=<%=usf.getUsersecondname()%>></td>
				</tr>
				<tr>
					<td>E-Mail:</td>
					<td><input type="text" name="useremail"value=<%=usf.getUseremail()%>></td>
				</tr>
				   <tr>
					<td>Role:</td>
					<td><input type="number" name="roleid" value=<%=usf.getRoleid()%>></td>
</tr>
<tr>
	<td>
					 <p>
						   <!--user gets accepted here -->
						   <a>href="AcceptServlet?userid=<%=usf.getUserid()%>">Accept User</a>
						</p>
				   
</td>
			</tr>
				



		  <%
				 }
		  %>
				 <tr>
					 
					

				   <td><input type="submit" value="register"/></td>
					 
						

				</tr>
			 </table>
				  </form>

		
	</body>
</html>


this is the servlet code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets;

import data.User;
import database.DBConn;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author markcass
 */
public class UpdateUserServlet extends HttpServlet {
   
	/** 
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			DBConn myConn = new DBConn();

ArrayList<User> penUsers = myConn.getPenUsers();

			User userToUpdate = new User();

			 //userToUpdate.setUserid(Integer.parseInt(request.getParameter("userid")));
			 userToUpdate.setUsername(request.getParameter("username"));
			 userToUpdate.setPassword(request.getParameter("password"));
			 userToUpdate.setUserfirstname(request.getParameter("userfirstname"));
			 userToUpdate.setUsersecondname(request.getParameter("usersecondname"));
			 userToUpdate.setUseremail(request.getParameter("useremail"));
			// userToUpdate.setRoleid(Integer.parseInt(request.getParameter("roleid")));
			  //  userToUpdate.setEnabled(Boolean.parseBoolean(request.getParameter("enabled")));
			 //DBConn myConn = new DBConn();

			 myConn.updateAuthor(userToUpdate);


			 response.sendRedirect("updateUser.jsp");




		} finally { 
			out.close();
		}
	} 

	// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
	/** 
	 * Handles the HTTP <code>GET</code> method.
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		processRequest(request, response);
	} 

	/** 
	 * Handles the HTTP <code>POST</code> method.
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		processRequest(request, response);
	}

	/** 
	 * Returns a short description of the servlet.
	 * @return a String containing servlet description
	 */
	@Override
	public String getServletInfo() {
		return "Short description";
	}// </editor-fold>

}


and this is the part of the database connector that updates:
 public void updateAuthor(User userToUpdate){
		try{
			connect();
			String sql = "update users set username = ?,password = ?,userfirstname = ?,usersecondname = ?,useremail = ? where userid = ?";
			 PreparedStatement ps = (PreparedStatement) dbConnection.prepareStatement(sql);
			ps.setString(1,userToUpdate.getUsername());
			ps.setString(2,userToUpdate.getPassword());
			ps.setString(3,userToUpdate.getUserfirstname());
			ps.setString(4,userToUpdate.getUsersecondname());
			ps.setString(5,userToUpdate.getUseremail());
		   // ps.setInt(6,userToUpdate.getRoleid());
			//ps.setBoolean(7,userToUpdate.getEnabled());
	  // ps.setInt(7,userToUpdate.getUserid());
			//added all fields for update user
			ps.executeUpdate();

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

the mysql table is called users and contains userid,username,password,userfirstname,usersecondname,useremail,roleid and enabled, i already created the users.java to insert the setters and getters and i can do the add user without any problem but i can't do update so if someone knows what i have wrong pls tell me

Is This A Good Question/Topic? 0
  • +

Replies To: java sql update problem

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: java sql update problem

Posted 27 December 2009 - 05:50 PM

Better to go to the JavaEE forum. There are more JSP resources there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1