In this program, we will input data from webpage and then store it into database using JDBC and Servlet.
For example: please see below screenshots:
Data successfully inserted in the database table.
Here first we will design web page (LoginScreen.html) where we insert the fields:
For example: please see below screenshots:
Data successfully inserted in the database table.
Here first we will design web page (LoginScreen.html) where we insert the fields:
<form method="post"
action="Login">
UserName: <input name="userName"
type="text" required="required"
placeholder="userName"/>
Enter Password: <input name="password"
type="password" required="required"
placeholder="passWord"/>
<!-- ReEnter Password: <input name="repass"
type="password" required="required"
placeholder="passWord"/>-->
Email Id: <input name="email"
type="email" required="required"
/>
<input type="submit"
name="submit" />
</form>
Change the welcome file in WEB.xml , so when servlet start, it will pick LoginScreen.html file.
<welcome-file-list>
<welcome-file>LoginScreen.html</welcome-file>
</welcome-file-list>
And all the logic for inserting data in DB will come in doPOST method of HTTPServlet. We will override this method in our class.
String
userName=request.getParameter("userName");
String
pass=request.getParameter("password");
String
toemail=request.getParameter("email");
response.setContentType("text/html");
PrintWriter
writer=response.getWriter();
try {
Class.forName("org.postgresql.Driver");
Connection
c=DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "oracle");
String
ins_new_user="Insert into
new_user(user_id,sys_creation_date,sys_update_date,user_status,user_name,passwd,email)
values"
+
"(nextval('user_id_1sq'),current_date,null,'A','"+userName+"','"+pass+"','"+toemail+"');";
PreparedStatement
pstmt=c.prepareStatement(ins_new_user);
pstmt.execute();
writer.println("Hi "+userName+ " Your data is
created!");
}
catch (SQLException e) {
// TODO Auto-generated
catch block
e.printStackTrace();
}
catch
(ClassNotFoundException e) {
// TODO Auto-generated
catch block
e.printStackTrace();
}
No comments:
Post a Comment