aspose file tools*
The moose likes JSP and the fly likes how can i print my database data in javascript array for autocomplete search box Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "how can i print my database data in javascript array for autocomplete search box" Watch "how can i print my database data in javascript array for autocomplete search box" New topic
Author

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

sanidhya kumar
Greenhorn

Joined: Sep 05, 2012
Posts: 18
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

Joined: Aug 09, 2012
Posts: 179
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


Programming is about thinking, NOT coding
rohit chavan
Ranch Hand

Joined: Oct 08, 2010
Posts: 132

UseCodeTags
sanidhya kumar
Greenhorn

Joined: Sep 05, 2012
Posts: 18
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

Joined: Aug 09, 2012
Posts: 179
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.
 
subject: how can i print my database data in javascript array for autocomplete search box