Pages

Monday 31 March 2014

Some Important MYSQL commands.


Hi,




 To concat emp_sname with emp_name in the query 
ex:  select emp_id, concat(emp_sname,'(',emp_name,')') as name from emp;

 The below query used to get date with weakname.
SimpleDateFormat hdf = new SimpleDateFormat("dd/MM EEE");
ex: 28/02 fri

 To get only time from date and time field
 select tra_candidate,(select date_format(tra_at,'%H:%i:%s')) from   training_att;
Ex: Date in the format of  2014-04-01 7:27

To get first In and last out from  login report in emp wise.
select tra.tra_candidate,(select date_format(tra.tra_at,'%H:%i:%s') from training_att tra  
 where(t.tr_candidate=tra.tra_candidate) order by tra.tra_at limit 1 )as firstIn, max(date_format(tra.tra_at,'%H:%i:%s'))as lastOut
 from training_att tra,trainings t where t.tr_candidate=tra.tra_candidate and tra.tra_type='O' group by tra.tra_candidate;

To count the number of dates in the database:
ex:
 dates may be:
2014-01-01 10:00:03;
2014-01-01  03:40:43:
2014-01-02  08:06:55;
2014-01-03  22:09:48;

 result: total number of dates are:3
 query:

select count(distinct date_format(tra.tra_at,'%Y:%M:%D')) from training_att tra;

Wednesday 19 March 2014

How to print dates along with weak days


Please find the source code  below.



