Here you can learn how to insert data to MySQL using Servlet .
SQL query to create Table.
CREATE
TABLE
`User`
(
`User_ID`
int
(10) unsigned
NOT
NULL
auto_increment,
`First_Name`
varchar
(45)
NOT
NULL
,
`Last_Name`
varchar
(45)
NOT
NULL
,
`Email_ID`
varchar
(45)
NOT
NULL
,
`User_Mobile`
varchar
(45)
NOT
NULL
,
`Password`
varchar
(45)
NOT
NULL
,
PRIMARY
KEY
(`User_ID`)
register.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div class="container">
<section id="content">
<form action="/samplepro/register">
<h1>Create an Account</h1>
<input type="text" placeholder="First Name" required="" id="FirstName" name="FirstName" />
<div>
<input type="text" placeholder="Last Name" required="" id="LastName" name="LastName" />
</div>
<div>
<input type="text" placeholder="Email_ID" required="" id="Email_ID" name="Email_ID" />
</div>
<div>
<input type="text" placeholder="Mobile No." required="" id="Mobile" name="Mobile" />
</div>
<div>
<input type="password" placeholder="Password" required="" id="password" name="password"/>
</div>
<div>
<input type="submit" value="Submit" />
<a href="#">Login</a>
</div>
</form><!-- form -->
</section><!-- content -->
</div><!-- container -->
</body>
</html>
Servlet Page: register.java
package com.pack;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.http.HTTPException;
/**
* Servlet implementation class register
*/
@WebServlet("/register")
public class register extends HttpServlet
{
private static final long serialVersionUID = 1L;
Connection con=null;
PreparedStatement ps=null;
Statement stmt=null;
ResultSet rs=null;
int flag=0;
public void init(ServletConfig config) throws HTTPException
{
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/gateschema","root","root");
System.out.println("con="+con);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void destroy()
{
try
{
ps.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
System.out.println("welcome");
PrintWriter out=response.getWriter();
String Id = null;
String FirstName=request.getParameter("FirstName");
String LastName=request.getParameter("LastName");
String Email_ID=request.getParameter("Email_ID");
String Mobile=request.getParameter("Mobile");
String password=request.getParameter("password");
HttpSession session=request.getSession();
session.setAttribute("fname", FirstName);
session.setAttribute("lname", LastName);
try
{
ps=con.prepareStatement("insert into user(User_ID, First_Name, Lasr_Name, Email_ID, User_Mobile, Password) values(?,?,?,?," + "?,?)");
ps.setString(1, null);
ps.setString(2, FirstName);
ps.setString(3, LastName);
ps.setString(4, Email_ID);
ps.setString(5, Mobile);
ps.setString(6, password);
int n=ps.executeUpdate();
flag=0;
}
catch (Exception e)
{
e.printStackTrace();
}
if(flag==1)
{
RequestDispatcher rd=request.getRequestDispatcher("/register.html");
rd.forward(request, response);
}
else
{
RequestDispatcher rd=request.getRequestDispatcher("/User");
rd.forward(request, response);
}
}
}
Servlet:User.java
package com.pack;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class User
*/
@WebServlet("/User")
public class User extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public User() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session=request.getSession(false);
String n=(String)session.getAttribute("fname");
String l=(String)session.getAttribute("lname");
out.print("Hello "+n + " " +l);
out.close();
}
catch(Exception e){System.out.println(e);}
} // TODO Auto-generated method stub
}