Showing posts with label Java Coding. Show all posts
Showing posts with label Java Coding. 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...

Friday, April 17, 2009

HashSet Collection Sample Program Code

Sample Program on HashSet Collection:

A HashSet is a collection set that neither allows duplicate elements nor order or position its elements.

HashSet Methods

  • add() method is used to insert an element in the HashSet collection.
  • size() method helps you in getting the size of the collection.
  • remove() method will be used to delete an element in the HashSet collection.
  • clear() method is used to remove all data from the HashSet collection.
Sample program: HashSet program shows the methods to add, remove and iterate the values of collection. Keys will be used to put and get values. When the HashSet is empty, the below program checks and will display a message "Collection is not having any Elements". If the collection is having Elements then program displays the size of HashSet collection.

import java.util.*;

public class HSetHasHSetet {
public static void main(String [] args) {
System.out.println( "HashSet Example" );
int size;

// Create a HashSet
HasHSetet HSet = new HasHSetet ();
String string1 = "Yellow", string2 = "White",
string3 = "Green", string4 = "Blue";
Iterator iterator;

//Adding data in to HashSet
HSet.add(string1);
HSet.add(string2);
HSet.add(string3);
HSet.add(string4);
System.out.print("HashSet data: ");

//Create a iterator
iterator = HSet.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
System.out.println();

// Get size of a HSet
size = HSet.size();
if (HSet.isEmpty()){
System.out.println("HashSet is empty");
}
else{
System.out.println( "HashSet size: " + size);
}
System.out.println();

// Remove specific data
HSet.remove(string2);
System.out.println("After removing [" + string2 + "]\n");
System.out.print("Now HashSet data: ");
iterator = HSet.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
System.out.println();
size = HSet.size();
System.out.println("HashSet size: " + size + "\n");

//HashSet empty
HSet.clear();
size = HSet.size();
if (HSet.isEmpty()){
System.out.println("Collection is not having any Elements");
}
else{
System.out.println( "HashSet size: " + size);
}
}
}

Read more...

Wednesday, April 15, 2009

Java Wrapper Classes (java.lang package)

(java.lang package) Wrapper Classes in Java:

Each of Java's eight primitive data types has a class dedicated to it. These are known as wrapper classes, because they "wrap" the primitive data type into an object of that class. So, there is an Integer class that holds an int variable, there is a Double class that holds a double variable, and so on. The wrapper classes are part of the java.lang package, which is imported by default into all Java programs.

For each primitive type there is a Wrapper Class: Boolean, Byte, Character, Double, Float, Integer, Long, and Short. Byte, Double, Float, Integer and Short extend the abstract Number class and all are public final ie cannot be extended.

Classes have two constructor forms:

  • constructor that takes the primitive type and creates an object eg Character(char), Integer(int)
  • constructor that converts a String into an object eg Integer("1"). Throws a NumberFormatException if the String cannot be converted to a number.
There is also a wrapper class for Void which cannot be instantiated.

Observe that an Object starts with a capital letter, while the primitives all start with a lowercase and also remember that Strings are Ojects.

Check here for primitive to Wrapper mapping:
byte --> Byte
short --> Short
int --> Integer
long --> Long
char --> Character
float --> Float
double --> Double
boolean --> Boolean
void --> Void
Sample Code for some of the Java Wrapper Classes :
Integer:
int i = 5;
Integer I = Integer.valueOf(i); //Wrapper Class
int i2 = I.intValue(); //Converting to primitive

Float:
float f = 5.5f;
Float F = Float.valueOf(f); //Wrapper Class
float f2 = F.floatValue(); //Converting to primitive

Double:
double d = 5.55555;
Double D = Double.valueOf(d); //Wrapper Class
double d2 = D.doubleValue(); //Converting to primitive

Boolean:
boolean b = true;
Boolean B = Boolean.valueOf(b); //Wrapper Class
boolean b2 = B.booleanValue(); //Converting to primitive
The wrapper classes also provide various tools such as constants nd static methods. We will often use wrapper methods to convert a number type value to a string or a string to a number type as mentioned above.

Read more...

Saturday, February 21, 2009

Creating a Date object in java

Creating instances of java.util.Date & java.sql.Date classes
using a Calendar Class in java.


Java Code:

import java.util.Calendar;

public class DateUtil
{
public void createDates()
{
int year = 2006;
int month = 0; //January
int date = 1;
Calendar cal = Calendar.getInstance();
//Clear all fields
cal.clear();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DATE, date);

//Create instance of java.util.Date
java.util.Date utilDate = cal.getTime();

//Create instance of java.sql.Date
java.sql.Date sqlDate = new java.sql.Date(cal.getTimeInMillis());

System.out.println(utilDate);
System.out.println(sqlDate);
}

public static void main(String[] args) {
DateUtil dateutil = new DateUtil();
dateutil.createDates();
}
}

Read more...

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...
Blog Widget by LinkWithin

JS-Kit Comments

  © Blogger template Newspaper III by Ourblogtemplates.com 2008

Back to TOP