Your logo Here

Tuesday, June 9, 2009

AjaxSearch

by Your Name 0 comments

Tag


Share this post:
Design Float
StumbleUpon
Reddit


package com.ajaxsearch;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.*;

public class AjaxSearchServlet extends HttpServlet {

public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

String search = request.getParameter("search");

response.setContentType("text/html");
response.setHeader("Cache-Control", "no-cache");
if(!((search.trim()).equals(""))){
String searchString = getSearchResult(search);
System.out.println(searchString);
response.getWriter().write(searchString);
}
else{
System.out.println("Length of string"+("".length()));
response.getWriter().write("");
}
}

public String getSearchResult(String search) {
Connection con = null;
String url = "jdbc:mysql://10.8.100.94:3306/";
String db = "jquerydb";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
String finalSearch="";
try{
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url+db, user, pass);
try{
Statement st = con.createStatement();
ResultSet res = st.executeQuery("SELECT distinct(jquery_username) FROM jquery_users WHERE jquery_username like('"+search+"%') ORDER BY jquery_username");

while (res.next()) {
String un = res.getString("jquery_username");
finalSearch+= un+"\n";
}
con.close();

}
catch (SQLException s){
System.out.println("SQL code does not execute.");
}
}
catch (Exception e){
e.printStackTrace();
}
return finalSearch;
}

}

Comments 0 comments

My Name Here
A short description here
youremail@here.com

Subscribe feeds via e-mail
Subscribe in your preferred RSS reader

Subscribe feeds rss Recent Entries

Advertise on this site Sponsored links

Categories

Sponsored Links

My Photos on flickr

Subscribe feeds rss Recent Comments

Technorati

Technorati
My authority on technorati
Add this blog to your faves