<% // 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; out.println("Employee inserted sucessfully.<br />\n"); } public void example_update (Connection conn, javax.servlet.jsp.JspWriter out ) throws Exception { out.println("<br />Update an existing employee.<br />\n"); // Prepare an update statement containing bind variables. String sql = "UPDATE emp SET ename = ?, job = ? WHERE empno = ?"; PreparedStatement stmt = conn.prepareStatement(sql); // Bind the value into the prepared statement. int empno = 9999; String ename = "TIM_HALL"; String job = "DBA/DEV"; stmt.setString(1, ename); stmt.setString(2, job); stmt.setInt(3, empno); // Execute the completed statement. int res = stmt.executeUpdate(); stmt.close(); stmt = null; out.println("Employee updated sucessfully.<br />\n"); } public void example_delete (Connection conn, javax.servlet.jsp.JspWriter out ) throws Exception { out.println("<br />Delete an existing employee.<br />\n"); // Prepare a delete statement containing bind variables. String sql = "DELETE FROM emp WHERE empno = ?"; PreparedStatement stmt = conn.prepareStatement(sql); // Bind the value into the prepared statement. int empno = 9999; stmt.setInt(1, empno); // Execute the completed statement. int res = stmt.executeUpdate(); stmt.close(); stmt = null; out.println("Employee deleted sucessfully.<br />\n"); } } // Start the main body of the code. try { // Instantiate the functions class. PageFunctions funcs = new PageFunctions(); // Accept a parameter called "deptno" from a form or the query string. String deptno = request.getParameter("deptno"); // Default the value if it is not present. if (deptno == null) deptno = "10"; // Connect to the SCOTT schema of the DB10G database. DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); Connection conn = DriverManager.getConnection("jdbc:oracle:oci:@DB10G", "SCO TT", "TIGER"); out.println("Connected sucessfully.<br /><br />\n"); funcs.example_query(conn, deptno, out); funcs.example_insert(conn, deptno, out); funcs.example_query(conn, deptno, out); funcs.example_update(conn, out); funcs.example_query(conn, deptno, out); funcs.example_delete(conn, out); funcs.example_query(conn, deptno, out); conn.close(); conn = null; out.println("<br />Disconnected sucessfully.<br /><br />\n"); } catch (Exception ex) { out.println(" Error: " + ex.getLocalizedMessage() + "<br><br>\n"); } %>