import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; import java.lang.Math; public class SunServlet3 extends HttpServlet { // The database connection Connection con; //The statement Statement stmt; //The queryString String queryString = null; public void init (ServletConfig conf) throws ServletException { super.init(conf); // ----confugure this for the site String username = "nobody"; String password = null; // the URL that will connect to TECFA's MySQL server // Syntax: jdbc:TYPE:machine:port/DB_NAME // String url = "jdbc:mysql://localhost:3306/staf2x String url = "jdbc:mysql://tecfa2.unige.ch:3306/staf2x"; //----configure END try { Class.forName ("org.gjt.mm.mysql.Driver"); //Establish Connection to the database at URL with username and password con = DriverManager.getConnection(url, username, password); System.out.println("Ok, the connection to the database is working."); } catch (Exception e) // (ClassNotFoudException and SQLException) { throw (new UnavailableException(this, "Sorry! The database didn't load!")); } } /* * service() mehtod to handle user interaction */ public void service (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); try { String login = req.getParameter ("log"); String pwd = req.getParameter ("pword"); String queryString ="SELECT * FROM teachers WHERE (Login='"+login+"') AND (Password='"+pwd+"')"; Statement stmt = con.createStatement(); ResultSet rs=stmt.executeQuery(queryString); if ((!login.equals("")) && (!pwd.equals(""))) { if (rs.next()) { String title = "English Course Marks Insertion Page"; out.println ("" + title + ""); out.println ("

" + title + "

"); out.println("
"); out.println("

"); out.println("Hello, " + login + ", you can now insert the sudents' marks by filling the follwing fields:"); out.println("

"); out.println("

Reading:     "); out.println("

Speaking: "); out.println("

Writing:     "); out.println("

Translation: "); out.println("

"); out.println("

"); out.println(""); }else { out.println("Sorry, there must be a mistake either in the login name or in the password! Please try again."); } }else { out.println("Sorry, you are required to put in both the login name and the password! Please try again."); } }catch (SQLException ex) { out.println("

");
		while (ex !=null){
		    out.println("Message: " + ex.getMessage());
		    out.println("SQLState: " + ex.getSQLState());
		    out.println("ErrorCode: " + ex.getErrorCode());
		    ex = ex.getNextException();
		    out.println("");
		}
		out.println ("

"); } out.println("


"); out.println("

SUN June 2000 "); out.println("Source Code"); out.println(""); try { con.close();} catch(Exception e) {} return; } }