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>

you do add an awfull lot of try-catch blocks, without checking whether a previous (required one) is completed without exception. you may want to re-think that.

also: are you working on a local server? otherwise you might get a lot of exceptions, but you just don't see them.
debugging might help you out. also, if you feel like you don't get a good answer here, the jsp forum might be worth looking at.

I think that you must add all that values in SQL Querry not just:

pstmt.setString(1,"NAME");
pstmt.setString(2,"USN");

Because you have someting like this:
email = ;
What is values for email? Try to put all values in pstmt.

What u need is an organize code :)

In establishing a connection with you database
i think this is a more better way.

The code below shows how to connect or establish a connection.
as you can see, it is organized in a way that if you miss something, you will easily notice it.

Here are the steps:
1. Create a new class.
2. Study the code below and try to analyse it.

    public Connection getConnection() {
        final String USER_NAME  ="You Username";
        final String PASSWORD = "Your Password";
        final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        final String JDBC_URL = "jdbc:mysql://localhost:3306/Your schema ";

        Connection conn = null;

        try{
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(JDBC_URL, USER_NAME, PASSWORD);
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }catch(SQLException e){
            e.printStackTrace();
        }
        return conn;
    }

}

After that you can now create your servlet.
Here's how :
1. Create a new class, and put it in a package named: "app.servlet"
You should put all ur servlet classes in an app.servlet package so that it will be easy for you to look for things.

  1. Our professor said that it is not neat to print from a servlet class. So dont do that. A servlet class should only be a servlet class, and not do anything that is not its purpose.

  2. Create another class. The class that you should create is a class that will write into your database.

    How should you write your servlet:

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String NAME=request.getParameter("fn");
    System.out.println("------name----------"+NAME);//Never do this again
    String USN=request.getParameter("usn");
    System.out.println("------name----------"+USN);//This too!
    
    NameOfYourWriteIntoDatabaseClass youNameWhatever = new NameOfYourWriteIntoDatabaseClass();
    yourNameWhatever.writeIntoDatabase(NAME,USN);//this is the part where you
    //send the value of your two string to the writer class
    

The code below shows you how to create a writer .

/* This is your new class. THe purpose of creating 
this class is to WRITE into your database.

*/

//Notice that the method accepts something?
//If you will be acquiring this type of coding
// what you should put there are things that contains
// all that you want to be written in your DB

//In this case I have 2 variables named nameIt and nameIt2
//Those variables contain the things that i want to write in my db

public void writeIntoDatabase(String NAME, String USN) {
        //remember the class that you created where I told you 
        //that it will connect to your database?
        //this is my instantiation with that.

        DatabaseManager dm = new MySQLDatabaseManager();
        Connection conn = dm.getConnection();
        StringBuilder sql = new StringBuilder(300);

        sql.append("INSERT INTO `nameodyouschema`.`nameofyourtable`(`NAME`, `USN`) VALUES(?,?);");

        String sqlUpdate = sql.toString();
        PreparedStatement pr = null;
        try{

            pr = conn.prepareStatement(sqlUpdate);

            pr.setString(1,NAME);
            pr.setString(2,USN);


            pr.executeUpdate();
            JOptionPane.showMessageDialog(null, "Succesfully new added group!");
        } catch(SQLException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "Sorry, unable to add new item!");
        } finally {
            if(pr!=null){
                try{
                    pr.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }   
            } try{
                conn.close();
            } catch(SQLException e) {
                e.printStackTrace();
            }
        }

    }

I Hope that this can help you! :)

commented: Excellent teaching post +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.