-1

enter image description here I have this code where i try to search data in sql database but it does not show me an error message, instead it runs but the textfields (or input text) disappear. The pic shows the input text should appear between the one named "Matricula" and the one named ""Estatus" This is the part of the code:

<% Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");%>
<%@ page import="java.sql.*"%>
<div id="busqueda">

        <table id="bus">
            <section id="boton">
                <button type="Submit" name="action" value="Entrar" class="btn">Buscar</button>
            </section>
            <tr>
                <td>Matricula:</td>
                <td><input id="mat" name="matricula" value="199800" size="20"/></td>
            </tr>
            <%
                String value = request.getParameter("matricula");
                Connection conn2 = DriverManager.getConnection("jdbc:sqlserver://192.168.1.124:1433;databasename=Test1;user=sa;password=AlInfinitoYMasAlla@9701");
                Statement st2=conn2.createStatement();
                ResultSet rs2 = st2.executeQuery("select * from Alumnos where Matricul = '"+value+"'");
                if(rs2.next()){              
            %>               
            <tr>
                <td>Alumno:</td>
                <td><input type="text" id="alu" name="alumno" value="<%= rs2.getString("Nombre") %>" size="50"/></td>
            </tr>
            <% 
                }
            %>             
            <tr>
                <td>Estatus:</td>
                <td><input type="text" id="sta" name="status" value="" required size="15"/>Semestre:<input id="sem" name="semestre" value="" required size="10"/></td>
            </tr>

So the issue is that wherever i put this: "<% } %>". Anything that is between that and the java code disappears for some reason, i mean that the input text where the results should be printed just do not show at all. I´ve verified a lot on the internet, other people´s code work when it´s exactly the same. I am using netbeans, and glassfish server 4.1.1 Hope someone can help me, thanks in advance.

David

0Tommy
  • 29
  • 3
  • 1
    Try surrounding the query with a try/catch and print any error. Also try adding an else block as it's possible the query returns no results. Also, using concatenation to build the query is open to SQL Injection attack. Finally, consider separating the java code from the jsp to make it easier to unit test and debug. Search on MVC and why scriptlets are evil. – Andrew S Mar 09 '18 at 19:14
  • Hello Andrew, i did use a try/catch but it still didnt work, i also used the code in a separate file in some screens but since im new with jsp there are some things i can only do like this. Thank you for your answer, i`ll defenitely take in consideration what u just told me ;) – 0Tommy Mar 13 '18 at 19:49

1 Answers1

-1

Clearly you are not getting anything in rs2. As a result you are not going inside if block. Hence nothing is printing. Please refer What does "if (rs.next())" mean?

Vipin Nagar
  • 105
  • 1
  • 4
  • This link should be flagged as a DUP. – Roman C Mar 10 '18 at 11:19
  • I`m not getting anything but i found out the mistake is in the request.getParameter("matricula");. Because it does not retrieve the text from the input text area for some weird reason and if i try to put the query with specific info about the data in sql it actually works... like this: "ResultSet rs2 = st2.executeQuery("select * from Alumnos where Matricul = 199800");" Being "199800" the value i need to retrieve to make the search Thank you for taking the time to answer my question, hope you can help me further – 0Tommy Mar 13 '18 at 19:43