Here you will find lots of code [JAVA & JAVASCRIPT] & some links that are useful for web development and stand alone projects in java. If you need more contact me on my email.
7/6/09
6/8/09
6/2/09
6/1/09
5/30/09
Folder Hide
code and make bat
cls
@ECHO OFF
title Folder private
if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto UNLOCK
if NOT EXIST Private goto MDLOCKER
:CONFIRM
echo Are you sure you want to lock the folder(Y/N)
set/p "cho=>"
if %cho%==Y goto LOCK
if %cho%==y goto LOCK
if %cho%==n goto END
if %cho%==N goto END
echo Invalid choice.
goto CONFIRM
:LOCK
ren Private "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
echo Folder locked
goto End
:UNLOCK
echo Enter password to unlock folder
set/p "pass=>"
if NOT %pass%==1234 goto FAIL
attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" Private
echo Folder Unlocked successfully
goto End
:FAIL
echo Invalid password
goto end
:MDLOCKER
md Private
echo Private created successfully
goto End
cls
@ECHO OFF
title Folder private
if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto UNLOCK
if NOT EXIST Private goto MDLOCKER
:CONFIRM
echo Are you sure you want to lock the folder(Y/N)
set/p "cho=>"
if %cho%==Y goto LOCK
if %cho%==y goto LOCK
if %cho%==n goto END
if %cho%==N goto END
echo Invalid choice.
goto CONFIRM
:LOCK
ren Private "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
echo Folder locked
goto End
:UNLOCK
echo Enter password to unlock folder
set/p "pass=>"
if NOT %pass%==1234 goto FAIL
attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" Private
echo Folder Unlocked successfully
goto End
:FAIL
echo Invalid password
goto end
:MDLOCKER
md Private
echo Private created successfully
goto End
5/29/09
5/23/09
4 - Hand Tree
public void get_trees(String id)
{
try{
java.sql.Connection con=new model.connection.connection().getCon();
java.sql.Statement st=con.createStatement();
java.sql.ResultSet rs=st.executeQuery("select child_1,child_2,child_3,child_4 from "+tablename+" where emp_code='"+id.trim()+"'");
if(rs.next())
{
String c1=rs.getString(1).trim();
String c2=rs.getString(2).trim();
String c3=rs.getString(3).trim();
String c4=rs.getString(4).trim();
if(c1.equals("N"))
{
v.addElement("N");
}
else
{
v.addElement(c1);
}
if(c2.equals("N"))
{
v.addElement("N");
}
else
{
v.addElement(c2);
}
if(c3.equals("N"))
{
v.addElement("N");
}
else
{
v.addElement(c3);
}
if(c4.equals("N"))
{
v.addElement("N");
}
else
{
v.addElement(c4);
}
}
else
{
v.addElement("N");
v.addElement("N");
v.addElement("N");
v.addElement("N");
}
}
catch(Exception ex){JOptionPane.showMessageDialog(null,ex.toString());}
}
public Vector get_tree(String id)
{
try
{
int x=0;
get_trees(id) ;
for(int y=0;y<2;y++)
{
int size= v.size();
System.out.println("S"+size);
for(;x {
//String sp[]=v.get(x).toString().split("
");
get_trees(v.get(x).toString());
// System.out.println(sp[1]);
}
//System.out.println(tot+""+tot.size());
//System.out.println(");
}
}
catch(Exception e1)
{
System.out.print(e1+"get");
}
return v;
}
{
try{
java.sql.Connection con=new model.connection.connection().getCon();
java.sql.Statement st=con.createStatement();
java.sql.ResultSet rs=st.executeQuery("select child_1,child_2,child_3,child_4 from "+tablename+" where emp_code='"+id.trim()+"'");
if(rs.next())
{
String c1=rs.getString(1).trim();
String c2=rs.getString(2).trim();
String c3=rs.getString(3).trim();
String c4=rs.getString(4).trim();
if(c1.equals("N"))
{
v.addElement("N");
}
else
{
v.addElement(c1);
}
if(c2.equals("N"))
{
v.addElement("N");
}
else
{
v.addElement(c2);
}
if(c3.equals("N"))
{
v.addElement("N");
}
else
{
v.addElement(c3);
}
if(c4.equals("N"))
{
v.addElement("N");
}
else
{
v.addElement(c4);
}
}
else
{
v.addElement("N");
v.addElement("N");
v.addElement("N");
v.addElement("N");
}
}
catch(Exception ex){JOptionPane.showMessageDialog(null,ex.toString());}
}
public Vector get_tree(String id)
{
try
{
int x=0;
get_trees(id) ;
for(int y=0;y<2;y++)
{
int size= v.size();
System.out.println("S"+size);
for(;x
//String sp[]=v.get(x).toString().split("
");
get_trees(v.get(x).toString());
// System.out.println(sp[1]);
}
//System.out.println(tot+""+tot.size());
//System.out.println(");
}
}
catch(Exception e1)
{
System.out.print(e1+"get");
}
return v;
}
5/11/09
To Set window in the middle of screen
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class Main {
public static void main(String[] argv) throws Exception {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
JFrame window = new JFrame();
window.setSize(300,300);
int w = window.getSize().width;
int h = window.getSize().height;
int x = (dim.width - w) / 2;
int y = (dim.height - h) / 2;
window.setLocation(x, y);
window.setVisible(true);
}
}
import java.awt.Toolkit;
import javax.swing.JFrame;
public class Main {
public static void main(String[] argv) throws Exception {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
JFrame window = new JFrame();
window.setSize(300,300);
int w = window.getSize().width;
int h = window.getSize().height;
int x = (dim.width - w) / 2;
int y = (dim.height - h) / 2;
window.setLocation(x, y);
window.setVisible(true);
}
}
5/9/09
Marging 2 PDF Hello1 and Hello2 using Itext.jar
PdfReader reader = new PdfReader("Hello1.pdf");
Document document = new Document(reader.getPageSizeWithRotation(1));
PdfCopy copy = new PdfCopy(document,new FileOutputStream("HelloWorldPdfCopy123.pdf"));
document.open();
System.out.println("Tampered? " + reader.isTampered());
copy.addPage(copy.getImportedPage(reader, 1));
reader = new PdfReader("Hello2.pdf");
copy.addPage(copy.getImportedPage(reader, 1));
System.out.println("Tampered? " + reader.isTampered());
document.close();
Document document = new Document(reader.getPageSizeWithRotation(1));
PdfCopy copy = new PdfCopy(document,new FileOutputStream("HelloWorldPdfCopy123.pdf"));
document.open();
System.out.println("Tampered? " + reader.isTampered());
copy.addPage(copy.getImportedPage(reader, 1));
reader = new PdfReader("Hello2.pdf");
copy.addPage(copy.getImportedPage(reader, 1));
System.out.println("Tampered? " + reader.isTampered());
document.close();
5/2/09
To Make a PDF using Images [ iText.jar ]
Document d=new Document();
PdfWriter.getInstance(d, new FileOutputStream("op.pdf"));
d.open();
Image img1 = Image.getInstance("hellsing.jpg");
d.add(img1);
d.close();
PdfWriter.getInstance(d, new FileOutputStream("op.pdf"));
d.open();
Image img1 = Image.getInstance("hellsing.jpg");
d.add(img1);
d.close();
To start a process with ProcessBuilder
try {ProcessBuilder proc = new ProcessBuilder("notepad.exe", "testfile");
proc.start();
} catch (Exception e) {
System.out.println("Error executing notepad.");
}
To open a file in it's editor [Default]
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] a) {
try {
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
desktop.edit(new File("hellsing.jpg"));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] a) {
try {
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
desktop.edit(new File("hellsing.jpg"));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
Code for Browsing a Site when uninstalling of installing Application
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class Main {
public static void main(String[] a) {
try {
URI uri = new URI("http://www.java2s.com");
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
if (desktop != null)
desktop.browse(uri);
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (URISyntaxException use) {
use.printStackTrace();
}
}
}
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class Main {
public static void main(String[] a) {
try {
URI uri = new URI("http://www.java2s.com");
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
if (desktop != null)
desktop.browse(uri);
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (URISyntaxException use) {
use.printStackTrace();
}
}
}
4/30/09
File Zip using JAVA
pass your file names in an arry to the method.
---------------------------------------------------
public static void zip(String [] args) throws FileNotFoundException, IOException
{
String outputFile = "new.zip";
int level = 9;
int start = 1;
FileOutputStream fout = new FileOutputStream(outputFile);
ZipOutputStream zout = new ZipOutputStream(fout);
zout.setLevel(level);
for (int i = start; i < args.length; i++)
{
ZipEntry ze = new ZipEntry(args[i]);
FileInputStream fin = new FileInputStream(args[i]);
try {
System.out.println("Compressing " + args[i]);
zout.putNextEntry(ze);
for (int c = fin.read(); c != -1; c = fin.read())
{
zout.write(c);
}
}
finally{
fin.close();
}
}
zout.close();
}
---------------------------------------------------
public static void zip(String [] args) throws FileNotFoundException, IOException
{
String outputFile = "new.zip";
int level = 9;
int start = 1;
FileOutputStream fout = new FileOutputStream(outputFile);
ZipOutputStream zout = new ZipOutputStream(fout);
zout.setLevel(level);
for (int i = start; i < args.length; i++)
{
ZipEntry ze = new ZipEntry(args[i]);
FileInputStream fin = new FileInputStream(args[i]);
try {
System.out.println("Compressing " + args[i]);
zout.putNextEntry(ze);
for (int c = fin.read(); c != -1; c = fin.read())
{
zout.write(c);
}
}
finally{
fin.close();
}
}
zout.close();
}
Type 4 Connection in MySQL
Connection Class for My SQL ( With Type 4 Connection )
---------------------------------------------------------
public class Conn {
java.sql.Connection con;
public Conn()
{
try{
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl="jdbc:mysql://localhost/[database name]?user=root&password=123";
con=java.sql.DriverManager.getConnection(connectionUrl);
}
catch(Exception ex){javax.swing.JOptionPane.showMessageDialog(null, ex.toString());}
}
---------------------------------------------------------
public class Conn {
java.sql.Connection con;
public Conn()
{
try{
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl="jdbc:mysql://localhost/[database name]?user=root&password=123";
con=java.sql.DriverManager.getConnection(connectionUrl);
}
catch(Exception ex){javax.swing.JOptionPane.showMessageDialog(null, ex.toString());}
}
Getting PDF Info using iText.jar
String path="Your PDF Path"; // ----------> like C:\Books\Hello.pdf
String BookTitle = " ";
String Author = " ";
String Description = " ";
String Publisher = " ";
int TotalPage=0;
PdfReader reader=null;
reader = new PdfReader( path);
HashMap map = null;
map = new HashMap();
map = reader.getInfo();
int nums = reader.getNumberOfPages();
if (map.containsKey("Title")) {
BookTitle = map.get("Title").toString().trim();
}
if (map.containsKey("Author")) {
Author = map.get("Author");
}
if (map.containsKey("Subject")) {
Description = map.get("Subject");
}
TotalPage = nums;
if (map.containsKey("Producer")) {
Publisher = map.get("Producer");
}
String BookTitle = " ";
String Author = " ";
String Description = " ";
String Publisher = " ";
int TotalPage=0;
PdfReader reader=null;
reader = new PdfReader( path);
HashMap map = null;
map = new HashMap();
map = reader.getInfo();
int nums = reader.getNumberOfPages();
if (map.containsKey("Title")) {
BookTitle = map.get("Title").toString().trim();
}
if (map.containsKey("Author")) {
Author = map.get("Author");
}
if (map.containsKey("Subject")) {
Description = map.get("Subject");
}
TotalPage = nums;
if (map.containsKey("Producer")) {
Publisher = map.get("Producer");
}
Getting PDF page as Pic Using JPedal.jar
PdfDecoder ff = new PdfDecoder(true);
ff.openPdfFile( path );
BufferedImage img = ff.getPageAsImage(1); -------------> Number of page
ImageIO.write(img, "png", new File("D:/PDF/" + File Name+ ".png"));
ff.closePdfFile();
ff.openPdfFile( path );
BufferedImage img = ff.getPageAsImage(1); -------------> Number of page
ImageIO.write(img, "png", new File("D:/PDF/" + File Name+ ".png"));
ff.closePdfFile();
JProgressbar
bar is the object of javax.swing.JProgressbar
---------------------------------------------
bar.setMinimum(0);
bar.setMaximum( int size ); ---------> Your Loop size.
bar.setValue(0);
your loop starts {
bar.setValue(bar.getValue()+1);
bar.setStringPainted(true);
Rectangle progressRect = bar.getBounds();
progressRect.x = 0;
progressRect.y = 0;
bar.paintImmediately( progressRect );
}
your loop ends.
---------------------------------------------
bar.setMinimum(0);
bar.setMaximum( int size ); ---------> Your Loop size.
bar.setValue(0);
your loop starts {
bar.setValue(bar.getValue()+1);
bar.setStringPainted(true);
Rectangle progressRect = bar.getBounds();
progressRect.x = 0;
progressRect.y = 0;
bar.paintImmediately( progressRect );
}
your loop ends.
Get Page Content Dynamically using url Class
java.net.URL url=new java.net.URL("http://www.mapsofworld.com/lat_long
/"+cntry+"-lat-long.html");
java.net.HttpURLConnection con=(java.net.HttpURLConnection)url.openConnection();
con.connect();
java.io.InputStream in=con.getInputStream();
java.io.BufferedReader br=new java.io.BufferedReader(new
java.io.InputStreamReader(in));
String temp="";
while((temp=br.readLine())!=null)
{
cont+=temp+"\n";
}
System.out.println(cont);
/"+cntry+"-lat-long.html");
java.net.HttpURLConnection con=(java.net.HttpURLConnection)url.openConnection();
con.connect();
java.io.InputStream in=con.getInputStream();
java.io.BufferedReader br=new java.io.BufferedReader(new
java.io.InputStreamReader(in));
String temp="";
while((temp=br.readLine())!=null)
{
cont+=temp+"\n";
}
System.out.println(cont);
Ajax code for alling a servlets
var req;
function toServlet()
{
url = "Test?hr="+document.getElementById('hr').value+"&min="+document.getElementById('min').value;
if(window.XMLHttpRequest) //non IE
{
req = new XMLHttpRequest();
try
{
req.open("GET",url,true);
req.onreadystatechange=modify;
}
catch(e)
{
alert("cannot connect to server");
}
req.send(null);
}
else if(window.ActiveXobject) //IE
{
req=new ActiveXobject("MicrosoftXMLHTTP");
if(req)
{
req.open("GET",url,true);
req.onreadystatechange=modify;
req.send(null);
}
}
}
the result must be write to out.println(s); in servlet
Receiving the response text from servlet.
function modify_InnerHtml()
{
if(req.readyState == 4)
{
if(req.status==200)
{
var res=req.responseText;
var value="
"+res+"
";
document.getElementById("lin").innerHTML=value;
}
}
}
function toServlet()
{
url = "Test?hr="+document.getElementById('hr').value+"&min="+document.getElementById('min').value;
if(window.XMLHttpRequest) //non IE
{
req = new XMLHttpRequest();
try
{
req.open("GET",url,true);
req.onreadystatechange=modify;
}
catch(e)
{
alert("cannot connect to server");
}
req.send(null);
}
else if(window.ActiveXobject) //IE
{
req=new ActiveXobject("MicrosoftXMLHTTP");
if(req)
{
req.open("GET",url,true);
req.onreadystatechange=modify;
req.send(null);
}
}
}
the result must be write to out.println(s); in servlet
Receiving the response text from servlet.
function modify_InnerHtml()
{
if(req.readyState == 4)
{
if(req.status==200)
{
var res=req.responseText;
var value="
"+res+"
";
document.getElementById("lin").innerHTML=value;
}
}
}
Javascript LightBox CSS Menus Links
http://www.1stwebdesigner.com/resources/57-free-image-gallery-slideshow-and-lightbox-solutions/
http://www.cssdrive.com/index.php/menudesigns/category/C20/P10/
http://www.dynamicdrive.com/dynamicindex4/simplegallery.htm
http://www.cssdrive.com/index.php/menudesigns/category/C20/P10/
http://www.dynamicdrive.com/dynamicindex4/simplegallery.htm
Javascript Code for Browser Dedection
var s;
var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var is_nav = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
&& (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var is_nav4 = (is_nav && (is_major == 4));
var is_nav6 = (is_nav && (is_major == 5));
var is_nav6up = (is_nav && (is_major >= 5));
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
function x()
{
alert(is_ie);
}
var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var is_nav = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
&& (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var is_nav4 = (is_nav && (is_major == 4));
var is_nav6 = (is_nav && (is_major == 5));
var is_nav6up = (is_nav && (is_major >= 5));
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
function x()
{
alert(is_ie);
}
Subscribe to:
Posts (Atom)