9lessons programming blog
Loading Search
9lessons Bring Friends
Wall Script
Follow Me:
Sunday, February 14, 2010

Live Availability Checking with Java.

Some days I had posted an article about User name live availability checking with PHP and jquery. In this post my brother Ravi Tamada explained the same with Java technologies like JSP and servelts using MySQL database.

Live Availability Checking with Java.


Download Script     Live Demo

About Author
Ravi Tamada
Computer science engineering, Final Year
India, Twitter Profile @ravi8x
( ravi8x[at]gmail.com )

You just download the script copy and paste the directory into Tomcat Webapps directory
Database
MySQL users table columns uid, uname, passcode.
CREATE TABLE users
(
uid INT PRIMARY KEY AUTO_INCREMENT,
uname VARCHAR(50) UNIQUE,
passcode VARCHAR(50)
);

index.jsp
$('#uname').change(function(){} - uname is the ID of the input. Using $("#uname").val() calling input field value. First checking the value string length max 3 (uname.length > 3)
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(".uname").change(function()
{
var uname = $(this).val();
if(uname.length > 3)
{
$(".status").html("Checking availability...");
$.ajax
({
type: "POST",
url: "check",
data: "uname="+ uname,
success: function(msg)
{
$(".status").ajaxComplete(function(event, request, settings)
{
$(".status").html(msg);
});
}
});
}

else
{
$(".status").html("username shold be 3 chars");
}

});
});

</script>
<div>
<label>user name :</label>
<input type="text" class="uname"/><span class="status"></span>
</div>

check.java
Contains java code. Download link Mysql Java Connector Jar file
import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class check extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try
{
// 9lessons is my database name
String connectionURL = "jdbc:mysql://localhost:3306/9lessons";
Connection connection = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
String uname = request.getParameter("uname");
PreparedStatement ps = connection.prepareStatement("select uname from USERS where uname=?");
ps.setString(1,uname);
ResultSet rs = ps.executeQuery();

if (!rs.next())
{
out.println("<b>"+uname+"</b> is avaliable");
}
else
{
out.println("<font color=red><b>"+uname+"</b> is already in use</font>");
}
out.println();
}
catch (Exception ex)
{
out.println("Error ->" + ex.getMessage());
}
finally
{
out.close();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request, response);
}
}
Comments
{ 16 comments }
Anonymous said...

nice!!!

Karthik said...

Very useful site !!
Please include Google custom search in ur website.
(google.com/webelements )
difficult to use blogger's default search for finding things.

sorrisosv said...

how can i prevent my page from doing the registration if the user use the username already use in the username field? tks

Yudz said...

this is nice. i was looking for something like this. however i need help about some part of it.

i use xampp for apache and mysql for my php system. i have a module in java that i need to connect. the java should go to that same database to retrieve values. how is can be achieved?

also, can you provide some links to help me learn further about the mysql java connector and tomcat.

thanks
Yudz

Stevens said...

Excelent code Ravi!

one question this works very well on FireFox and Opera but not in IE, when you type any letter and hit enter key notghing appears!, any clues ??

Steven

Ravi Kumar said...

@steve

Hello Steve thanx for ur comment. In this code i write code for invoking an event only when we change the content in the TEXT FIELD, this is done when we come out from the TEXT FIELD not when hitting ENTER key. This works in firefox when hitting ENTER key, this may be fire fox feature n in opera and IE hitting enter is not working.

infopediaonlinehere said...

this is an interesting script...i never thought there is a way in java to do this...thank you ravi

demo said...

is this script well secured .i hope its not prone to hackers.

demo said...

i want to know if its possible to insert an image button where you have it available

Diaa Kasem said...

I like the post .. thank you ..

Anonymous said...

Nice...but can't get it to work :-(
Copied all files into myapps
Engineers explain things so other engineers can understand :-)

Great I'm sure !
thx

Anonymous said...

nice article but not work completely.when the error displayed the cursor not moved to the next field...
Best luck

Anonymous said...

please help me... its urgent... the code is working fine.. but its posting the form even if the username already existed... i want to stop this please help me how should i do this.....

How are you Hacher! said...

good one,
very useful in my project

Anonymous said...

please help me. it urgent .this code is working but i need to disable the submit button when the user i already exist?

Anonymous said...

it's not working m getting error..
n.extend.ajax jquery.min.js:19
(anonymous function) register1.jsp:19
n.event.handle jquery.min.js:19
(anonymous function)

Post a Comment