Thursday 1 August 2013

Play mp3 song on page load


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Play mp3 Song on page Load</title>
</head>

<body onload="playSound('horse.mp3')">
<span id="dummy"></span>
</body>
<script language="javascript" type="text/javascript">
<!--
 function playSound(soundfile) {
 document.getElementById("dummy").innerHTML="<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
 }
 -->
 </script>
</html>

<!------------------------------------------------------------------------------------->

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Play mp3 Song on page Load</title>
</head>
<Script>
function getMimeType(){
var mimeType = "application/x-mplayer2"; //default
var agt=navigator.userAgent.toLowerCase();
if (navigator.mimeTypes && agt.indexOf("windows")==-1) {
//non-IE, no-Windows
  var plugin=navigator.mimeTypes["audio/mpeg"].enabledPlugin;
  if (plugin) mimeType="audio/mpeg" //Mac/Safari & Linux/FFox
}//end no-Windows
return mimeType
}//end function getMimeType


var imagePath = 'DenaJingle.mp3';
function playSound(){
alert("hi");
 document.getElementById("songplay").innerHTML="<embed src=\""+imagePath+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" type='"+getMimeType()+"' />";
 }
</script>
<body onload="playSound()">
<span id="songplay"></span>
</body>
</html>


Tuesday 2 July 2013

How to create SqlBean class for Set and Get Query in java


public class SQLBeanTest {

public static void main(String args[]){
SQLBean testBean = new SQLBean();
try{
testBean.setClassname("net.sourceforge.jtds.jdbc.Driver");
testBean.setUrl("jdbc:jtds:sqlserver://192.168.7.128:1433/COMPANY;user=sa;password=ABCD;appName=COMPANY;progName=Ramsis");
//testBean.setClassname("com.mysql.jdbc.Driver");
//testBean.setUrl("jdbc:mysql://192.168.7.128:3306/gts;user=root;password=ABCD;");

testBean.connect();
testBean.setAutoCommit(false);
//String[][] rsResult =
testBean.getSPResultSet("{call_RTE(?,?,?,?,?,?,?)}", 1, 25);
testBean.setQuery("select * from dbo.Atm_2009");
//testBean.setQuery("select * from roadways_busstop");
testBean.execute();
testBean.commit();
testBean.close();
System.out.println("Test Complete...");
}
catch (Exception ex){
System.out.println(ex);
}

}

}


//*************************************************************************//

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;


public class SQLBean {

String pr;
String pr1;
String classname;
String url;
String username;
String password;
String query;
ResultSet rs = null;
Vector result;
Connection con;
String[] tableNames;
int[] columnTypes;
String[][] resultArray;
String temp_catch_val = "";
String strError;
Vector vcParam;
PreparedStatement pstmt = null;
public void setClassname(String classname)
{
this.classname = classname;
}

public String getClassname()
{
return this.classname;
}

public void setUrl(String url)
{
this.url = url;
}

public String getUrl()
{
return this.url;
}

public void setUsername(String username)
{
this.username = username;
}

public String getUsername()
{
return this.username;
}

public void setPassword(String password)
{
this.password = password;
}

public String getPassword()
{
return this.password;
}

public void setQuery(String query)
{
this.query = query;
}

public String getQuery()
{
return this.query;
}

public void setAutoCommit(boolean bState)
throws SQLException
{
this.con.setAutoCommit(bState);
}

public boolean getAutoCommit()
throws SQLException
{
return this.con.getAutoCommit();
}

public void commit()
throws SQLException
{
this.con.commit();
}

public void rollback()
throws SQLException
{
this.con.rollback();
}

public void close()
throws SQLException
{
if (this.con != null)
this.con.close();
}


public void connect()
throws ClassNotFoundException, SQLException
{
Class.forName(this.classname);
this.con = DriverManager.getConnection(this.url, this.username,
this.password);
}


public Vector execute()
throws SQLException
{
Statement stmt = (Statement) this.con.createStatement();

rs = stmt.executeQuery(this.query);
while(rs.next()){
System.out.println(rs.getString(3));
//System.out.println(rs.getString(5));
}
return result;
}

public String toString(){

return "result"+result;
}

}

Friday 21 June 2013

How to create a Singleton Class in java

//  Example of Singleton Pattern in java //


package com.singletondemo;


class SingletonDB {


public static volatile SingletonDB getInstance;


private SingletonDB() {

   if (getInstance != null) {


               throw (new RuntimeException("instance access only 

                            createInstance()"));


    }

}


public static SingletonDB createInstance() {


if (getInstance == null) {

synchronized (SingletonDB.class) {

if (getInstance == null) {

getInstance = new SingletonDB();

}

}

}

return getInstance;

}


}


public class Main {

public static void main(String[] args) {


SingletonDB db = SingletonDB.createInstance();

System.out.println(db);


}

}




How to Insert and Get Data In Mysql Database through ( Prepared Statement ) in java

/*-- Example of Insert Data using Prepared Statement --*/


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

class DBInsert{

public static void main(String ar[]) throws SQLException{

Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;

String sender="992003002";
String receiver="Aman Kumar";
String message="Hi testing msg";

try{
Class.forName("com.mysql.jdbc.Driver");

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","public");
if(con==null){
System.out.println("DataBase Connection Error....");
System.exit(0);
}
else{
System.out.println("DataBase Connected. ");
}

String strQry="Insert into table1(sender,receiver,message)values(?,?,?)";
ps = con.prepareStatement(strQry);
System.out.println("Ready for Insert...");
ps.setString(1,sender);
ps.setString(2,receiver);
ps.setString(3,message);
ps.executeUpdate();
System.out.println("inserted data in DB...");
ps.close();
con.close();
}
catch (SQLException e){
System.out.println(e);
}
catch (ClassNotFoundException e){
System.out.println(e);
}

}
}


/*-- Example of Select (Get) Data using Prepared Statement --*/


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBGet {

public static void main(String args[]) throws SQLException{
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","public");
if(con==null){
System.out.println("DataBase Connection Error....");
System.exit(0);
}
else{
System.out.println("DataBase Connected. \n");
}

ps=con.prepareStatement("SELECT sender,receiver, message FROM table1");
ResultSet rs1 = ps.executeQuery();
while (rs1.next()){
String sender = rs1.getString(1);
String receiver = rs1.getString(2);
String message = rs1.getString(3);
System.out.println(sender + "\t " + receiver + "\t" + message);
}
}
catch (SQLException e){
System.out.println(e);

catch (ClassNotFoundException e) {
System.out.println(e);
}

}
}


Sunday 26 May 2013

How Text Message Converted into Image format Like .BMP, .JPEG, .JPG using jsp



This code save as "img.jsp" and use it.


<%@ page import="java.io.*"
%><%@ page import="java.awt.*"
%><%@ page import="java.awt.image.*"
%><%@ page import="javax.imageio.ImageIO"
%><%@ page import="java.util.*"
%>
<%
int width=500;
int height=300;
int colSpaceDefault[]={30,60,90,95};
try{
//Color background = new Color(233,211,136);
Color background = new Color(156,250,140);
String[] chars= {"501 Mussoorie 15.30 15.35","531 New Delhi 16.15 16.35","631 Chandighar 11.30 14.35","501 Mumbai 15.30 10.35","211 Lucknow 12.30 12.45","510 Bangalore 12.30 10.10"};

Color fbl = new Color(0,0,0);
Font fnt=new Font("VERDANA",1,15);
BufferedImage cpimg =new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g = cpimg.createGraphics();

g.setColor(background);
g.fillRect(0,0,width,height);
g.setColor(fbl);
g.setFont(fnt);

int j=0;
int length = 20;

for(int i=0;i<chars.length;i++){
j=25*(i+1);
g.drawString(chars[i],10,j);
}
g.setColor(background);
response.setContentType("image/bmp");   

OutputStream strm = response.getOutputStream();
ImageIO.write(cpimg,"bmp",strm);
strm.flush();
strm.close();
return;
}catch(Exception _objEx){

}

%>


Thursday 16 May 2013

How to create a jar of java programs

How to create a jar of java programs ?
How to execute a jar ?
How to extracting a jar ?


1) Create a java program as per given below java code.
2) Compile this code as (javac CreateJar.java).
3) Run this code as (java CreateJar).

After this you have got two class files (1) AddData.class and (2) CreateJar.class.

4) Create A Menifest.txt file, In this file write Main class name.

   Main-Class: CreateJar

5) Create A folder as createJar name, Then past all three files.
   AddData.class, CreateJar.class, Menifest.txt

After complete these process, open Command prmpt and type

jar -cfmv CreateJar.jar Menifest.txt *





Commands for jar:-

For Creating Jar: 
jar -cfmv CreateJar.jar Menifest.txt *

For Xtracting Jar:
jar -xfv CreateJar.jar

For executing Jar:
java -jar CreateJar.jar


/*compile this java code and create your own jar */

import java.util.Scanner;

  class CreateJar {
    public static void main(String args[]){
    Scanner scan = new Scanner(System.in);
   
   
    System.out.println("enter value 1 :");
    int a = scan.nextInt();
   
    System.out.println("enter value 2 :");
    int b = scan.nextInt();
       AddData ad = new AddData(a,b);
       ad.adddata();
    }
}

class AddData{
    int a,b;
    public AddData(int a, int b){
         setA(a);
         setB(b);
    }
   
public int getA() {
    return a;
}

public void setA(int a) {
    this.a = a;
}

public int getB() {
    return b;
}

public void setB(int b) {
    this.b = b;
}

public void adddata(){

    int c = getA()+getB();
   
    System.out.println("Addition is : "+c);
     
}

}

How to close current tab in a browser window


<script type="text/javascript">
function closeWin()
{
var win = window.open('','_self'); /* url = "" or "about:blank"; target="_self" */
win.close();
}
</script>

<input type="button" name="CloseWin" value="CloseWin" onclick="closeWin();" ></input>
<!-------------------------------------------------------------->
<script type="text/javascript">
function close_window() {
  if (confirm("Close Window?")) {
    close();
  }
}
</script>
<a href="javascript:close_window();">close</a>
<a href="#" onclick="close_window();return false;">close</a>

<!-------------------------------------------------------------->

<script>
    function closeWindow() {
        window.open('','_parent','');
        window.close();
    }
</script>

<a href="javascript:closeWindow();">Close Window</a>

<!-------------------------------------------------------------->

<a href="blablabla" onclick="setTimeout(function(){var ww = window.open(window.location, '_self'); ww.close(); }, 1000);">
    If you click on this the window will be closed after 1000ms
</a>