Hi all, I hope my thread is posted in the right place.
Actually, I am trying to use JSP and ajax to check if record exists in the DB. However, I got a problem that I struggle with it almost half day.

My code:
Java Code:
<html>
<body>

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code

function checkRecordExits(sql){
	var ajaxRequest;  // The variable that makes Ajax possible!
	var result = false;
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			} // end catch
		} // end catch
	} // end catch

	var name = document.getElementById('name').value;
	var queryString = "?queryString=" + sql + "'" + name + "'";
	ajaxRequest.open("GET", "checkRecords.jsp" + queryString, true);
	ajaxRequest.send(null); 

	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var resultText = ajaxRequest.responseText;
			var ajaxDisplay = document.getElementById('ajaxDiv');

			if(resultText.match("exists")) {
                                ajaxDisplay.innerHTML = "Record exists"
				result = true;
			} // end if
		} // end if
	} // end function

        return result;
} // end checkRecordExits

function test(sql) {
	if(checkRecordExits(sql)) {
		alert("Record exists");
	} else {
		alert("Record does not exist");
	} // end else
} // end test

//-->
</script>

<form name='myForm'>
Name: <input type='text' id='name' /> <br />
<br />
<input type='button' onclick='test("SELECT * FROM people WHERE name = ")' value='Button' />
</form>
<div id='ajaxDiv'>Your result will display here</div>
</body>
</html>
And, the checkRecords.jsp
Java Code:
<%@ page language="Java" import="java.sql.*" %>
<%
	
	String queryString = request.getParameter("queryString"); // get the parameter
	
	// JDBC driver name and database URL                              
	String JDBC_DRIVER = "com.mysql.jdbc.Driver";        
	String DATABASE_URL = "jdbc:mysql://localhost/test";
	Connection connection = null; // manages connection
	Statement statement = null; // query statement
	
	// connect to database books and query database
    try {
        Class.forName( JDBC_DRIVER ); // load database driver class

        // establish connection to database
        connection = 
			DriverManager.getConnection( DATABASE_URL, "root", "root" );

        // create Statement for querying database
        statement = connection.createStatement();
         
        // query database
        ResultSet resultSet = statement.executeQuery(queryString);
        
		if(resultSet.first()) {
			out.print("exists");
		} // end if
    } catch ( SQLException sqlException ) {
        sqlException.printStackTrace();
        System.exit( 1 );
    } catch ( ClassNotFoundException classNotFound ) {
        classNotFound.printStackTrace();            
        System.exit( 1 );
    } finally {                                                             
        try {                                                          
            statement.close();                                      
            connection.close();                                     
        } catch ( Exception exception ) {                                                          
            exception.printStackTrace();                                     
            System.exit( 1 );                                       
        } // end catch                                             
    } // end finally
%>
I expected both alert() and the text below the button will display "Record exists" if record exists. But, I got the text displays "Record exists" but alert() displays "Record does not exist".
I hope someone can help me.

Thank you.