TechieDrill Your World Of Technical Tutorials

Archive for the ‘Java’ Category

Java Console Tricks

11.22.2009 · Posted in Java

import java.io.*; class ConsoleDemo { public static void main(String args[]) { String str; Console con; con = System.console(); if(con ==null) return; str = con.readLine(”Enter a string”); con.printf(”Here is your string: %s\n”, str); System.out.println(”Please enter password…”); char cpass[] = con.readPassword(”****************************”); System.out.println(”Your password is : “); for ( int i=0; i<cpass.length; i++) System.out.println(cpass[i]); } } This is a cool java trick. ...

ByteArrayInputStream

11.22.2009 · Posted in Java

import java.io.*; class ByteArrayInputStreamDemo { public static void main(String [] args) throws IOException { byte data[]=”Here is a String”.getBytes(); ByteArrayInputStream in = new ByteArrayInputStream(data); int ch; while((ch = in.read()) !=-1) { System.out.println(ch+ ” “); System.out.println((char)ch+” “); } } }import java.io.*; class ByteArrayInputStreamDemo { public static void main(String [] args) throws IOException { byte data[]=”Here is a String”.getBytes(); ByteArrayInputStream in = ...

Java Assertions

11.22.2009 · Posted in Java

Assertions are typically a predictable statements that we can program. Assertions facilitate to test our assumptions about our program. Assertions holds a boolean value which we assume it to be true, if it turn it to be false then interpreter will throw an exception. Having assertions in programs will be feasible for us to detect and correct ...

Understanding Java Static block/variables/method

11.20.2009 · Posted in Java

lass UseStatic { static int a=3; static int b; static void meth(int x) { System.out.println(”x: “+x); System.out.println(”a: “+a); System.out.println(”b: “+b); } static { System.out.println(”static block initialized: “); b=a*4; } public static void main(String [] args) { meth(42); } } Static block/variables/methods are very powerful in java. When we say static it is static throughout the application. If you declare ...

Java Variable Arguments Example

11.20.2009 · Posted in Java

lass VarArgs { static void vaTest(String msg, int… v) { System.out.println(msg +v.length+ ” Contents:”); for(int x : v) System.out.print(x+ ” “); System.out.println(); } public static void main(String args[]) { vaTest(”One vaarg:”, 10); vaTest(”Three varrgs:”,1,2,3); vaTest(”No varrgs”); } } Variable arguments or in short VarArgs introduced in J2SE1.5. This feature is very helpful for developers to declare as much as parameters ...

Launching Applications from Java Runtimes

11.20.2009 · Posted in Java

You can run any Applications by executing Java Runtime Classes See the code below class RuntimeDemo { public static void main(String[] args) throws Exception { Runtime r =Runtime.getRuntime(); Process p=null; p=r.exec(”notepad.exe”); } } Copy and paste the above piece of de and execute the java interpreter class RuntimeDemo { public static void main(String[] args) throws Exception { Runtime r =Runtime.getRuntime(); Process p=null; ...

Executing SQL from Apache Derby

11.20.2009 · Posted in Java

Execute SQL statements If connection is established properly with the Apache Derby, you can now execute simple sequel statements from your command prompt ij tool as shown below Creating tables using ij tool ij> create table table_name(roll_no int, first_name varchar(10), last_name varchar(10)); Inserting values into the table ij> insert into table_name values (197878,’jack’,'Samurai’); ij> insert into table_name values (197823,’Corsa’,'Neo’); ij> insert ...

Setting Environment for Apache Derby and Connecting to DB

11.20.2009 · Posted in Java

Setting Environment for Apache Derby Goto command prompt by keying on cmd from Run application launcher. Goto your java classpath and give the below command > java org.apache.derby.tools.ij Now your command prompt would change as shown below ij> Note: you may get errors if classpath is not set correctly Connecting to the database Connect to the database by giving the below command in ...

Installing and configuring Apache Derby

11.20.2009 · Posted in Java

Install Derby Install Apache Derby from the below link http://db.apache.org/derby/derby_downloads.html “db-derby-10.5.1.1-bin.zip” Go to Command Prompt by clicking start button and selecting RUN option. Type cmd in the Run window to open the command prompt. SET Derby INSTALL using the below command > set DERBY_INSTALL=D:\Program Files\Java\db-derby-10.5.1.1-bin Configure Apache Derby To configure Apache Derby in embedded mode connect derby jars to the classpath derby.jar: contains the ...