Showing posts with label Java Programming. Show all posts
Showing posts with label Java Programming. Show all posts

Thursday, January 28, 2010

JDBC Java Program Using JDBC TYPE 3 Driver

A Sample Java Program Using JDBC TYPE 3 Driver. This driver is called as Network Protocol Pure Java Driver. This driver would only be the option when DB vendor supplied Type II & IV drivers are not available.

Software: www.idssoftware.com


Server:


a) Install IDSServer software


b) d:\IDSServer\IDSS.exe


(The above said .exe runs as system startup service. IDSS.exe is a server socket program which runs on port number 12.)


c) Notice one more dir called


d:\IDSServer\classes>


jdk11drv.jar, jdk13drv.jar, jdk14drv.jar


Client:
a) Client must demand server to download jdk14drv.jar file into client system and the same must be updated in CLASSPATH

[Note: Software installation is not required]


PATH: (only server)


d:\IDSServer


CLASSPATH (server & client)


d:\IDSServer\classes\jdk14drv.jar


Arch:


Driver: ids.driver.IDSDriver


URL: jdbc:ids://abc:12/conn?dsn='oracleSysDSN'


Procedure creation


c:> sqlplus scott/tiger


SQL> cle scr


sql> create or replace procedure emp_sal_proc(eno IN number, sal1 OUT number) IS

BEGIN

SELECT sal INTO sal1 FROM emp WHERE empno=eno;

END;


// Java
Program

// ProcExecTest.java


import java.sql.*;
public class ProcExecTest

{

public static void main(String rags[]) throws Exception

{

Class.forName("ids.driver.IDSDriver");

Connection
con=DriverManager.getConnection
("jdbc:ids://abc:12/conn?dsn
='oracleSysDSN'", "scott", "tiger");
CallableStatement cstmt=con.prepareCall("{call emp_sal_proc(?,?)}");

cstmt.setInt(1, Integer.parseInt(rags[0]));

cstmt.registerOutParameter(2, Types.DOUBLE);

cstmt.execute();

System.out.println(cstmt.getDouble(2));

cstmt.close();

con.close();

}// main()

}// class

Read more...

Wednesday, May 6, 2009

Java code to get the Users IP Address

Java Program to get the Users IP Address

This following code shows how to obtain the IP address of the user that calls the server. The Request object has a few methods to get information about the call and about the caller, and one of them is named getRemoteAddr() which returns the IP address of the calling computer.

The below example will print the IP address of the user on a blank html-page.

import java.io.*;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;

public class ExampleServlet extends HttpServlet {

protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
printPageStart(out);

//Funtion to Print out the IP address of the caller
out.println(request.getRemoteAddr());
}
}

Read more...

Monday, May 4, 2009

Java Object Serialization

Serialization in Java :

Serialization is the process of converting a set of object instances that contain references to each other into a linear stream of bytes, which can then be sent through a socket, stored to a file, or simply manipulated as a stream of data. Serialization involves saving the current state of an object to a stream, and restoring an equivalent object from that stream.

Serialization is the mechanism used by RMI to pass objects between JVMs, either as arguments in a method invocation from a client to a server or as return values from a method invocation.

For an object to be serialized, it must be an instance of a class that implements either the Serializable or Externalizable interface. Both interfaces only permit the saving of data associated with an object's variables.

The Serializable interface relies on the Java runtime default mechanism to save an object's state. Writing an object is done via the writeObject() method in the ObjectOutputStream class or the ObjectOutput interface.

The Externalizable interface specifies that the implementing class will handle the serialization on its own, instead of relying on the default runtime mechanism.

Classes ObjectInputStream and ObjectOutputStream, which respectively implement the ObjectInput and ObjectOutput interfaces, enable entire objects to be read from or written to a stream (possibly a file). To use serialization with files, we initialize ObjectInputStream and ObjectOutputStream objects with stream objects that read from and write to files—objects of classes FileInputStream and FileOutputStream, respectively.

Initializing stream objects with other stream objects in this manner is sometimes called wrapping—the new stream object being created wraps the stream object specified as a constructor argument. To wrap a FileInputStream in an ObjectInputStream, for instance, we pass the FileInputStream object to the ObjectInputStream’s constructor.

The ObjectOutput interface contains method writeObject, which takes an Object that implements interface Serializable (discussed shortly) as an argument and writes its information to an OutputStream. Correspondingly, the ObjectInput interface contains method readObject, which reads and returns a reference to an Object from an InputStream. After an object has been read, its reference can be cast to the object’s actual type.

The object to be serialized must implement java.io.Serializable. This example serializes a javax.swing.JButton object.


Object object = new javax.swing.JButton("push me");

try {
// Serialize to a file
ObjectOutput out = new ObjectOutputStream(new FileOutputStream("filename.ser"));
out.writeObject(object);
out.close();

// Serialize to a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
out = new ObjectOutputStream(bos) ;
out.writeObject(object);
out.close();

// Get the bytes of the serialized object
byte[] buf = bos.toByteArray();
} catch (IOException e)
{
}


Object serialization is performed with byte-based streams, so the sequential files created and manipulated will be binary files. Recall that binary files cannot be viewed in standard text editors.

Read more...

Saturday, February 21, 2009

Creating a zip file in Java

Java Code Example: Create a zip file in Java

