0

I have been trying to select type of printer on the "first" try catch method to output the "second" try catch method but it does not showing the result at all even i have selected the "first" printer selection.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import = "java.sql.*"%> 
  <!DOCTYPE html>
  <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>JSP Page</title>
    </head>
  <body>

  <%
    try{
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/xxx?serverTimezone=Singapore","xxx", "xxx");
      String SQLQuery = "SELECT * FROM testprinter";
      Statement STM = conn.createStatement();
      ResultSet RS = STM.executeQuery(SQLQuery);
  %>
  <P>Printer: 
  <select name = "testprinter1" id = "testprinter1" onchange="this.form.submit();">
      <option value = "0">Select Printer</option>
  <% while(RS.next()){ %>
      <option value = "<%=RS.getInt("PID")%>"
      <%
        if(request.getParameter("testprinter1") != null){
          if(RS.getInt("PID") == Integer.parseInt(request.getParameter("testprinter1"))){
            out.print("selected");
          }
        }
      %>        
      > 
        <%=RS.getString("Brand")%>
      </option>            
  <%  }
    } catch (Exception e1) {
      e1.printStackTrace();
    }
  %>
  </select>
  </P>

  <table class = "table table-bordered table-striped">
    <tr>
      <th>A</th>
      <th>B</th>
      <th>C</th>
      <th>D</th>
      <th>E</th>
    </tr>
    <%
      try{
        String Query = "SELECT a, b, c, d, e FROM testprinter2 WHERE PID = ?";
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/xxx?serverTimezone=Singapore","xxx", "xxx");
        PreparedStatement PS = con.prepareStatement(Query);
        PS.setString(1, request.getParameter("testprinter1"));

        ResultSet rs = PS.executeQuery();

        while(rs.next()){
    %>
    <tr>
      <td><%= rs.getString("a")%></td>
      <td><%= rs.getString("b") %></td>
      <td><%= rs.getString("c") %></td>
      <td><%= rs.getString("d") %></td>
      <td><%= rs.getString("e") %></td>
    <tr>
    <%
        }
      } catch (Exception e2) {
        e2.printStackTrace();
      }
    %>

    </body>
  </html>

Expected Output of the result:

Printer : "user select the printer" 

Table: 
  A | B | C | D | E
  a | b | c | d | e

Actual result :

Printer : "user select the printer"

table : "empty blank"
Jozef Chocholacek
  • 2,874
  • 2
  • 20
  • 25
wen
  • 1
  • 1
  • 1
    You have no form in your page. Just to select the printer in browser is not enough, you have somehow to pass this information to your code on the server - i.e. submit the form. See e.g. https://stackoverflow.com/a/4971908/3511123 – Jozef Chocholacek May 09 '19 at 10:59
  • @JozefChocholacek i see it and just added the form in my page. It works and still got things need to be improvise the table with checkbox =_= but thanks for the help :) – wen May 13 '19 at 01:52

0 Answers0