Hi , i have done a project in servlet , i didn't get any error in the coding and console page . My problem is that datas are not added to the database. Below are my files. What i'm doing wrong ?
StudentRegistration.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 bgcolor="#ccffff">
<center><h1>"Welcome to Student Registration Form"</h1></center>
<form method="post" action="user">
<table>
<tr>
<td> Name :<input type="text" name="fn" id="fn"></td>
</tr>
<tr>
<td> USN : <input type="text" name="usn" id="usn"></td>
</tr>
<tr>
<td> DOB : <input type="text" name="dob"></td>
</tr>
<tr>
<td> E.ID:<input type="text" name="email"></td>
</tr>
<tr>
<td> M.NO:<input type="text" name="mob"></td>
</tr>
<tr>
<td>domain: <input type="text" name="branch"></td>
</tr>
<tr>
<td>10th : <input type="text" name="sslc"></td>
</tr>
<tr>
<td>12th : <input type="text" name="puc"></td>
</tr>
<tr>
<td>degree : <input type="text" name="deg"></td>
</tr>
<tr>
<td>BACKLOGS:<input type="text" name="back"></td>
</tr>
<tr>
<td><center>SUBMIT :<input type="submit"></center></td>
</tr>
</table>
</form>
</body>
</html>
StudentRegistrationServlet.java
package Praveena.Shanthala;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
public class StudentRegistrationServlet extends javax.servlet.http.HttpServlet
{
private PreparedStatement pstmt;
private ResultSet rs =null;
private Connection con;
@Override
public void init() throws ServletException {
String user="root";
String password="root";
String url="jdbc:mysql://localhost";
// String sql="select NAME from project.testdb where fn=? ,usn=? ,dob=? ,email=?, mob=? ,branch=? ,sslc=?, puc=? ,deg=? and back=?";
String sql="insert into project.testdb(fName,lName)" + "values(?,?)";
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("--------------connection----------------");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con=DriverManager.getConnection(url,user,password);
System.out.println("----------establish-----------");
} catch (SQLException e) {
e.printStackTrace();
}
try {
pstmt=con.prepareStatement(sql);
System.out.println("--------prepare----------------");
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void destroy() {
if(con!=null)
{
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String NAME=request.getParameter("fn");
System.out.println("------name----------"+NAME);
String USN=request.getParameter("usn");
System.out.println("------name----------"+USN);
try {
pstmt.setString(1,"NAME");
pstmt.setString(2,"USN");
int rs = pstmt.executeUpdate();
System.out.println("no of rows affected"+rs);
if(rs>0)
{
response.getWriter().println("sucess"+NAME);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>
StudentRegistrationProject</display-name>
<servlet>
<servlet-name>StudentRegistrationServlet</servlet-name>
<servlet-class>
Praveena.Shanthala.StudentRegistrationServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>PlacementCell</servlet-name>
<servlet-class>
Praveena.Shanthala.PlacementCell</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StudentRegistrationServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PlacementCell</servlet-name>
<url-pattern>/user</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Welcome.html</welcome-file>
<welcome-file>StudentRegistration.html</welcome-file>
<welcome-file>PlacementCell.html</welcome-file>
<welcome-file>Error.html</welcome-file>
</welcome-file-list>
</web-app>