//RQ411117422011 <%@ page language="Java" import="java.sql.

*" %> <% // Define a class holding the functions we will use in the main body of the co de. class PageFunctions extends Object { public void example_query (Connection conn, String deptno, javax.servlet.jsp .JspWriter out) throws Exception { out.println("Return employees for department " + deptno + ".<br />\n"); // Prepare a query containing a bind variable. String sql = "SELECT * FROM emp WHERE deptno = ? ORDER BY empno"; PreparedStatement stmt = conn.prepareStatement(sql); // Bind the value into the prepared statement. stmt.setInt(1, new Integer(deptno).intValue()); // Execute the completed statement. ResultSet rs = stmt.executeQuery(); while (rs.next()) { // Do something with the data out.println("empno=" + rs.getString("empno")); out.println(" ename=" + rs.getString("ename")); out.println(" job=" + rs.getString("job")); out.println(" mgr=" + rs.getString("mgr")); out.println(" hiredate=" + rs.getString("hiredate")); out.println(" sal=" + rs.getString("sal")); out.println(" comm=" + rs.getString("comm")); out.println(" deptno=" + rs.getString("deptno") + "<br />\n"); } stmt.close(); stmt = null; } public void example_insert (Connection conn, String deptno, javax.servlet.js p.JspWriter out) throws Exception { out.println("<br />Insert a new employee.<br />\n"); // Prepare an insert statement containing bind variables. String sql = "INSERT INTO emp (empno, ename, job, deptno) VALUES (?, ?, ?, ?)"; PreparedStatement stmt = conn.prepareStatement(sql); // Bind the value into the prepared statement. int empno = 9999; String ename = "HALL"; String job = "DBA"; stmt.setInt(1, empno); stmt.setString(2, ename); stmt.setString(3, job); stmt.setInt(4, new Integer(deptno).intValue()); // Execute the completed statement. int res = stmt.executeUpdate(); stmt.close(); stmt = null;

int res = stmt. ename). job = ? WHERE empno = ?". javax.jsp.println("<br />Update an existing employee. out. try { // Instantiate the functions class. PreparedStatement stmt = conn.servlet.println("Employee deleted sucessfully.prepareStatement(sql). job). stmt = null. stmt.<br />\n").close(). } } // Start the main body of the code. String deptno = request. empno). PageFunctions funcs = new PageFunctions().JspWriter out ) throws Exception { out. int empno = 9999.getParameter("deptno"). stmt.setInt(1.println("Employee updated sucessfully.setString(2.setInt(3.executeUpdate().out. String ename = "TIM_HALL".JspWriter out ) throws Exception { out. } public void example_update (Connection conn. // Execute the completed statement. int res = stmt.prepareStatement(sql).<br />\n"). // Bind the value into the prepared statement. // Accept a parameter called "deptno" from a form or the query string. stmt. // Execute the completed statement.<br />\n"). int empno = 9999. // Prepare a delete statement containing bind variables. empno). String sql = "DELETE FROM emp WHERE empno = ?". } public void example_delete (Connection conn. stmt = null.jsp. stmt.println("Employee inserted sucessfully. javax.<br />\n"). stmt.servlet. // Prepare an update statement containing bind variables.setString(1. out.executeUpdate().close(). String sql = "UPDATE emp SET ename = ?.<br />\n"). // Bind the value into the prepared statement. String job = "DBA/DEV". stmt.println("<br />Delete an existing employee. // Default the value if it is not present. . PreparedStatement stmt = conn.

} catch (Exception ex) { out. funcs. out).example_update(conn.OracleDriver()).<br /><br />\n").example_insert(conn. // Connect to the SCOTT schema of the DB10G database. funcs. conn = null.example_query(conn. conn. out).getLocalizedMessage() + "<br><br>\n"). Connection conn = DriverManager.println("<br />Disconnected sucessfully.<br /><br />\n"). funcs.example_query(conn.println(" Error: " + ex.registerDriver(new oracle.println("Connected sucessfully. out).example_delete(conn. DriverManager. deptno. out). out). out. deptno. funcs. out). deptno.jdbc.example_query(conn. funcs.if (deptno == null) deptno = "10".getConnection("jdbc:oracle:oci:@DB10G".close().driver. deptno. "SCO TT".example_query(conn. funcs. "TIGER"). deptno. out. funcs. out). } %> .