Hi Friend,
Try the following code:
1)register.html
<html>
<form method="POST" action="
http://localhost:8080/examples/Registration">;
<table>
<tr><td>User Id:</td><td><input type="text" name="userId" size="20"></td></tr>
<tr><td>First Name:</td><td><input type="text" name="firstname" size="20"></td></tr>
<tr><td>Last Name:</td><td><input type="text" name="lastname" size="20"></td></tr>
<tr><td>Email: </td><td><input type="text" name="email" size="20"></td></tr>
<tr><td>State: </td><td><input type="text" name="state" size="20"></td></tr>
<tr><td>City: </td><td><input type="text" name="city" size="20"></td></tr>
<tr><td>Country: </td><td> <input type="text" name="country" size="20"></td></tr>
<tr><td><input type="submit" value="Submit" name="B1"></td></tr>
</table>
</form>
</html>
2)Registration.java:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Registration extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
String connectionURL = "jdbc:
mysql://localhost:3306/test";;
Connection connection=null;
ResultSet rs;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String uId = req.getParameter("userId");
String fname = req.getParameter("firstname");
String lname = req.getParameter("lastname");
String email = req.getParameter("email");
String state = req.getParameter("state");
String city = req.getParameter("city");
String country = req.getParameter("country");
long s1 = System.currentTimeMillis()/1000;
System.out.println(s1);
long s2=0 ;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, "root", "root");
String sql = "insert into login(id,firstname,lastname,email,state,city,country) values (?,?,?,?,?,?,?)";
PreparedStatement pst = connection.prepareStatement(sql);
pst.setString(1, uId);
pst.setString(2, fname);
pst.setString(3, lname);
pst.setString(4, email);
pst.setString(5, state);
pst.setString(6, city);
pst.setString(7, country);
int numRowsChanged = pst.executeUpdate();
out.println(" Hello : ");
out.println(" '"+fname+"'");
pst.close();
s2 = System.currentTimeMillis()/1000;
System.out.println(s2);
}
catch(Exception e){
out.println(e);
}
out.println("Time taken in inserting data: "+(s2-s1) +"seconds ");
}
}
Thanks