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>

How to Disable Right click in the Browser




<script language=JavaScript>
<!-- http://ramsis-code.blogspot.in -->
var message="Right Click Function Disabled!";

function clickIE4(){
if (event.button==2){
alert(message);
return false;
}
}

function clickNS4(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
alert(message);
return false;
}
}
}

if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
document.onmousedown=clickIE4;
}

document.oncontextmenu=new Function("alert(message);return false;")

</script>

<script type="text/javascript">
function showKeyCode(e){
var unicode=e.keyCode? e.keyCode : e.charCode
//alert(unicode)
// block F5
if(unicode == 116){
alert("Not Authorised.");
return false;
}

// block crtl+R
if(unicode == 82 && e.ctrlKey){
alert("Not Authorised.");
return false;
}
}
</script>


<body onload="JavaScript:document.body.focus();" onkeydown="return showKeyCode(event);">
</body>