import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class test {

      public static void main(String[] args) {
        String[] temp=new String[2];
        String date="2014-02-20 23:20:34";
        System.out.println("selected date "+date);
        String[] splited=date.split("\\s");
        try{
        int days=15;
        SimpleDateFormat df=new SimpleDateFormat();
        Date  date1=df.parse((String)df.format(new Date()));
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        SimpleDateFormat hdf = new SimpleDateFormat("dd/MM EEE");
        String[] sa=new String[days];
        cal.add(Calendar.DAY_OF_MONTH,-1);
        for(int i=2;i<days-2;i++) {
            cal.add(Calendar.DAY_OF_MONTH,1);
            sa[i]=hdf.format(cal.getTime());
            System.out.println(sa[i]);
        }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

A simple login page by using division tag in html.

      
 Please find the source code of the login page below.

Source code:


<html>
<body>
<div style="background-color:rgb(100,255,255);width:330px;height:65px; margin-left:500px;">
            <br/>
            <h3 style="text-align:center;color:black;"><b>Login</b></h3>
            </div>
            <div style="background-color:rgb(130,168,240);width:330px;height:230px;margin-left:500px;">
                <br/><br/>
                <center>
                <form action="/login1">
                <br/>
                 <table>
                 <tr><td><font color="black"><b>UserName</b></font></td>
                     <td><input type="text" name="password"/></td></tr>                  
                   <tr><td><font color="black"><b>Password</b></font></td>
                  <td><input type="password" name="password"></td></tr>
                  <tr><td/><td> <input type="submit" value="Login"/></td></tr>   
                       </table>
                </form>
                </center>
            </div>
            </body>
            </html>

Thursday 6 March 2014

Write a java program to play music on system default music player


 Save the file with name PalyMusic.java and run the program. The music will  play now.

Source Code:

public class PlayMusic{
 
 public static void main(String args[])
    {
        String windowsMediaPlayer="\"%programfiles%\\Windows Media Player\\wmplayer.exe\"";
        String songPath="E:\\kalimba.mp3"; // mymusic is the folder name
       
        try
        {    
        Runtime.getRuntime().exec("cmd /c start "+windowsMediaPlayer+" "+songPath);
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}

Check username availability using ajax and jsp

We will see how to check the user name availability in the database with ajax,  this is some thing what we can find out at time of gmail signup.

Files Required:
  • register.jsp
  • unameverify.jsp
-------------------- ----------
register.jsp

<%@ page language="java" pageEncoding="ISO-8859-1"%>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="tcal.css" />
        <script type="text/javascript" src="tcal.js"></script>
        <script src="js/main.js" type="text/javascript"></script>
        <script type="text/javascript" src="jquery-1.10.2.min.js"></script>

        <script type="text/javascript">
                 var unameflag = false;
                 var pwordflag = false;
                 var pword2flag = false;
                  var unamevalidated = false;
                 var emailvalidated = false;
                  var xmlhttp = false;
                  try
                 {
                     //If the javascript version is greater than 5.
                     xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                 catch (e)
                 {
        //If not, then use the older active x object.
         try
         {
            //If we are using IE.
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch (E)
         {
            //Else we must be using a non-IE browser.
            xmlhttp = false;
         }
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
    {
        xmlhttp = new XMLHttpRequest();
    }
    function grabword (theelement)
{
//If there is nothing in the box, run Ajax to populate it.
    switch (theelement)
    {    case "uname":
                //do javascript validation
                validate(theelement);
                //do ajax functionality
                if(unamevalidated)
                {
                    var unametemp = document.getElementById(theelement).value;
                    if ( unametemp != "")
                    {    serverPage = "unameverify.jsp?uname="+unametemp;
                        var obj = document.getElementById("unamemsg");
                        xmlhttp.open("GET", serverPage, true);
                        xmlhttp.onreadystatechange = function()
                        {
                        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                            {
                                                                var indexu=xmlhttp.responseText.indexOf("available");
                                                                if(indexu != -1)
                                {
                                    obj.innerHTML = "<font color='green'> <img src='images//tick.gif' /><b>Valid</b> </font>";
                                    unameflag = true;
                                }
                                else
                                    obj.innerHTML = " <font color='red'><img src='images//cross4.png' />User Name already exists</font>";
                                        }
                        }
                        xmlhttp.send(null);
                    }
                    else
                    {
                        document.getElementById(theelement).innerHTML = "";
                    }
                }
                break;
        default:
        validate(theelement);
    }
}   
function validate(theelement)
{
var uname;
 switch (theelement)
    {
 case "uname":
            var unamecheck=new RegExp("^[a-zA-Z0-9]+$","g");
            uname=document.getElementById("uname").value;
            var uflag = 0;
            if(uname != "")
            {
                if(!uname.match(unamecheck))
                {
                    document.getElementById("unamemsg").innerHTML = "<font color='red'> Spaces and special charecters are not allowed</font>";
                    uflag = 1;
                     obj.innerHTML = "<font color='green'> </font>";
                   
                }
                if(uname==""||uname.length < 4||uname.length > 10)
                {
                    document.getElementById("unamemsg").innerHTML = "<font color='red'> min 4 characters and max 10 characters</font>";
                    obj.innerHTML = "<font color='green'> </font>";
                    uflag = 1;
                    //obj.innerHTML = "<font color='green'> </font>";
                }
                if(uflag == 0)
                unamevalidated = true;
            }
            break;
     }
}
function checkfun(){
    if(unameflag==false){
    // alert("exited ");
     document.getElementById("uname").focus();
     return false;
    }
    if(unamevalidated==false){
    // alert("exited ");
     document.getElementById("uname").focus();
     return false;
    }   
    else
    return true;
    }
    function setFocus(){
    document.getElementById("uname").focus();
    return false;
    }
    </script>
    </head>
    <body onload="return setFocus()">
        <div id="divpage"
            style="position: absolute; overflow: hidden; left: 350px; top: 10px; width: 300px; height: 80px; z-index: 0">
            <h2>
                <font color="#036A71">Registration Form</font>
            </h2>
        </div>
        <html:form action="/registrationform" onsubmit="return checkfun()">
            <div id="divpage"
                style="position: absolute; overflow: hidden; left: 0px; top: 70px; width: 300px; height: 360px; z-index: 0">
                <table>
                        <tr>
                        <td>
                            UserName:
                            <font color="red">*</font>
                        </td>
                        <td>
                        <input id="uname" type="text" name="uname" required
                                pattern="[a-zA-Z0-9]+" maxlength="10"
                                x-moz-errormessage="Spaces and special charecters are not allowed"
                      onfocus="if(this.value == '') document.getElementById('unamemsg').innerHTML = '';"
                                onblur="grabword(this.id)" size="20">
                            <span id="unamemsg" style="height: 20px;"> </span>
                        </td>
                    </tr>
                </table>
            </div>
        </html:form>
    </body>
</html>
------------------------
unameverify.jsp

<%@page import="java.sql.*" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page language="java" %>

<%
String uname= request.getParameter ("uname");
 try {
          Class.forName("com.mysql.jdbc.Driver");
           Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/acnmain","jbossapp","JBossAppDE");
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from registrations where re_username='"+uname+"' ");
           
if(!rs.next ())
{
// for plain text response:
// Or for XML formatted response:
        response.getWriter ().print ("available");
//System.out.println("after sending response");
}
else
{
//nothing to show
        System.out.println ("Unavailable");
        response.getWriter ().print ("User Name already exists");
}

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error in Login");
        }

%>

 ------------------------------------

Note: You must have mysql-connector.jar in the lib folder of you applications. And  place two required images related to the text (valid and already existed) with name tick.gif  and cross4.png.

Ex:
Username: User Name already exists