Here is the following code i am using
package com.stardeveloper.servlets.db;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class InsertServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html"
PrintWriter out = res.getWriter();
out.print("<html><body>"
out.print("<form action=\""
out.print( req.getRequestURI() );
out.print("\" method=\"post\">"
out.print("First Name :<br>"
out.print("<input type=\"text\" name=\"first\"><br>"
out.print("<br><br><input type=\"submit\" value=\" \">"
out.print(" Insert Record"
out.print(" <input type=\"submit\" value=\" \">"
out.print(" Display Records</form>"
out.print("</body></html>"
out.close();
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html"
PrintWriter out = res.getWriter();
out.print("<html><body>"
out.print("<code><pre>"
out.println("ID\tFirst Name\n"
// receiving parameters
String first = req.getParameter("first".trim();
boolean proceed = false;
if(first != null && last != null)
if(first.length() > 0 && last.length() > 0)
proceed = true;
// connecting to database
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
PreparedStatement ps = null;
try {
Load Oracle driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// Connect to the database
String dbaddress = ("jdbcracle:thinmacha.wit.ie:1521:MACH"
Connection Conn = DriverManager.getConnection ("jdbcracle:thinmacha.wit.ie:1521:MACH", "W99402734","UBWELE"
Statement Stmt = Conn.createStatement();
String sql;
sql = "INSERT INTO interest(first_name) VALUES (john)";
ps = con.prepareStatement(sql);
stmt = con.createStatement();
// inserting records
if(proceed) {
ps.setString(1, first);
ps.executeUpdate();
}
// displaying records
rs = stmt.executeQuery("SELECT * FROM interest"
while(rs.next()) {
out.print(rs.getObject(1).toString());
out.print("\t"
out.print("\t"
out.print("\n"
}
} catch (SQLException e) {
throw new ServletException(e);
} catch (ClassNotFoundException e) {
throw new ServletException(e);
} finally {
try {
if(rs != null)
rs.close();
if(stmt != null)
stmt.close();
if(ps != null)
ps.close();
if(con != null)
con.close();
} catch (SQLException e) {}
}
out.print("</body></html>"
out.close();
}
}
package com.stardeveloper.servlets.db;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class InsertServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html"
PrintWriter out = res.getWriter();
out.print("<html><body>"
out.print("<form action=\""
out.print( req.getRequestURI() );
out.print("\" method=\"post\">"
out.print("First Name :<br>"
out.print("<input type=\"text\" name=\"first\"><br>"
out.print("<br><br><input type=\"submit\" value=\" \">"
out.print(" Insert Record"
out.print(" <input type=\"submit\" value=\" \">"
out.print(" Display Records</form>"
out.print("</body></html>"
out.close();
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html"
PrintWriter out = res.getWriter();
out.print("<html><body>"
out.print("<code><pre>"
out.println("ID\tFirst Name\n"
// receiving parameters
String first = req.getParameter("first".trim();
boolean proceed = false;
if(first != null && last != null)
if(first.length() > 0 && last.length() > 0)
proceed = true;
// connecting to database
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
PreparedStatement ps = null;
try {
Load Oracle driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// Connect to the database
String dbaddress = ("jdbcracle:thinmacha.wit.ie:1521:MACH"
Connection Conn = DriverManager.getConnection ("jdbcracle:thinmacha.wit.ie:1521:MACH", "W99402734","UBWELE"
Statement Stmt = Conn.createStatement();
String sql;
sql = "INSERT INTO interest(first_name) VALUES (john)";
ps = con.prepareStatement(sql);
stmt = con.createStatement();
// inserting records
if(proceed) {
ps.setString(1, first);
ps.executeUpdate();
}
// displaying records
rs = stmt.executeQuery("SELECT * FROM interest"
while(rs.next()) {
out.print(rs.getObject(1).toString());
out.print("\t"
out.print("\t"
out.print("\n"
}
} catch (SQLException e) {
throw new ServletException(e);
} catch (ClassNotFoundException e) {
throw new ServletException(e);
} finally {
try {
if(rs != null)
rs.close();
if(stmt != null)
stmt.close();
if(ps != null)
ps.close();
if(con != null)
con.close();
} catch (SQLException e) {}
}
out.print("</body></html>"
out.close();
}
}