Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have to request <code>doGet</code> service method of <em>servlet</em> from web-browser (client) - <code>http://localhost:8084/DBConn</code> or <code>http://localhost:8084/webapp_context/DBConn</code>.</p> <p>Create a POJO,</p> <pre><code>public class Employee{ private Integer id; private String name; //public constructors and //setter/getter } </code></pre> <p>and change your servlet code,</p> <pre><code>public class DBConn extends HttpServlet{ @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { Connection connection = null; Statement stmt=null; ResultSet rset=null; List&lt;Employee&gt; dataList = new ArrayList&lt;Employee&gt;(); try { // Load the JDBC driver String driverName = "oracle.jdbc.driver.OracleDriver"; Class.forName(driverName); // Create a connection to the database String serverName = "localhost"; String portNumber = "1521"; String sid = "xe"; String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid; String username = "hr"; String password = "hr"; connection = DriverManager.getConnection(url, username, password); stmt = connection.createStatement(); rset = stmt.executeQuery("select * from employees"); while (rset.next()) { dataList.add(new Employee(rset.getInt("employee_id"), rset.getString("first_name")); } } catch (ClassNotFoundException e) { // Could not find the database driver } catch (SQLException e) { // Could not connect to the database } finally{ if(rs!=null){ try{ rs.close(); }catch(Exception ex) { /* */ } } if(stmt!=null){ try{ stmt.close(); }catch(Exception ex) { /* */ } } if(connection !=null){ try{ connection.close(); }catch(Exception ex) { /* */ } } } request.setAttribute("data", dataList); String strViewPage = "index.jsp"; RequestDispatcher dispatcher = request.getRequestDispatcher(strViewPage); if (dispatcher != null) { dispatcher.forward(request, response); } } } </code></pre> <p>and markup in <code>index.jsp</code>,</p> <pre><code>&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt; &lt;c:forEach var="employee" items="${data}"&gt; &lt;br/&gt; ${employee.id} ${employee.name} &lt;/c:forEach&gt; </code></pre> <p>NOTE: Never create <em>Types(Classes)</em> in <em>default package</em>. You <em>must</em> have to specify the package name for POJO and Servlet sub-class.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload