Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Mykodes

HOME JAVA JAVASCRIPT ORACLE/ SQL OTHERS FORUM

Run java Program Every One Hour


5:03:00 pm codings, html, java No comments This post describe the automation of java program run continuously using timer class. Here i coded this program run every one hour(1000 * 60 * 60 ).If you need change the time interval change your timing instead of 60.If you need to run the program only in particular day use date.set( Calendar.DAY_OF_WEEK, Calendar.TUESDAY ); Code: import import import import java.util.Calendar; java.util.Date; java.util.Timer; java.util.TimerTask;

public class EveryHour extends TimerTask { public void run() { Calendar date = Calendar.getInstance(); int n=date.getTime().getMinutes(); System.out.println("Generating report:"+n+":"+date.getTime()); //TODO generate report } public static void main(String[] args) { Timer timer = new Timer(); Calendar date = Calendar.getInstance(); // date.set( // Calendar.DAY_OF_WEEK, // Calendar.TUESDAY // ); date.set(Calendar.HOUR, 0); date.set(Calendar.MINUTE, 0); date.set(Calendar.SECOND, 0); date.set(Calendar.MILLISECOND, 0); // Schedule to run every Sunday in midnight timer.schedule( new sleep(), date.getTime(), 1000 * 60 * 60 );

} } I hope it will help you.Thanks for visiting this page. READ MORE

Open Webpage URL in Java Swing Application


4:48:00 pm html, java, swing No comments Hi guys,here i am posting the method ,how to view webpage in swing application. import import import import org.eclipse.swt.*; org.eclipse.swt.browser.*; org.eclipse.swt.layout.*; org.eclipse.swt.widgets.*;

public class url { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); Browser browser = new Browser(shell, SWT.NONE); browser.setUrl("http://www.mykodes.com"); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } } Here we also use the static html file open using, browser.setUrl("C://mykodes.html");

READ MORE

Read XML File using Java


12:22:00 pm codings, java No comments This post describes about ,how to read data from xml file using java. Coding: import import import import import import import javax.xml.parsers.DocumentBuilderFactory; javax.xml.parsers.DocumentBuilder; org.w3c.dom.Document; org.w3c.dom.NodeList; org.w3c.dom.Node; org.w3c.dom.Element; java.io.File;

public class readXML { public static void main(String argv[]) { try { File fXmlFile = new File("d://file.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("Value"); System.out.println("----------------------------"); System.out.println("node length:"+nList.getLength()); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); System.out.println("\nCurrent Element :" + nNode.getNodeName()); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode;

System.out.println("Value id : " + eElement.getAttribute("id")); System.out.println("tag1 : " + eElement.getElementsByTagName("tag1").item(0).getTextContent ()); System.out.println("tag2 : " + eElement.getElementsByTagName("tag2").item(0).getTextContent ()); System.out.println("----"); } } } catch (Exception e) { e.printStackTrace(); } } } Output: Root element :Root ---------------------------node length:5 Current Element :Value Value id : 1111 tag1 : check1 tag2 : 2 ---Current Element :Value Value id : 1122 tag1 : check2 tag2 : 12 ---Current Element :Value Value id : 1133 tag1 : check3 tag2 : 22 ---Current Element :Value Value id : 1441 tag1 : check4 tag2 : 32 ---Current Element :Value Value id : 1155 tag1 : check5

tag2 : 42 ---Similar Posts: Create XML file in Java using Loop READ MORE

Create XML file in Java using Loop