Steps:

  • Create an input stream from the file to compress
  • Read from it we write the contents to an output stream.
  • Output stream is of type ZipOutputStream which takes an FileOutputStream as parameter.
  • Add a zip entry to the output stream before we start writing to it.
  • Clean up by closing the zip entry and both the input stream and output stream.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
*
* @author javadb.com
*/

public class Main {

/**
* Creates a zip file
*/

public void createZipFile() {

try {
String inputFileName = "test.txt";
String zipFileName = "compressed.zip";

//Create input and output streams
FileInputStream inStream = new FileInputStream(inputFileName);
ZipOutputStream outStream = new ZipOutputStream(new
FileOutputStream(zipFileName));

// Add a zip entry to the output stream
outStream.putNextEntry(new ZipEntry(inputFileName));

byte[] buffer = new byte[1024];
int bytesRead;

//Each chunk of data read from the input stream
//is written to the output stream
while ((bytesRead = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, bytesRead);
}

//Close zip entry and file streams
outStream.closeEntry();

outStream.close();
inStream.close();

} catch (IOException ex) {
ex.printStackTrace();
}
}

/**
* @param args the command line arguments
*/

public static void main(String[] args) {
new Main().createZipFile();
}
}

Read more...

Friday, October 31, 2008

Java Programming Language Versions

Since its introduction, Sun has released a new version of the Java language every two years or so. These new versions brought enhancements, new capabilities and fixes to bugs. Until recently, the versions were numbered 1.x, where x reached up till 4. (Intermediate revisions were labeled with a third number - 1.x.y - as in 1.4.2.) The newest version, however, is called Java 5.0 rather than Java 1.5.

Below is a timeline of the different versions of the basic, or Standard Edition (SE), of Java along with some of the new features that each one introduced. This edition contains the core language packages (the name for code libraries in Java) and is aimed for desktop programming.

* 1995: Version 1.0 of the Java Development Kit (JDK) was released for free by Sun.
o 8 packages with 212 classes
o Netscape 2.0-4.0 included Java 1.0.
o Microsoft and other companies licensed Java.

* 1997: Version 1.1:
o 23 packages - 504 classes
o Improvements include better event handling, inner classes, improved JVM.
o Microsoft developed its own 1.1. compatible Java Virtual Machine for the Internet Explorer.
o Many browsers in use are still compatible only with 1.1.
o Swing packages of greatly improved graphics became available during this time but not included with the core language.

* 1999: Version 1.2, also called the Java 2 Platform
o 59 packages - 1520 classes
o Code and tools distributed as The Software Development Kit (SDK)
o Java Foundation Classes (JFC), based on Swing, for improved graphics and user interfaces, now included with the core language.
o Collections API included support for various lists, sets, and hash maps.

* 2000: Version 1.3:
o 76 packages - 1842 classes
o Performance enhancements including the Hotspot virtual machine.

* 2002: Version 1.4:
o 135 packages - 2991 classes
o Improved IO, XML support, etc.

* 2004: Version 5.0 (previously numbered 1.5):
o 165 packages, over 3000 classes
o Faster startup and smaller memory footprint
o Metadata
o Formatted output
o Generics
o Improved multithreading features

* 2006: Version 6.0
o Dramatic performance improvements for the core platform and Swing.
o Java Compiler API
o Improved Web Service support through JAX-WS
o JDBC 4.0 support
o Support for pluggable annotations
o Many GUI improvements, such as integration of SwingWorker in the API..etc

Java SE 6 Update 10 is meant as a major enhancement in terms of end-user usability. The release version is currently available for download. This version features

o Java Kernel, a small installer including only the most commonly used JRE classes.
o A new Swing look and feel called Nimbus and based on synth.
o Enhanced updater.
o Enhanced versioning and pack200 support: server-side support is no longer required.
o Java Quick Starter, to improve cold start-up time.

Java SE 7

Codename Dolphin. This is in the early planning and development stages. The Dolphin Project began in August 2006 and is tentatively scheduled for release in 2009.

Read more...

History of Java Programming Language

Java is a programming language and environment invented by James Gosling and others in 1994. Java was originaly named Oak and was developed as a part of the Green project at the Sun Company. The writing of Java began in December of 1990. Patrick Naughton, Mike Sheridan, and James Gosling and were trying to figure out the "next wave" in computing.
Sun formally announced Java and HotJava at SunWorld `95. Soon after, Netscape Inc. announced that it would incorporate Java support in their browser. This was a great triumph for Java since it was now supported by the most popular browser in the world. Later, Microsoft also announced that they would support Java in their Internet Explorer web browser, further solidifying Java's role in the World Wide Web.
Java 1.0 was the first version. Java 1.1 added important changes to user interface management, and many important new classes were added (JDBC, JavaBeans, ...). Java 2 was released at the end of 1998 with important additions to many different parts of Java, but especially with an improved graphical user interface, and many additional packages.
Versions:
Java 1.0 - 212 classes in 8 packages, released May 1996
Java 1.1 - 503 classes in 23 packages, released Feb 1997
Java 1.2/2.0 - 1,520 classes in 59 packages, released Dec 1998
Java 5.0 - 3562 classes in 166 packages.

Read more...
Blog Widget by LinkWithin

JS-Kit Comments

  © Blogger template Newspaper III by Ourblogtemplates.com 2008

Back to TOP