Solved

Query from url string?

Posted on 2001-07-05
7
222 Views
Last Modified: 2010-04-16
Hello,

  I have a problem, probably is pretty simple. I generate page using the code below. It makes links to ProbId's. These ProbId's are in my DB and when a user clicks on the link it should then generate another page using the ID number I embedded into the link. How do I make that part work?  Would I make a second java class to handle this or within this same class? Please give an example based on this example.

import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ShowLinks extends HttpServlet {
     public void doGet(HttpServletRequest request, HttpServletResponse response)
     throws IOException, ServletException {
          try {

               Class.forName("oracle.jdbc.driver.OracleDriver");
               String url = "jdbc:oracle:thin:@site:1521:webSite";
               Connection con = DriverManager.getConnection(url, "SYS", "CHANGEME");
               Statement stmt1 = con.createStatement();
               ResultSet rs1;    

               response.setContentType("text/html");
               PrintWriter out = response.getWriter();
               out.println("<html>");
               out.println("<body>");
               out.println("<head>");
               out.println("<title>Servlet demo</title>");
               out.println("</head>");
               out.println("<body>");
               out.println("<h3><Center>Show data.</Center></h3>");
               //Show data
               out.println("<table border=1>");
               // Create Header Row
               out.println("<TR>");
               out.println("<TD><B>No</B></TD>");
               out.println("<TD><B>ProbID</B></TD>");
               out.println("<TD><B>Name</B></TD>");
               out.println("<TD><B>Entry Date</B></TD>");
               out.println("<TD><B>Modify Date</B></TD>");
               out.println("</TR>");
               

               String Val = new String();
               rs1 = stmt1.executeQuery("Select NO, Probid, name, ENTRYDATE, TO_CHAR(MODIFYDATE,'MM/DD/YYYY')From tbl_1 ");

               while(rs1.next())
               {
                    out.println("<TR>");
                    Val = rs1.getString(1);
                    out.println("     <TD>" + Val + "</TD>");
                    Val = rs1.getString(2);
                    out.println("     <TD><a href='www.site.com/?ID='" + Val + ">" + Val + "</a></TD>");
                    Val = rs1.getString(3);
                    out.println("     <TD>" + Val + "</TD>");
                    Val = rs1.getString(4);
                    out.println("     <TD>" + Val + "</TD>");
                    Val = rs1.getString(5);
                    out.println("     <TD>" + Val + "</TD>");
                    Val = rs1.getString(6);
                    out.println("     <TD>" + Val + "</TD>");
                    Val = rs1.getString(7);
                    out.println("     <TD>" + Val + "</TD>");
                    Val = rs1.getString(8);
                    out.println("     <TD>" + Val + "</TD>");
                    out.println("</TR>");
                  }
               out.println("</table>");
               out.println("</body>");
               out.println("</html>");
          }
          catch (IOException e)
          {
               System.out.println(e);
          }
          catch (SQLException e)
          {
                       e.printStackTrace();
          }
          catch (Exception e)
          {
                       e.printStackTrace();
          }    
     }
}
0
Comment
Question by:MrError
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6258076
> Would I make a second java class to handle this or
> within this same class?

It depends on what the second page is doing/displaying.
It can be implemented as either depending on your needs, but from the sounds of it a second servlet would be best.
This servlet would get the ID value from the servlet context, make the required database query and output the results.
If you were doing it in the same servlet then you would check for the ID value in the context, and if it is present then handle as I described above, and if it was not prenet then generate the list as you have already done.
0
 

Author Comment

by:MrError
ID: 6258095
Objects,

Yes I think a second class would be excellent. The info would be more details about the problem that occured. How it was resolved who handled it etc... Could you should how this second class would look like? It would have a very similiar table appearance like the first page. The part I am most confused by is how this second class will use the url which has the problem Id contained within it for the Sql.

-MrError
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 6258109
You can just use request.getParameter("ID") to retrieve the ID value, and the rest is along the lines of what you've already done in your existing servlet.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:MrError
ID: 6258183
So would I put inside the links a call to the second servlet say it is called Getlink... then the first class would make the links like this:

out.println("<TD><a href='www.site.com/GetLink?ID='" + Val + ">" + Val + "</a></TD>");

This then calls GetLink servlet? Does this look right?

GetServlet uses the ID like this:

import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class GetLink extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {
         try {

           Class.forName("oracle.jdbc.driver.OracleDriver");
              String url = "jdbc:oracle:thin:@site:1521:webSite";
              Connection con = DriverManager.getConnection(url, "SYS", "CHANGEME");
              Statement stmt1 = con.createStatement();
              ResultSet rs1;    

              response.setContentType("text/html");
              PrintWriter out = response.getWriter();
              out.println("<html>");
              out.println("<body>");
              out.println("<head>");
              out.println("<title>Servlet demo page2</title>");
              out.println("</head>");
              out.println("<body>");
              out.println("<h3><Center>Show detail</Center></h3>");
              //Show data
              out.println("<table border=1>");
              // Create Header Row
              out.println("<TR>");
              out.println("<TD><B>ProbID</B></TD>");
              out.println("<TD><B>Name</B></TD>");
              out.println("<TD><B>Desc</B></TD>");
              out.println("</TR>");
             
          PId = request.getParameter("ID");

              String Val = new String();
              rs1 = stmt1.executeQuery("Select Probid, fixedby, desc From tbl_2 where ProbId ="+ PId);

              while(rs1.next())
              {
                   out.println("<TR>");
                   Val = rs1.getString(1);
                   out.println("     <TD>" + Val + "</TD>");
                   Val = rs1.getString(2);
             out.println("     <TD>" + Val + "</TD>");
                   Val = rs1.getString(3);
                   out.println("     <TD>" + Val + "</TD>");
                   out.println("</TR>");
                 }
              out.println("</table>");
              out.println("</body>");
              out.println("</html>");
         }
         catch (IOException e)
         {
              System.out.println(e);
         }
         catch (SQLException e)
         {
                      e.printStackTrace();
         }
         catch (Exception e)
         {
                      e.printStackTrace();
         }    
    }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 6258193
Looks about right to me :)
0
 

Author Comment

by:MrError
ID: 6258238
ok.. I have my second page being called now. Getlink shows up, the string was slightly wrong but this is correct:

out.println("     <TD><a href='http://www.site.com:8080/GetLink?ID=" +Val+ "'>" +Val+ "</a></TD>");

thank you for the help.

0
 
LVL 92

Expert Comment

by:objects
ID: 6258254
No worries.
Thanks for the points :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Glassfish admin console not working 1 88
running on tomcat not jboss eap 7.0 3 52
dao vs facade design patterns 2 68
JDBC performance 8 33
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question