i ve creted registration page n login page. after entering details in reg page,n enter the submit button,how can v store the data in db, n hw can v retrive the data frm db(m developing new website,so it doesn't ve any data)... bt after storing data,thr will b data na ,so hw to retrive that data.... rpy asap plzzz,i ve to submit project in this week....
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="Login"></td></tr> <tr><td></td><td><a href="register.jsp">Register Here</a></td></tr> </table> </form> </html>
2)check.jsp:
<%@page import="java.sql.*"%> <% try{ 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; while(rs.next()) { count++; } if(count>0){ out.println("welcome "+user); Statement s=con.createStatement(); ResultSet rst=s.executeQuery("select * from login where username='"+user+"' and password='"+pass+"'"); while(rst.next()){ System.out.println(rst.getString("firstname")); %> <h3>Your Account Information</h3> <table> <tr><td>FirstName:</td><td><input type="text" name="name" value="<%=rst.getString("firstname")%>"></td></tr> <tr><td>LastName:</td><td><input type="text" name="name" value="<%=rst.getString("lastname")%>"></td></tr> <tr><td>Address:</td><td><input type="text" name="name" value="<%=rst.getString("address")%>"></td></tr> <tr><td>Contact No:</td><td><input type="text" name="name" value="<%=rst.getString("contactNo")%>"></td></tr> <tr><td>Email:</td><td><input type="text" name="name" value="<%=rst.getString("email")%>"></td></tr> </table> <% } } else { response.sendRedirect("login.jsp"); } } catch(Exception e){ System.out.println(e); } %>
continue..
3)register.jsp:
<html> <form method="post" action="insertdata.jsp"> <table> <tr><td>Username:</td><td><input type="text" name="uname"></td></tr> <tr><td>First Name:</td><td><input type="text" name="fname"></td></tr> <tr><td>Last Name:</td><td><input type="text" name="lname"></td></tr> <tr><td>Password:</td><td><input type="password" name="pass"></td></tr> <tr><td>Address:</td><td><input type="text" name="address"></td></tr> <tr><td>Contact No:</td><td><input type="text" name="contact"></td></tr> <tr><td>Email:</td><td><input type="text" name="email"></td></tr> <tr><td></td><td><input type="submit" value="Submit"></td></tr> </table> </form> </html>
2)insertdata.jsp:
<%@page import="java.sql.*,java.util.*"%> <table> <% String uname=request.getParameter("uname"); String fname=request.getParameter("fname"); String lname=request.getParameter("lname"); String pass=request.getParameter("pass"); String address=request.getParameter("address"); int contact=Integer.parseInt(request.getParameter("contact")); String email=request.getParameter("email"); try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); Statement st=con.createStatement(); int i=st.executeUpdate("insert into login(username,password,firstname,lastname,address,contactNo,email) values('"+uname+"','"+pass+"','"+fname+"','"+lname+"','"+address+"',"+contact+",'"+email+"')"); out.println("Data is successfully inserted into database."); ResultSet rs=st.executeQuery("Select * from login"); if(rs.last()){ %> <tr><td>User Name</td><td><input type="text" value="<%=rs.getString("username")%>"></td></tr> <tr><td>First Name</td><td><input type="text" value="<%=rs.getString("firstname")%>"></td></tr> <tr><td>Last Name</td><td><input type="text" value="<%=rs.getString("lastname")%>"></td></tr> <tr><td>Address</td><td><input type="text" value="<%=rs.getString("address")%>"></td></tr> <tr><td>Contact No</td><td><input type="text" value="<%=rs.getInt("contactNo")%>"></td></tr> <tr><td>Email</td><td><input type="text" value="<%=rs.getString("email")%>"></td></tr> <% } con.close(); } catch(Exception e){ System.out.println(e); } %> </table>
Ads