Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


gautamshetkar
edit data
2 Answer(s)      3 years and 5 months ago
Posted in : JSP-Servlet


sir i want to do edit a particular customers information after he logs in, i am also using sessions. thank you


Advertisement
View Answers

April 1, 2011 at 3:41 PM


1)login.jsp:

<html>
<script>
function validate(){
var username=document.form.user.value;
var password=document.form.pass.value;
if(username==""){
 alert("Enter Username!");
  return false;
}
if(password==""){
 alert("Enter Password!");
  return false;
}
return true;
}
</script>
<form name="form" method="post" action="check.jsp" onsubmit="javascript:return validate();">
<table>
<tr><td>Username:</td><td><input type="text" name="user"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass"></td></tr>
<tr><td></td><td><input type="submit" value="Submit"></td></tr>
</table>
</form>
</html>

2)check.jsp:

<%@page import="java.sql.*"%>

<%
String user=request.getParameter("user");
String pass=request.getParameter("pass");
 Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");  
           Statement st=con.createStatement();
           ResultSet rs=st.executeQuery("select * from login where username='"+user+"' and password='"+pass+"'");
int count=0;
int id=0;
          while(rs.next())
          {
            id=rs.getInt("id");
                   count++;
          }

                    if(count>0)
          {
            out.println("welcome "+user);
            %>
<a href="edit.jsp?id=<%=id%>">Edit Profile</a>
            <%
          }
          else
          {
                       response.sendRedirect("login.jsp?msg=Invalid Username or Password");
          }
%>

3)edit.jsp:

<%@page language="java"%>
<%@page import="java.sql.*"%>
<form method="post" action="update.jsp">
<table>
<%
String id=request.getParameter("id");
int no=Integer.parseInt(id);
int sumcount=0;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
String query = "select * from login where id='"+no+"'";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
if(rs.next()){
%>
<tr><td>User Name:</td><td><input type="text" name="name" value="<%=rs.getString("username")%>"></td></tr>
<tr><td>Address:</td><td><input type="text" name="address" value="<%=rs.getString("address")%>"></td></tr>
<tr><td>Email:</td><td><input type="text" name="email" value="<%=rs.getString("email")%>"></td></tr>
<tr><td>Contact No:</td><td><input type="text" name="contact" value="<%=rs.getString("contactNo")%>"></td></tr>
<tr><td><input type="hidden" name="id" value="<%=rs.getString(1)%>"></td></tr>

<%
}
%>
<tr><td><input type="submit" name="Submit" value="Update" style="background-color:#49743D;font-weight:bold;color:#ffffff;"></td>
</tr>
<%
}
catch(Exception e){}
%>
</table>
</form>


April 1, 2011 at 3:41 PM


4)update.jsp:

<%@page import="java.sql.*"%>
<%
String ide=request.getParameter("id");
int num=Integer.parseInt(ide);
String name=request.getParameter("name");
String address=request.getParameter("address");
int contact=Integer.parseInt(request.getParameter("contact"));
String email=request.getParameter("email");
try{
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
Statement st=null;
st=conn.createStatement();
st.executeUpdate("update login set username='"+name+"',address='"+address+"',contactNo="+contact+",email='"+email+"' where id='"+num+"'");
}
catch(Exception e){
System.out.println(e);
}
%>


Related Tutorials/Questions & Answers:
 

 

 

DMCA.com