I am now trying to create a student registration page
with servlet. But I could not figure out the
following:
"When adding a course, the system checks whether the
class is in database already. If yes, the system gives
an error message".
I donot know how to make the system check the
database.
any help is greatly appreciated.
the following is my current servlet:
import java.sql.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Student extends HttpServlet {
public void doPost (HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
String studentid = req.getParameter("ID");
String coursenumber = req.getParameter("COURSENO");
res.setContentType("text/html");
PrintWriter out=res.getWriter();
Connection con=null;
try {
Class.forName
("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection
("jdbc:oracle:thin:@bonsai.ite.gmu.edu:1521:ite",
"****","****");
con.setAutoCommit(true);
Statement stmt = con.createStatement ();
stmt.executeUpdate("insert into registration values
('"+studentid+"','"+coursenumber+"')");
out.println("Course added! Thanks for using this
system!");
}
catch (Exception e) {
try {
con.rollback();
}
catch (SQLException ignored) {}
out.println("Process failed. Please contact technical
support.");
}
finally {
try {
if(con!=null) con.close();
}
catch (SQLException ignored) {}
}
}
}