Tuesday, June 26, 2012

Summary of Operators in JAVA


The following quick reference summarizes the operators supported by the Java programming language.

Simple Assignment Operator

=       Simple assignment operator

Arithmetic Operators

+       Additive operator (also used
        for String concatenation)
-       Subtraction operator
*       Multiplication operator
/       Division operator
%       Remainder operator

Unary Operators

+       Unary plus operator; indicates
        positive value (numbers are 
        positive without this, however)
-       Unary minus operator; negates
        an expression
++      Increment operator; increments
        a value by 1
--      Decrement operator; decrements
        a value by 1
!       Logical complement operator;
        inverts the value of a boolean

Equality and Relational Operators

==      Equal to
!=      Not equal to
>       Greater than
>=      Greater than or equal to
<       Less than
<=      Less than or equal to

Conditional Operators

&&      Conditional-AND
||      Conditional-OR
?:      Ternary (shorthand for 
        if-then-else statement)

Type Comparison Operator

instanceof      Compares an object to 
                a specified type 

Bitwise and Bit Shift Operators

~       Unary bitwise complement
<<      Signed left shift
>>      Signed right shift
>>>     Unsigned right shift
&       Bitwise AND
^       Bitwise exclusive OR
|       Bitwise inclusive OR

Tuesday, March 20, 2012

Monday, January 2, 2012

Java Database connectivity (Type 4) to Oracle db

Make sure you have the JDBC driver downloaded and added to External Jar paths in eclipse. my driver file was ojdbc14.jar.

My Oracle version was :
SQL> select BANNER from SYS.V_$VERSION;

BANNER
----------------------------------------------------------------
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Product
PL/SQL Release 10.2.0.1.0 - Production
CORE    10.2.0.1.0    Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production



Code Steps :
In general, to process any SQL statement with JDBC, you follow these steps:
  1. Establishing a connection.
  2. Create a statement.
  3. Execute the query.
  4. Process the ResultSet object.
  5. Close the connection.
Note: please make sure the user you are connecting with has create session priviledge.

Give this on your oracle DB.
SQL> grant create session to sriram;
Grant succeeded.

===== Class file I used to connect ======
 import java.sql.*;
class JDBC1example {
  public static void main (String[] args) throws Exception
  {
   Class.forName ("oracle.jdbc.OracleDriver");

   Connection conn = DriverManager.getConnection
     ("jdbc:oracle:thin:@//localhost:1521/XE", "sriram", "sriram321");
                        // @//machineName:port/SID,   userid,  password
   try {
     Statement stmt = conn.createStatement();
     try {
       //ResultSet rset = stmt.executeQuery("select BANNER from SYS.V_$VERSION");
         ResultSet rset = stmt.executeQuery("select * from all_users");
       try {
         while (rset.next())
           System.out.println (rset.getString(1));   // Print col 1
       }
       finally {
          try { rset.close(); } catch (Exception ignore) {}
       }
     }
     finally {
       try { stmt.close(); } catch (Exception ignore) {}
     }
   }
   finally {
     try { conn.close(); } catch (Exception ignore) {}
   }
  }
}

================Result-=====================

SRIRAMS
SRIRAM
FLOWS_020100
FLOWS_FILES
HR
MDSYS
ANONYMOUS
XDB
CTXSYS
DBSNMP
TSMSYS
DIP
OUTLN
SYSTEM
SYS
ORADBA1
TEST









Friday, December 9, 2011

Using Eclipse with Java7 on Windows 7

I tried using below Eclipse Build with Java SE 7

Eclipse Platform
Version: 3.7.1
Build id: M20110909-1335

It throws error if you try running with Java 7 sdk or jre.

I was able to run by invoking below jar file.

 C:\Program Files\Glassfish\jdk7\bin>java -jar D:\eclipse-jee-indigo-SR1-win32\plugins\org.eclipse.equinox.launcher_1.2.0.v20110502.jar


Check this wiki for those who want to run Java 7 on eclipse :

http://wiki.eclipse.org/JDT/Eclipse_Java_7_Support_%28BETA%29

Thursday, September 1, 2011

Free Online Java Training Using Alice and Greenfoot

 
"It is a great opportunity to find out the fun of programming and creating your own animation or whatever you want to create," said Isabella, a 10th grade student who participated in the summer workshop.
 

Alice and Greenfoot are two award-winning educational software tools designed to teach Java programming and problem solving. No programming experience is required to benefit from the recordings of the Java Summer Workshop 2011, which was organized by the Oracle Technology Network and Oracle Academy.

Monday, July 4, 2011

Java : read from xml


Check this API : javax.xml.parsers  below :
http://download.oracle.com/javase/6/docs/api/




==============XMLReader.java==================

import java.io.File;
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 XMLReader {

 public static void main(String argv[]) {

  try {
  File file = new File("MyXMLFile.xml");
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  Document doc = db.parse(file);
  doc.getDocumentElement().normalize();
  System.out.println("Root element " + doc.getDocumentElement().getNodeName());
  NodeList nodeLst = doc.getElementsByTagName("employee");
  System.out.println("Information of all employees");

  for (int s = 0; s < nodeLst.getLength(); s++) {

    Node fstNode = nodeLst.item(s);
   
    if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
 
           Element fstElmnt = (Element) fstNode;
      NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("firstname");
      Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
      NodeList fstNm = fstNmElmnt.getChildNodes();
      System.out.println("First Name : "  + ((Node) fstNm.item(0)).getNodeValue());
      NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("lastname");
      Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
      NodeList lstNm = lstNmElmnt.getChildNodes();
      System.out.println("Last Name : " + ((Node) lstNm.item(0)).getNodeValue());
    }

  }
  } catch (Exception e) {
    e.printStackTrace();
  }
 }
}

=====================END=====================

============MyXMLFile.xml=================
<?xml version="1.0"?>
<company>
    <employee>
        <firstname>Sriram</firstname>
        <lastname>Sreedhar</lastname>
    </employee>
    <employee>
        <firstname>Harini</firstname>
        <lastname>Sriram</lastname>
    </employee>
    <employee>
        <firstname>firstname</firstname>
        <lastname>lastname</lastname>
    </employee>
</company>








========================================