12:18:00 pm codings, java No comments Here i post the steps for write xml file using Java with loop.I have three arraylist with values and i will insert that value in my xml file using DocumentBuilder. Coding: import java.io.File; import java.util.ArrayList; import import import import import import import import import javax.xml.parsers.DocumentBuilder; javax.xml.parsers.DocumentBuilderFactory; javax.xml.parsers.ParserConfigurationException; javax.xml.transform.Transformer; javax.xml.transform.TransformerConfigurationException; javax.xml.transform.TransformerException; javax.xml.transform.TransformerFactory; javax.xml.transform.dom.DOMSource; javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; public class createXML { public static void main(String argv[]) throws TransformerException { ArrayList<String> ar1=new ArrayList<String>(); ArrayList<String> ar2=new ArrayList<String>(); ArrayList<String> ar3=new ArrayList<String>(); ar3.add("1111");ar3.add("1122");ar3.add("1133");ar3.add("1441") ;ar3.add("1155");

ar1.add("check1");ar1.add("check2");ar1.add("check3");ar1.add(" check4");ar1.add("check5"); ar2.add("2");ar2.add("12");ar2.add("22");ar2.add("32");ar2.add("4 2");ar2.add("52"); DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder; try { docBuilder = docFactory.newDocumentBuilder(); // root elements Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement("Root"); doc.appendChild(rootElement); for(int i=0;i<ar1.size();i++){ // staff elements Element staff = doc.createElement("Value"); rootElement.appendChild(staff); // set attribute to staff element Attr attr = doc.createAttribute("id"); attr.setValue(ar3.get(i).toString()); staff.setAttributeNode(attr);

Element tag1 = doc.createElement("tag1"); tag1.appendChild(doc.createTextNode(ar1.get(i).toString())); staff.appendChild(tag1); Element tag2 = doc.createElement("tag2"); tag2.appendChild(doc.createTextNode(ar2.get(i).toString())); staff.appendChild(tag2);

} TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer; try { transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc);

StreamResult result = new StreamResult(new File("d:\\file.xml")); // Output to console for testing // StreamResult result = new StreamResult(System.out); transformer.transform(source, result); System.out.println("File saved!"); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (ParserConfigurationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } Output:
<Root> <Value id="1111"> <tag1>check1</tag1> <tag2>2</tag2> </Value> <Value id="1122"> <tag1>check2</tag1> <tag2>12</tag2> </Value> <Value id="1133"> <tag1>check3</tag1> <tag2>22</tag2> </Value> <Value id="1441"> <tag1>check4</tag1> <tag2>32</tag2> </Value> <Value id="1155"> <tag1>check5</tag1> <tag2>42</tag2> </Value> </Root>

Check this also:

Read XML File using Java READ MORE

Remove Last comma in String


5:03:00 pm codings, java No comments

String Array to String in java


This post is just develop the String using String Array. And we will separate every word in String using comma and delete last comma.It will use for concatenate multiple Strings. Coding: public class sample { public static void main(String args[]){ String as[]={"mykodes.com","java","String"}; String fin = ""; char c=','; for(int i=0;i<as.length;i++){ fin+=as[i]+c; } String a = fin.substring(0, fin.lastIndexOf(",")); System.out.println(a); } Output: mykodes.com,java,String READ MORE

Pointel solutions interview JAVA Questions October 2013


11:38:00 am codings, java No comments Pointel solutions interview Questions on October 2013 for JAVA Candidates. 1) Design Login page in jsp and pass user credentials to next page.click here 2) Find smallest and largest number from array Coding: import java.util.ArrayList; import java.util.Collections; import java.util.List; class Sample{ public static void main(String args[]){ int ar[]={11,2,500,5000,5,88,14,9977}; List li=new ArrayList(); for(int i=0;i<ar.length;i++){ li.add(ar[i]); } Collections.sort(li); System.out.println("Smallest no:"+li.get(0)); System.out.println("Highest no:"+li.get(li.size()-1)); } } Output: Smallest no: 2 Highest no: 9977 3) Reverse a sentence without using string built-in function Coding: import java.util.ArrayList; import java.util.StringTokenizer; public class Split { public static void main(String args[]){ String myTextToBeSplit = He is a Boy"; StringTokenizer tok = new StringTokenizer(

myTextToBeSplit ); ArrayList ar=new ArrayList(); while ( tok.hasMoreTokens()) { String word = tok.nextToken(); ar.add(word); } for(int i=ar.size()-1;i>=0;i--){ System.out.print(ar.get(i)+" "); } } } Output: Boy a is He 4) Find missing odd number in an sequence order of odd numbers Coding: import java.util.ArrayList; public class Odd { public static void main(String args[]){ int ar[]={1,3,5,9,13,17,25,35}; ArrayList ar1=new ArrayList(); System.out.println("Missing Odd Numbers:"); for(int i=0;i<ar.length-1;i++){ int k=ar[i+1]; int l=ar[i]; int n=k-l; if(n%2==0){ if(n!=2&&n>2){ for(int j=2;j<n;j+=2){ System.out.println(l+j); } } } else{ System.out.println("-----------------------"); System.out.println(ar[i+1]+"is even number"); } } } } Output:

Missing Odd Numbers: 7 11 15 19 21 23 27 29 31 33

READ MORE

Split first (proceeding) Character in String using Java


3:22:00 pm codings, java No comments This post describes how to separate String by String split.Now we have the string with multiple "-" and we are going to split whole string into two String.Here i am using s.split("-", 2); It will define first argument as the character which will want to separate and the 2 means it will position in the String. Code: class Sample{ public void split(String s){ String s1[]=new String[2]; s1=s.split("-", 2); System.out.println("String::"+s); System.out.println("first:"+s1[0]); System.out.println("second:"+s1[1]); System.out.println("-----------"); } public static void main(String args[]){ Sample ob=new Sample(); ob.split("012-1111"); ob.split("SAD-1111"); ob.split("SAD-012-1111"); } } Output: String::012-1111 first:012 second:1111 -------------------------String::SAD-1111 first:SAD second:1111 -------------------------String::SAD-012-1111 first:SAD second:012-1111

SOCIAL PROFILE

Popular @Mykodes

Tag

Blog Archives

Pointel Solution Park,Chennai Ja Technical writte Interview Questi First round,Techn for java candidate questions. 1)Fibo series cli 2)Prime Numbe...

Differnce betwee and jdk 1.7 JDK 1.6 JDK 1.7 was released as Java SE1.6. JDK released as part

Convert Tiff Ima Jpeg using java Convert Tiff Imag using java We ne com.sun.media.ja for convert Tiff im The sun.media.ja

Create from .b exe file window Create Services f file or exe file 1.T

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

Windows NT user service, perform following steps: DOS comm... READ MORE
Home Older Posts

Subscribe to: Posts (Atom)

Write a program Number using ja A prime number ) is a natural num than 1 that has no divisors other tha itself. A natural nu

Look a Method java import j import java.awt.e import javax.swin class newlookAnd JFrame fr; JTex txtArea; public .

Disable refresh using javascript This coding is use disable refresh bu F5 button in brow the eb page is run <html> <head>

Write a program Fibonacci series in java import java.lang.S import java.util.*; class fib{ int i1=0,i2=0,i3=1,n; Scanne Scanner...

Find HCF or GC numbers using j

Highest common mathematics, also greatest common Now this program find the HCF for t numbers...

HTML File in Ecl Application View HTML File in Eclip Application ViewP article is show ho local html file in y rcp application.He

Powered by Blo

BLOG ARCHIVE

2013 (105) Decem Ru

Op

Novem

Octob

Augus

July (2 June

May (3

April (1

March

Februa

TOTAL PAGEVIE

Copyright 2013 Mykodes | Powered by Blogger

You might also like