<%@ page import="java.sql.*" %> Simple JSP/Oracle Query Example
Employees

<% Connection conn = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:fuju", "scott", "tiger"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM scott.emp"); //Print start of table and column headers out.println(""); out.println(""); out.println(" "); //Loop through results of query. while(rs.next()) { out.println(""); out.println(" "); out.println(" "); out.println(" "); out.println(" "); out.println(" "); out.println(""); } out.println("
IDNAMESURNAMESALARYSTARTDATE
" + rs.getString("EMPNO") + "" + rs.getString("ENAME") + "" + rs.getString("JOB") + "" + rs.getInt("SAL") + "" + rs.getString("HIREDATE") + "
"); } catch(SQLException e) { out.println("SQLException: " + e.getMessage() + "
"); while((e = e.getNextException()) != null) out.println(e.getMessage() + "
"); } catch(ClassNotFoundException e) { out.println("ClassNotFoundException: " + e.getMessage() + "
"); } finally { //Clean up resources, close the connection. if(conn != null) { try { conn.close(); } catch (Exception ignored) {} } } %>