30 October 2012

dom parser

package check;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;



public class DOM_Demo {

Document dom;

public static void main(String[] args) throws Exception {

new DOM_Demo().go();

}


void ret(Node d){

NodeList n=d.getChildNodes();

{
System.out.println(d.getNodeName());
for@(int i=0;i{
    if(n.item(i).getNodeType()==1)  // 1 IS THE VALUE FOR TAG

ret(n.item(i));

}
else

{

if(n.item(i).getNodeValue().trim().length()!=0)
System.out.println("val : "+n.item(i).getNodeValue());
}

}


}

}

void go() throws Exception

{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//Using factory get an instance of document builder

DocumentBuilder db = dbf.newDocumentBuilder();
//parse using builder to get DOM representation of the XML file

dom = db.parse("C:/Users/shasharm/Desktop/ibf/NS_ABIS_IRP_IP_E1.xml");

Element d = dom.getDocumentElement();

ret(d);

}

}





Delicious add to del.icio.us saved by 0 users

sax parser



import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class SAXParserExample extends DefaultHandler{

    List myEmpls;
   
    private String tempVal;
   
    //to maintain context
    private Employee tempEmp;
   
   
    public SAXParserExample(){
        myEmpls = new ArrayList();
    }
   
    public void runExample() {
        parseDocument();
        printData();
    }

    private void parseDocument() {
       
        //get a factory
        SAXParserFactory spf = SAXParserFactory.newInstance();
        try {
       
            //get a new instance of parser
            SAXParser sp = spf.newSAXParser();
           
            //parse the file and also register this class for call backs
            sp.parse("employees.xml", this);
           
        }catch(SAXException se) {
            se.printStackTrace();
        }catch(ParserConfigurationException pce) {
            pce.printStackTrace();
        }catch (IOException ie) {
            ie.printStackTrace();
        }
    }

    /**
     * Iterate through the list and print
     * the contents
     */
    private void printData(){
       
        System.out.println("No of Employees '" + myEmpls.size() + "'.");
       
        Iterator it = myEmpls.iterator();
        while(it.hasNext()) {
            System.out.println(it.next().toString());
        }
    }
   

    //Event Handlers
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        //reset
        tempVal = "";
        if(qName.equalsIgnoreCase("Employee")) {
            //create a new instance of employee
            tempEmp = new Employee();
            tempEmp.setType(attributes.getValue("type"));
        }
    }
   

    public void characters(char[] ch, int start, int length) throws SAXException {
        tempVal = new String(ch,start,length);
    }
   
    public void endElement(String uri, String localName, String qName) throws SAXException {

        if(qName.equalsIgnoreCase("Employee")) {
            //add it to the list
            myEmpls.add(tempEmp);
           
        }else if (qName.equalsIgnoreCase("Name")) {
            tempEmp.setName(tempVal);
        }else if (qName.equalsIgnoreCase("Id")) {
            tempEmp.setId(Integer.parseInt(tempVal));
        }else if (qName.equalsIgnoreCase("Age")) {
            tempEmp.setAge(Integer.parseInt(tempVal));
        }
       
    }
   
    public static void main(String[] args){
        SAXParserExample spe = new SAXParserExample();
        spe.runExample();
    }
   
}








Delicious add to del.icio.us saved by 0 users

17 October 2012

jdbc connection with oracle 10 g

try{


String driverName = "oracle.jdbc.driver.OracleDriver";

Class.forName(driverName);



// Create a connection to the database

String serverName = "127.0.0.1";

String portNumber = "1521";

String sid = "xe";

String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + "/" + sid;

String username = "system";

String password = "9868626216";

Connection connection = DriverManager.getConnection(url, username, password);



String query="select * from employee";

Statement st= connection.createStatement();

ResultSet rs=st.executeQuery(query);

System.out.println(rs);

//while(rs.next())

//{



//System.out.println("not 1 empty");

//}











} catch (Exception e) {

// Could not find the database driver

e.printStackTrace();

}




Delicious add to del.icio.us saved by 0 users

08 October 2012

japplet fundamental

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class test extends JApplet  {

    public void go() {
       
       
        this.setSize(500,500);
        this.setVisible(true);
   
        this.setLayout(new FlowLayout());
        System.out.println("in go");
        JButton jb=new JButton("click me");
        this.add(jb);
       jb.addActionListener(new ActionListener()
       {
             public void actionPerformed(ActionEvent ae)
             {
                  JOptionPane.showMessageDialog(null,"japplet example");
                
                 }
           }
           );
    }
   
    public void init()
    {
        System.out.println("in init");
        try{
            SwingUtilities.invokeAndWait(new Runnable(){
                public void run()
                {
                    go();
                   
                    }
            });
        }
         catch(Exception e){
            
               e.printStackTrace();
             }
        
    }
   
   public void paint(Graphics g)
   {
          //g.drawString("hi shailesh",10,10);
       }
}




Delicious add to del.icio.us saved by 0 users

07 October 2012

creating object from string in java

import java.io.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;


   
   

class test{
   
   
    static void display()
    {
        System.out.println("this is display");
        }
   
    public static void main(String args[]) throws Exception
    {
       
    String s="test";
   
    ((test)Class.forName(s).newInstance()).display();
       
    }
       
    }   




Delicious add to del.icio.us saved by 0 users

code to execute .exe in java

import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;


public class test {
public static void main(String[] args) {
Runtime run = Runtime.getRuntime();
try {

Process pp=run.exec("progs");    // for cmd commands use=> run.exec("cmd /c dir");  where dir is a command. another eg=>run.exec("cmd /c javac x.java")
BufferedReader in =new BufferedReader(new InputStreamReader(pp.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}


//run.exec("cmd /c start");  for opening cmd




Delicious add to del.icio.us saved by 0 users