Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: "Search Form" JSP-SERVLET USING NETBEANS

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default "Search Form" JSP-SERVLET USING NETBEANS

    Hi Dear,
    I need help with my code, I have built a search form based on comparison logic drop down list "greater than, less than and equal" and user input text field where you can enter a number that will be compared to the chosen comparison logic; example I WILL ENTER "45" AND CHOOSE FROM MY DROP DOWN LIST "GREATER THAN" AND PRESS SEARCH THAT WILL DISPLAY THE RESULT OF THE SEARCH.

    ITS A MS ACCESS DATABASE, the problem is that I only get a blank formatted page without my requested fields as specified in my search.

    In my servlet I have the following SQL statements that might be the problem:

    List StudentList=new ArrayList();
    String StudentID="";
    String Relationship="";

    StudentID=request.getParameter("StudentID");



    String strQuery = "SELECT StudentID,Name FROM School where StudentID ='"+StudentID+"'";

    if(StudentID ==null && (Relationship.equals("-1"))) {
    strQuery+=" and StudentID='"+StudentID+"'";
    }
    else if(StudentID != null && (Relationship.equals("Equal")) && !(StudentID.equals(""))){
    strQuery+=" and StudentID='"+StudentID+"'";
    }
    else if(SensorID != null && (Relationship.equals("Greaterthan")) && !(SensorID.equals(""))){
    strQuery+=" and StudentID < '"+StudentID+"'";
    }
    else if(StudentID != null && (Relationship.equals("Lessthan")) && !(StudentID.equals(""))){
    strQuery+=" and StudentID > '"+StudentID+"'";
    }

    thanks for helping


  2. #2
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Search Form" JSP-SERVLET USING NETBEANS

    Where to start? Can you currently select anything at all from your database? How are you connecting?

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Search Form" JSP-SERVLET USING NETBEANS

    connected via jdbc and i can display the entire table but my problems is displaying the filtering of the search form!!!!

  4. #4
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Search Form" JSP-SERVLET USING NETBEANS

    Ok, but there is a lot of code you havent shown. Where is your statement object for example. It might be worth you printing out your sql statement to the screen, just before you call executeQuery on your statement object, so you can see it. Does your data really have all those plus symbols in the tables as your where clauses seem to suggest?

    I think you are expecting +StudentId+ to be evaluated, it wont be
    But printing out your generated statement should make this clear.

    Also are you using Statement or PreparedStatements. PreparedStatements would be cleaner.

    Also by convention, variable names should start with lower case letters.
    Last edited by wrightkevin; July 29th, 2012 at 09:43 AM.

  5. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Search Form" JSP-SERVLET USING NETBEANS

    "Where is your statement object for example. It might be worth you printing out your sql statement to the screen, just before you call executeQuery on your statement object, so you can see it. "

    Can you give an example(java code) of printing out my sql statement to the screen?

    "I think you are expecting +StudentId+ to be evaluated, it wont be"

    why do you think so? can you tell me how I can implement( code please) my idea as you seem to know what I expect from the search?

  6. #6
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Search Form" JSP-SERVLET USING NETBEANS

    Quote Originally Posted by Brane View Post
    "Where is your statement object for example. It might be worth you printing out your sql statement to the screen, just before you call executeQuery on your statement object, so you can see it. "

    Can you give an example(java code) of printing out my sql statement to the screen?

    "I think you are expecting +StudentId+ to be evaluated, it wont be"

    why do you think so? can you tell me how I can implement( code please) my idea as you seem to know what I expect from the search?
    Your code is building up a string called sqlQuery.just before you call executeQuery.(You are calling execute query right?) Do a
    System.out.println(sqlQuery);
    the output should show up in your tomcat log.
    My guess is that you will.have a where clause that says
    Where studentid = "+studentId+"
    which will return no rows, hence your blank screen

  7. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Search Form" JSP-SERVLET USING NETBEANS

    I dont want to give you the complete answer but what do you think the following would do?
    strEmp="select whatever from yourtable where empid='" + strempid + "'"
    See how tricky it is to embed data into a sql statement...

  8. #8
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Search Form" JSP-SERVLET USING NETBEANS

    Quote Originally Posted by Brane View Post
    Hi Friend,

    Could you have a look at my servlet code to see what i might be doing wrong and make the correction to the code directly. Any suggestion to the code, apply it to the code.

    string relationship is not in my database, its just a logic expression in my drop down list "equal to .... " and I do not have any sign in my database such as "+".

    package studentadminservlets;



    import java.io.PrintWriter;
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import javax.servlet.http.HttpSession;
    import java.util.*;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.sql.*;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;




    /**
    *
    * @author brane
    */
    public class ViewSearch extends HttpServlet {

    private String dbName = "Brane";
    private Connection conn = null;
    private ResultSet results = null;

    /**
    * 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
    */
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException,IOException{
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    HttpSession session = request.getSession(true);
    List StudentList=new ArrayList();
    String StudentID="";
    String Relationship="";

    StudentID=request.getParameter("StudentID");



    String strQuery = "SELECT StudentID,Name FROM School where StudentID ='"+StudentID+"'";

    if(StudentID ==null && (Relationship.equals("-1"))) {
    strQuery+=" and StudentID='"+StudentID+"'";
    }
    else if(StudentID != null && (Relationship.equals("Equal")) && !(StudentID.equals(""))){
    strQuery+=" and StudentID='"+StudentID+"'";
    }
    else if(StudentID != null && (Relationship.equals("Greaterthan")) && !(StudentID.equals(""))){
    strQuery+=" and StudentID < '"+StudentID+"'";
    }
    else if(StudentID != null && (Relationship.equals("Lessthan")) && !(StudentID.equals(""))){
    strQuery+=" and StudentID > '"+StudentID+"'";
    }




    try {

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String dbURL = "jdbcdbc:" + dbName;
    conn = DriverManager.getConnection(dbURL);

    Statement stmt = conn.createStatement();

    System.out.println("My query is: " + strQuery);
    ResultSet rs = stmt.executeQuery(strQuery);


    while(results.next())
    {
    List School=new ArrayList();
    School.add(rs.getInt(1));
    School.add(rs.getInt(2));
    School.add(rs.getString(3));
    School.add(rs.getString(4));
    StudentList.add(School);

    }


    }catch(Exception e){
    System.out.println(e);{
    System.out.println("SQL statement is not executed!");
    }

    }

    request.setAttribute("SchoolList",SchoolList);
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/viewsearch.jsp");
    dispatcher.forward(request, response);


    }

    }
    Hi,
    I'm sorry I cannot just fix your program for you. The problem with your code is that the sql string that you is building up is just wrong. Try printing it out (with a System.out.println as indicated above) or use netbeans debugger to print sqlQuery and you will realise that the where clause will not be satisfied.

    At the point where you call executequery the query string should look like one of the following:

    "select studentid, name from school where studentid = 12"

    "select studentid, name from school where studentid < 12"

    "select studentid, name from school where studentid > 12"

    If it does not look like any of those, it is wrong.

    regards,

    Kevin.

  9. #9
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Search Form" JSP-SERVLET USING NETBEANS

    [QUOTE=wrightkevin;72247]Hi,
    I'm sorry I cannot just fix your program for you. The problem with your code is that the sql string that you is building up is just wrong. Try printing it out (with a System.out.println as indicated above) or use netbeans debugger to print sqlQuery and you will realise that the where clause will not be satisfied.

    At the point where you call executequery the query string should look like one of the following:

    "select studentid, name from school where studentid = 12"

    "select studentid, name from school where studentid < 12"

    "select studentid, name from school where studentid > 12"

    If it does not look like any of those, it is wrong.

    just a final thought: have a look at this:



    java.io.PrintWriter pw = response.getWriter();

    String studentId = "12";
    String sqlString = "select studentid, name from schools where studentid ='" + studentId + "'";
    pw.println(sqlString);

  10. #10
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Search Form" JSP-SERVLET USING NETBEANS

    Hi,
    I'm sorry I cannot just fix your program for you. The problem with your code is that the sql string that you is building up is just wrong. Try printing it out (with a System.out.println as indicated above) or use netbeans debugger to print sqlQuery and you will realise that the where clause will not be satisfied.

    At the point where you call executequery the query string should look like one of the following:

    "select studentid, name from school where studentid = 12"

    "select studentid, name from school where studentid < 12"

    "select studentid, name from school where studentid > 12"

    If it does not look like any of those, it is wrong.

    regards,

    Kevin.
    Hi Kevin,

    You are just confirming what I have mentioned earlier, I know the problem is with my sql statement.
    I need a solution to my problem not theory around the problem.

    I appreciate your time spent on it.

    Regards,
    Brane

  11. #11
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Search Form" JSP-SERVLET USING NETBEANS

    Quote Originally Posted by Brane View Post
    Hi Kevin,

    You are just confirming what I have mentioned earlier, I know the problem is with my sql statement.
    I need a solution to my problem not theory around the problem.

    I appreciate your time spent on it.

    Regards,
    Brane
    Here is the logic you need:


    String studentID = "12";
    String relation = "LESS THAN";
    int nStudentId = Integer.parseInt(studentID);
    String strQuery = "select student_id, name from school where studentid ";

    if ( relation.equals("EQUALS") )
    strQuery += "= " + nStudentId ;
    else if ( relation.equals("GREATER THAN") )
    strQuery += "> " + nStudentId ;
    else if ( relation.equals("LESS THAN") )
    strQuery += "< " + nStudentId ;



    System.out.println(strQuery);

  12. #12
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Search Form" JSP-SERVLET USING NETBEANS

    Hi Kevin,

    I get the following print out, does that tell you anything apart from "SQL statement is not executed" ?

    java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.
    SQL statement is not executed!

    Cheers mate

  13. #13
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Search Form" JSP-SERVLET USING NETBEANS

    Ah, that tells me that your studentid column is some text type and not a numeric type as i had assumed. I'll get back to you on that once i've seen how to work with access char types

  14. #14
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Search Form" JSP-SERVLET USING NETBEANS

    Hi Brane,

    After a bit of playing with Access, and its been a while

    try this instead (with a val function around studentid)


    String studentID = "12";
    String relation = "LESS THAN";
    int nStudentId = Integer.parseInt(studentID);
    String strQuery = "select student_id, name from school where val(studentid) ";

    if ( relation.equals("EQUALS") )
    strQuery += "= " + nStudentId ;
    else if ( relation.equals("GREATER THAN") )
    strQuery += "> " + nStudentId ;
    else if ( relation.equals("LESS THAN") )
    strQuery += "< " + nStudentId ;

  15. #15
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Search Form" JSP-SERVLET USING NETBEANS

    Hi Kevin

    String studentID = "12";
    String relation = "LESS THAN";
    int nStudentId = Integer.parseInt(studentID);
    String strQuery = "select student_id, name from school where val(studentid) ";

    if ( relation.equals("EQUALS") )
    strQuery += "= " + nStudentId ;
    else if ( relation.equals("GREATER THAN") )
    strQuery += "> " + nStudentId ;
    else if ( relation.equals("LESS THAN") )
    strQuery += "< " + nStudentId ;
    I got the following:

    java.lang.NullPointerException
    SQL statement is not executed!

  16. #16
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Search Form" JSP-SERVLET USING NETBEANS

    Quote Originally Posted by Brane View Post
    Hi Kevin



    I got the following:

    java.lang.NullPointerException
    SQL statement is not executed!
    Hi Brane, gosh this getting messy. Can you determine which line of code
    is giving you a NullPointerException? Once I know that I should be able to help.

  17. #17
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Search Form" JSP-SERVLET USING NETBEANS

    One thing I did notice about your code... You are only selecting two columns name, and studentid
    I think, but when processing the resultset you are requesting a third and fourth column.

    i.e.
    School.add(rs.getInt(1));
    School.add(rs.getInt(2));
    School.add(rs.getString(3));
    School.add(rs.getString(4));
    Last edited by wrightkevin; July 31st, 2012 at 05:43 AM.

  18. #18
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Search Form" JSP-SERVLET USING NETBEANS

    Hi kevin,
    it is indeed not very pleasant at the moment.

    i.e.
    School.add(rs.getInt(1));
    School.add(rs.getInt(2));
    School.add(rs.getString(3));
    School.add(rs.getString(4));
    I am aware of it and i have the exact number of column, in my actual code they are all selected .

    Hi Brane, gosh this getting messy. Can you determine which line of code
    is giving you a NullPointerException? Once I know that I should be able to help.
    Reading the Stack Trace, just can find at the moment "java.lang.NullPointerException
    SQL statement is not executed!"

  19. #19
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Search Form" JSP-SERVLET USING NETBEANS

    Quote Originally Posted by Brane View Post
    Hi kevin,
    it is indeed not very pleasant at the moment.



    I am aware of it and i have the exact number of column, in my actual code they are all selected .



    Reading the Stack Trace, just can find at the moment "java.lang.NullPointerException
    SQL statement is not executed!"
    Hi Brane,

    We found your NullPointerException!!!!!

    Your loop while(results.next) should be while( rs.next() )

    Try that and let me know....

  20. #20
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Search Form" JSP-SERVLET USING NETBEANS

    Hi Brane,

    We found your NullPointerException!!!!!

    Your loop while(results.next) should be while( rs.next() )

    Try that and let me know....
    Hi Kevin,

    It seems that you found the NullPointerException as it does not appear in the stack trace anymore but my page is still no showing the result of the search.

    As usual, I just see the "No fields found for this particular search" as specified in my jsp viewsearch.jsp


    This viewsearch.jsp is referred to in the my servlet at the following code line
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/viewsearch.jsp");
    Does the following tells you anything abnormal?

    viewsearch.jsp

    <%@page contentType="text/html" pageEncoding="UTF-8"%>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <%@page import="studentadminclasses.*" %>
    <%@ page import="java.util.*" %>
    <jsp:useBean id="db" scope="page" class="studentadminclasses.DBConnector" />




    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="style.css" media="screen" />
    <title>school System</title>
    </head>

    <body>

    <div id="content">

    <div id="header">
    <img src="images/brane.jpg" alt="logo" />
    </div>





    <table align="center">

    <%

    List studentList=new ArrayList();

    studentList=(ArrayList)request.getAttribute("stude ntList");

    if(studentList!=null && studentList.size()>0 ){

    %>

    <h2 align="center">Search List</h2>

    <tr>

    <th>student ID</th>

    <th>Date and Time</th>

    <th>name </th>

    <th>grade</th>


    </tr>

    <%

    for(int i=0;i<studentList.size();i++){

    List School=(List)studentList.get(i);

    %>

    <tr>

    <td><%=School.get(1) %></td>

    <td><%=School.get(2) %></td>

    <td><%=School.get(3) %></td>

    <td><%=School.get(4) %></td>






    </tr>


    <%

    }

    }else{

    %>

    No fields found for this particular search


    <%}%>

    </table>

    </body>

    </html>

  21. #21
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Search Form" JSP-SERVLET USING NETBEANS

    Quote Originally Posted by Brane View Post
    Hi Kevin,

    It seems that you found the NullPointerException as it does not appear in the stack trace anymore but my page is still no showing the result of the search.

    As usual, I just see the "No fields found for this particular search" as specified in my jsp viewsearch.jsp


    This viewsearch.jsp is referred to in the my servlet at the following code line


    Does the following tells you anything abnormal?

    viewsearch.jsp

    <%@page contentType="text/html" pageEncoding="UTF-8"%>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <%@page import="studentadminclasses.*" %>
    <%@ page import="java.util.*" %>
    <jsp:useBean id="db" scope="page" class="studentadminclasses.DBConnector" />




    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="style.css" media="screen" />
    <title>school System</title>
    </head>

    <body>

    <div id="content">

    <div id="header">
    <img src="http://www.javaprogrammingforums.com/images/brane.jpg" alt="logo" />
    </div>





    <table align="center">

    <%

    List studentList=new ArrayList();

    studentList=(ArrayList)request.getAttribute("stude ntList");

    // Sometimes it looks like there is a space in "student List"
    // in the previous line..

    if(studentList == null){
    out.println("studentList is null, this is not good!!");
    }

    out.println("The size of the list is: " + studentList.size() );



    if(studentList!=null && studentList.size()>0 ){

    %>

    <h2 align="center">Search List</h2>

    <tr>

    <th>student ID</th>

    <th>Date and Time</th>

    <th>name </th>

    <th>grade</th>


    </tr>

    <%

    for(int i=0;i<studentList.size();i++){

    List School=(List)studentList.get(i);

    %>

    <tr>

    <td><%=School.get(1) %></td>

    <td><%=School.get(2) %></td>

    <td><%=School.get(3) %></td>

    <td><%=School.get(4) %></td>






    </tr>


    <%

    }

    }else{

    %>

    No fields found for this particular search


    <%}%>

    </table>

    </body>

    </html>

    Hi Brane,

    Either your getAttribute("studentList") is returning a null or an
    empty list is being passed from the Servlet. Can you insert the code
    I have suggested, and tell me what happens?

    I have also noticed inconsistencies between the name of the
    object you bound to the request, and the name you subsequently
    refer to it, SchollList, StudentList, getting this wrong would certainly
    cause getAttribute to return a null. Please check this.



    regards,

    Kevin.
    Last edited by wrightkevin; August 1st, 2012 at 03:49 AM.

Similar Threads

  1. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  2. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  3. Replies: 4
    Last Post: July 15th, 2011, 06:31 AM
  4. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM
  5. Replies: 4
    Last Post: August 13th, 2009, 05:54 AM

Tags for this Thread