Autocomplete using jquery and Mysql Database



Requirement:

Required javascript and css file Download Here

Required jsp files see below code

update.jsp

<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script src="jquery.autocomplete.js"></script>
<script language="javascript" type="text/javascript">

jQuery(function(){
$("#country").autocomplete("reg_update.jsp");
});
</script>
<title>Ganesh Rengarajan</title>
</head>
<body onLoad="show_clock()">
<table align="center" border="0" width=" 60%" cellspacing="6">
<tr>
<td >Select Name</td>
<td><input name="appno" id="country" name="country" size="20" ></td>
</tr>
</table>
</body>
</html>


reg_update.jsp

<%@page import="java.sql.*"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%

try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/path","root","root");
Statement s1=con.createStatement();
Statement st1=con.createStatement();
ResultSet rs=st1.executeQuery("select * from jsample");
String product="";
ArrayList aa=new ArrayList();
while(rs.next())
{
aa.add(rs.getString("name"));
}
int cnt=1;
String query = (String)request.getParameter("q");

for(int i=0;i<aa.size();i++)
{
String temp=(String) aa.get(i);

if(temp.toUpperCase().startsWith(query.toUpperCase()))
{
out.print(temp+"\n");
if(cnt>=10)
break;
cnt++;
}
}

}
catch(Exception ex)
{
System.out.println("error"+ex);
}

%>

2 comments:

  1. Juliana said...:

    String query = (String)request.getParameter("q"); que esta haciendo en esta linea?

  1. Anonymous said...:

    Thanks a lot , It worked

Post a Comment

 
java4practices © 2013 | Designed by Ganesh Rengarajan