Tom
Tom

Reputation: 771

servlet oracle database connection example

I am using windows 8.1 64 bit OS, oracle 12c database and netbeans ide.i created a java web project named as WebApplication1 in my netbeans IDE. in my database I created a user C##abc and password abc having database SID is orcl. In my database I created one table named as city with column name address and inserted one data 'xyz' to the table city. I run the project and in d browser after writing city in text box I am receiving the message- Servlet NewServlet at /WebApplication1 but it should display xyz. Where i am wrong?

I added 2 jar flies ojdbc14.jar and ojdbc_14.jar files.

My new.jsp

<form action="NewServlet" method="post">
<font face="verdana" size="2">
Enter Table Name :<input type="text" name="table"> 
                   <input type="submit" value="Display">
</font>
</form>

NewServlet.java

package p;

import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.System.out;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class NewServlet extends HttpServlet {


/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet NewServlet</title>");            
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet NewServlet at " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse  response)
        throws ServletException, IOException {
    String tb=request.getParameter("table");     

    try
    {
         Class.forName("oracle.jdbc.driver.OracleDriver");
         Connection
   con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","c##abc","abc");
         Statement st=con.createStatement();
      out.println("connection established successfully...!!");     

         ResultSet rs=st.executeQuery("Select * from "+tb);


             while(rs.next())
             {
          out.println(rs);              }

    }
    catch (ClassNotFoundException | SQLException ex){
       out.println(ex);
    }

Upvotes: 1

Views: 5433

Answers (1)

Alex Poole
Alex Poole

Reputation: 191245

Within your doPost() method, the out you are using is System.out, which you have imported (as static). In processRequest() you override that with:

PrintWriter out = response.getWriter()

... which is a bit confusing and I'd choose a different variable name. But you aren't doing that in doPost(). So nothing you write in that method with println is doing to the browser via the HttpServletResponse, it's just going to the JVM console.

From the code you posted it isn't obvious how processRequest() then gets called - which it must be for you to see the output you get. Either you haven't shown a call to doGet()/processRequest() from that method, or the servlet handling is dropping back to doGet() automatically because the response hasn't been initiated, or it's redirecting back to that automatically after an error condition.

Even with a local variable for the response.getWriter() you would need to send proper HTML, not just the plain non-tag messages you have now, otherwise you'd just get a blank page.

Upvotes: 2

Related Questions