Solved

Query from url string?

Posted on 2001-07-05
7
221 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
  • 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
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!

 

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

Technology Partners: 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
difference of if loops 23 68
hibernate example for saving data 19 73
ejb example issues 3 47
JDBC performance 8 28
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

685 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