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 = "jdbc
dbc:" + 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);
}
}