Friday, 11 May 2012

Nested Interfaces in Java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author bjr
*/
public class Main implements nested
{
public static void main(String args[])
{
ImplTopIfImplTopIf m=new ImplTopIfImplTopIf ();
System.out.println("Hello world");
System.out.println(m.fNestedIf1());
}
public static class ImplTopIfImplTopIf implements NestedIf1
{
@Override
public String fNestedIf1()
{
return "NestedIf1 implementation";
}
}

public static class NestedImplNestedIf2 implements NestedIf1.NestedIf2
{
@Override
public String fNestedIf2()
{
return "NestedIf2 implementation";
}
}

public interface NestedIf3
{
String fNestedIf3();
}

public static class NestedImplNestedIf3 implements NestedIf3
{
@Override
public String fNestedIf3()
{
return "NestedIf3 implementation";
}
}

@Override
public String fTopIf()
{
return "fTopIf implementation";
}
}
class ImplNestedIf3 implements Main.NestedIf3
{
@Override
public String fNestedIf3()
{
return "NestedIf3 implementation";
}
}

Thursday, 10 May 2012

JDBC connection in net beans

The following statements in the java net beans in try catch block will connects to mysql jdbc connection
Before that you have to add jdbc connector jar file into you netbeans java project libraries

You also need to import the following packages

import com.mysql.jdbc.Statement; // for executing sql queries
import java.sql.DriverManager; // for jdbc connection
import java.sql.SQLException; // fir raising exception

Class.forName("java.sql.Driver");
Connection conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost/snakes_ladders","root","root");
Statement stmt=(Statement) conn.createStatement();
if(conn!=null)
{
System.out.println("connection success");
}
else
{
System.out.println("connection failed");
}

Wednesday, 2 May 2012

Reading pdf file in java

import com.itextpdf.awt.geom.Rectangle;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import java.io.FileOutputStream;


/**
*
* @author bjr
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try {
PdfReader reader = new PdfReader("D:\\MSIT_2012\\SSD\\Week3\\Applying_UML_And_Patterns_2001_Craig_Larman.pdf");
int n = reader.getNumberOfPages();
com.itextpdf.text.Rectangle psize = reader.getPageSize(1);
Document document = new Document(psize);
// creating new pdf file and writing the contentof the other pdf file to it
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\\BookShelf\\new.pdf"));

document.open();
PdfContentByte pdf = writer.getDirectContent();
document.newPage();
int i=1;
while (i<=n)
{
String str=PdfTextExtractor.getTextFromPage(reader,i); // priting content of the pdf file to console
System.out.println(str);
i++;
}
PdfImportedPage page = writer.getImportedPage(reader, 1);
pdf.addTemplate(page, .5f, 0, 0, .5f, 60, 120);
document.close();
} catch (Exception de) {}
}

}