Archive for the ‘MySQL’ Category

Ajax Search

November 14, 2008

With Ajax you can able to dynamically showing the searching results for a search item. Instead of entering a search value in the text box and submit the value after that get a search results in a list. Its an old model. Now web technologies are growing rapidly. So we move to the current trend is an important one. In Ajax Search, the implementation should be like this; type a character sequence in the text box during that time we will query the database for that search value and retrieve & showing the matching results dynamically. The below example will explain you clearly.

Example.

Database Name : Books
Table Name : BookDetails

BookId BookName Author Price
1001 Ajax Nicolas Zakas 1000
1002 Java Herbert Schildt 800
1003 JSP & Servlet James Gosling 1200
1004 C Programming Dennis Ritchie 300
1005 C++ Programming Bjarne Stroustroup 600
1006 Operating Donald 750

Action Class : BookDetails.java

File contains search logic to retrieve data from the database and send results to the client in XML format.

import java.sql.*;
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class BookDetails extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//Details of DB
String driverName="org.gjt.mm.mysql.Driver";
String url="jdbc:mysql://localhost:2345/Books";
String username="root";
String password="";
Connection con=null;
try
{
Class.forName(driverName);
con = DriverManager.getConnection(url,username,password);
}
catch( SQLException e )
{
e.printStackTrace();
}
Statement stmt = conn.createStatement();;
ResultSet res = null;
String input = request.getParameter("searchtext");
res = stmt.executeQuery("select BookName from BookDetails where BookName like '%"+input+"%'");
Sting output = "";
String result = "";
while( res.next() )
{
String bookname = res.getString("BookName ");
result = ""+bookname+"";
}
output = output + result + "";
System.out.println("Search Output is : "+output);
stmt.close();
conn.close();
response.setContentType("text/xml");
PrintWriter out = response.getWriter();
out.println(output);
out.close();
}
}

Client File : book.jsp

File used to get the input of search value and shows the matching search results.

<html>
<script src=”ajax.js” language=”javascript”></script>
<script src=”ajaxsearch.js” language=”javascript”></script>
<body>
<form action=”BookDetails” name=”book”>
Enter Search value : <input type=”text” name=”searchtext” value=”” onkeyup=”showSearchItems(this.value)”/>
</form>
<div id=”showdetails”>
</div>
</body>
<script>
function showSearchItems(val)
{
sendAjaxSearchRequest(“\BookDetails”, “showdetails”);
}
</script>
</html>

Script File : ajaxsearch.js

function sendAjaxSearchRequest(url, divid)
{
url = url + getNoCacheValue(url);
var req = getXMLHttpRequest();
req.onreadystatechange = function(){ processResponse(req, divid) };
req.open(’GET’, url, true);
req.send(null);
}

function processResponse(req, divid)
{
if (req.readyState == 4) {
if (req.status == 200) {
var response = req.responseXML;
var tags = response.getElementsByTagName(BookDetails)[0];
var result = “”;
for(i=0; i<tags.length; i++)
{
var bookdet = tags[i];
var bookname = bookdet.childNodes[0].firstChild.data
result += “<div>”+bookname+”</div>”;
}
document.getElementById(divid).innerHTML = result;
}
}
}

onkeyup – is one of the property of  html tags. method specified in this property gets fired when you type any characters in the search input box. In our example javascript method showSearchItems called and this should produce the search results for your entered search input.

Ajax Basics – https://manikandanmv.wordpress.com/2008/09/23/ajax-asynchronous-javascript-and-xml/
Ajax Example – https://manikandanmv.wordpress.com/2008/09/29/ajax-example/

Advertisements

Remote Access to MySQL database

November 5, 2008

Scenario: running a mysql server alone in a separate machine but want to connect and access the database from other machine.

Want to remotely connect to your mysql databases. ?

Yes..! its possible, Use the below command to connect your MySQL databases from remote location.

mysql -u user_name-P port_number -h host_name

user_name – name has privilege to connect remote mysql
port_number – number in which mysql is running.
host_name – remote machine name. you can also use IpAddress instead of host name.

Before execute the above command ensure that you have privileges to access that remote mysql database.If not kindly grant permissions to access and perform sql operations on that database. Lets see the command for providing permission to remote users.

grant all on *.* to username@’host_name’

*.* – enables access to all databases of the remote mysql.

you can restrict the access by changing like this.

tablename.* – set access to table level
databasename.* – set access to database level
select on *.* – set select access to all databases.

If you need more informations about grant command, go here http://dev.mysql.com/doc/refman/5.0/en/grant.html

Find MySQL version

October 10, 2008

Below are the possible ways to determine the version of mysql server.

  • display the version details when connecting to mysql server.
  • use the version variable to get the information about mysql

mysql>show variables like ‘%version%’;

  • use the version function to get the version details.

mysql>select version();

  • you can also use status command to get the version details & other informations.

mysql>status;

How to execute case insensitive query in MySQL

September 20, 2008

Most of you came across this problem when executing query. Creating tables in proper case and access the same tables in lowercase.This should return error in some OS environment.I will explain below.

create table ProductDetails(ProductName varchar(50), Version varchar(10));

select * from productdetails;

The above query will work fine when execute in mysql in windows environment. But the same is failed to process in linux environment mysql. Because most linux operating systems are case sensitive so based on that database or table names are case sensitive.

To solve this problem. Use the –lower_case_table_names parameter to start the  mysql dameon. This parameter will ensure that the mysql will process the database or table names in case insensitive.

select * from productdetails;

select * from ProductDetails;

After setting the above param. The above two queries will work fine.