• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how can i print my database data in javascript array for autocomplete search box

 
sanidhya kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what i am trying, i have created a database in mysql, i have connected it using jsp, and now i want it to display below my auto complete search box

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>

<link rel="stylesheet" href="http://code.jquery.com/ui/1.8.23/themes/base/jquery-ui.css" type="text/css" media="all" />
<link rel="stylesheet" href="http://static.jquery.com/ui/css/demo-docs-theme/ui.theme.css" type="text/css" media="all" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.8.23/jquery-ui.min.js" type="text/javascript"></script>

</head>
<body>


<% try {

Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3308/usertable","root","");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select name from userinfo");
%>
<% while(rs.next()){%>

<script type="text/javascript">

$(function() {
var availableTags=new Array(<%rs.getString(1);%>);


$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>


<%}%>


<div class="demo">

<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
</div>

</div>

<%}

catch(Exception ex){
out.println(ex);
}
%>
</body>
</html>
 
Vishal Shaw
Ranch Hand
Posts: 179
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

First of all you need to add CodeTags around your code, that makes it readable to us.
Second, you have created a database connection inside your jsp. You should not do it. Use MVC for separating your view from your business logic.

Now coming to your problem of using autocomplete feature. You can call a javascript function on a event (say KeyUp) in your view element (which implements the autocomplete feature), then call your data via ajax (assuming you are not using any framework ) and populate your autocomplete div
 
rohit chavan
Ranch Hand
Posts: 132
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UseCodeTags
 
sanidhya kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what i am trying, i have created a database in mysql, i have connected it using jsp, and now i want it to display below my auto complete search box


 
Vishal Shaw
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sanidhya please read my post
Vishal Shaw wrote: Second, you have created a database connection inside your jsp. You should not do it
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic