For developing a registration form, you have to have a table withinside the database. You can write the database logic in the JSP documents, but isolating it from the JSP page is a better approach. Here, we're going to use DAO, Factory Method, DTO, and Singleton design patterns. There are many files:
In this example, we're using the Oracle10g database to connect to the database. Let's first create the table withinside the Oracle database:
CREATE TABLE "USER432" ( "NAME" VARCHAR2(4000), "EMAIL" VARCHAR2(4000), "PASS" VARCHAR2(4000) ) /
We have generated the table identity user432 here.
index.jsp
We are having the most effective three fields here, to make the concept clear and simplify the flow of the application. You will have different fields additionally like country, interest, etc. in step with your requirement.
<form action="process.jsp"> <input type="text" name="uname" value="Name..." onclick="this.value=''"/><br/> <input type="text" name="uemail" value="Email ID..." onclick="this.value=''"/><br/> <input type="password" name="upass" value="Password..." onclick="this.value=''"/><br/> <input type="submit" value="register"/> </form>
process.jsp
This JSP document consists of all the incoming values to an object of bean class that is handed as an issue withinside the check-in method of the RegisterDao class.
<%@page import="bean.RegisterDao"%> <jsp:useBean id="obj" class="bean.User"/> <jsp:setProperty property="*" name="obj"/> <% int status=RegisterDao.register(obj); if(status>0) out.print("You are successfully registered"); %>
User.java
It is the bean class that has 3 properties: uname, uemail and upass with its setter and getter ways.
package bean; public class User { private String uname,upass,uemail; public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpass() { return upass; } public void setUpass(String upass) { this.upass = upass; } public String getUemail() { return uemail; } public void setUemail(String uemail) { this.uemail = uemail; } }
Provider.java
This interface consists of four constants that can differ from database to database.
package bean; public interface Provider { String DRIVER="oracle.jdbc.driver.OracleDriver"; String CONNECTION_URL="jdbc:oracle:thin:@localhost:1521:xe"; String USERNAME="system"; String PASSWORD="oracle"; }
ConnectionProvider.java
This class is in charge of returning the item of Connection. Here, the driver class is loaded only once and the connection object gets memory only once.
package bean; import java.sql.*; import static bean.Provider.*; public class ConnectionProvider { private static Connection con=null; static{ try{ Class.forName(DRIVER); con=DriverManager.getConnection(CONNECTION_URL,USERNAME,PASSWORD); }catch(Exception e){} } public static Connection getCon(){ return con; } }
RegisterDao.java
This class puts the values of the bean elements into the database.
package bean; import java.sql.*; public class RegisterDao { public static int register(User u){ int status=0; try{ Connection con=ConnectionProvider.getCon(); PreparedStatement ps=con.prepareStatement("insert into user432 values(?,?,?)"); ps.setString(1,u.getUname()); ps.setString(2,u.getUemail()); ps.setString(3,u.getUpass()); status=ps.executeUpdate(); }catch(Exception e){} return status; } }