0% found this document useful (0 votes)
911 views

Servlet Program To Insert Student Info in Table

This servlet program allows students to submit their name, roll number, and marks through an HTML form. The servlet uses JDBC to connect to a MySQL database and insert the submitted student information into a database table. It catches any exceptions during the database connection or insertion and displays error messages. Finally, it closes the database connection.

Uploaded by

Deepesh Kandpal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
911 views

Servlet Program To Insert Student Info in Table

This servlet program allows students to submit their name, roll number, and marks through an HTML form. The servlet uses JDBC to connect to a MySQL database and insert the submitted student information into a database table. It catches any exceptions during the database connection or insertion and displays error messages. Finally, it closes the database connection.

Uploaded by

Deepesh Kandpal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Servlet Program to Insert student info in table

Servlet
________________________
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import java.sql.*;
//import javax.sql.*;

public class studentinfo extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse respon


se)
throws ServletException, IOException {

String url="jdbc:MySql://localHost:3306/demo";
Connection con=null;
ResultSet rs;
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<body bgcolor=tan>");
String name=request.getParameter("name");
String roll=request.getParameter("roll");
int marks=Integer.parseInt(request.getParameter("marks"));
try
{
Class.forName("org.gjt.mm.mysql.Driver");
con = DriverManager.getConnection(url, "root", "admin");
String sql="insert into stuinfo values (?,?,?)";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, name);
pst.setString(2, roll);
pst.setInt(3, marks);
int numRowsChanged = pst.executeUpdate();
// show that the new account has been created
out.println(" Your Record has been Inserted into Database ");
out.println("<p>No. of Record updated:"+numRowsChanged);
out.println("<p><p>Thank You");
pst.close();
}
catch(ClassNotFoundException e){
out.println("Couldn't load database driver: " + e.getMessage());
}
catch(SQLException e){
out.println("SQLException caught: " + e.getMessage());
}
catch (Exception e){
out.println(e);
}
finally {
// Always close the database connection.
try {
if (con != null) con.close();
}
catch (SQLException ignored){
out.println(ignored);
}
}
out.println("</html>");
out.println("</body>");
}

HTML
_____________________
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body bgcolor="tan">
<form name="student" method= "post" action="studentinfo">
Enter the Name <input type="text" name="name" value="" /><p>
Enter Roll No <input type="text" name="roll" value="" /><p>
Enter the Marks <input type="text" name="marks" value="" /><p><p>
<p><input type="submit" value="Store Data" name="sub" />
</form>
</body>
</html>

You might also like