Cbs Dbms Even Sem
Cbs Dbms Even Sem
An Autonomous Institution, Approved by AICTE, New Delhi, Affiliated to Anna University, Chennai
Accredited by NBA (AERO, AUTO, CIVIL, CSE, ECE, EEE, IT, MECH, MCTS)
Accredited by NAAC ‘A++’ Grade with CGPA of 3.69 out of 4 in Cycle 2
Valley Campus, Coimbatore – 641 032, Tamil Nadu, INDIA
Tel: +91 422 4242424 www.hicet.ac.in
Course Outcomes:
Mapping of Content beyond the Syllabus with the POs and PSOs:
Submit the data (with validation) as all the required data are inserted.
Step 3: Creation of Java Servlet program with JDBC Connection
To create a JDBC Connection steps are
1. Import all the packages
2. Register the JDBC Driver
3. Open a connection
4. Execute the query, and retrieve the result
5. Clean up the JDBC Environment
Create a separate class to create a connection of database, as it is a lame process to
writing the same code snippet in all the program. Create a .java file which returns a
Connection object.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
// Database Connection
Class.forName(dbDriver);
dbUsername,
dbPassword);
return con;
}
Step 4: To use this class method, create an object in Java Servlet program
Below program shows Servlet Class which create a connection and insert the data in
the demo table,
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
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 code.DatabaseConnection;
// Servlet Name
@WebServlet("/InsertData")
HttpServletResponse response)
throws ServletException, IOException
try {
PreparedStatement st = con
st.setInt(1, Integer.valueOf(request.getParameter("id")));
st.setString(2, request.getParameter("string"));
st.executeUpdate();
st.close();
con.close();
out.println("<html><body><b>Successfully Inserted"
+ "</b></body></html>");
catch (Exception e) {
e.printStackTrace();