Friday, 21 December 2012

Java MYSQL connection

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

*/
package helloworld;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Scanner;
import java.sql.*;

/**
*// In order to get database mysql connection you need add mysql-connector-java-5.0.8.tar jar file to the libraries in netbeans or eclipse
for doubts contact jayachandra1805@gmail.com

* @author jayachandra
*/
public class Helloworld {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// TODO code application logic here
char c;
Hashtable ht=new Hashtable();
for(int i=0;i<10;i++) { ht.put(i,"a"); } // public final native Class getClass();
System.out.println(!ht.contains("ad"));
Enumeration e=ht.keys();
while(e.hasMoreElements())
{
System.out.println(ht.get(e.nextElement()));

}

Scanner sc=new Scanner(System.in);

sqlConnection();
}
public static void sqlConnection() throws ClassNotFoundException, SQLException{
Connection con;
Statement st;
ResultSet rs;
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/practice","root", "root");
System.out.println(con);
String s="Select * from orders"; // write any query here
st=con.createStatement();
rs=st.executeQuery(s); // query is performing on database
if (rs.next()) {
System.out.print(rs.getString(1)+" ");
System.out.print(rs.getString(2)+" ");
System.out.print(rs.getString(3)+ " ");
}
con.close();


}
}

Tuesday, 27 November 2012

Friday, 14 September 2012

Different ways to create objects in Java



This is a trivia. Yeah, it’s a bit tricky question and people often get confused. I had searched a lot to get all my doubts cleared.

There are four different ways (I really don’t know is there a fifth way to do this) to create objects in java:

1. Using new keyword
This is the most common way to create an object in java. I read somewhere that almost 99% of objects are created in this way.

MyObject object = new MyObject();

2. Using Class.forName()
If we know the name of the class & if it has a public default constructor we can create an object in this way.

MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();

3. Using clone()
The clone() can be used to create a copy of an existing object.

MyObject anotherObject = new MyObject();
MyObject object = anotherObject.clone();

4. Using object deserialization
Object deserialization is nothing but creating an object from its serialized form.

ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();

Now you know how to create an object. But its advised to create objects only when it is necessary to do so.

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) {}
}

}

Tuesday, 21 February 2012

Appending Serialized Objects to a file in java

// This program demonstrate how to write multiple bojects to a file and reading multiple objects from // a file using Object Serilization import java.io.Serializable; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.IOException; import java.io.File; import java.util.ArrayList; import java.io.OutputStream; //import java.io.BinaryWriteHelper; import java.util.Scanner; // class Student class Student implements Serializable { String name; int age; String clas; String dob; float cgp; } public class PS_Task3 { static ArrayList al=new ArrayList(); public static void main(String[] args) { int n; Scanner sc=new Scanner(System.in); //storeArrayList(); // This try block will write multiple objects to a file ObjectOutputStream oo=null; try { File file1 = new File ("student.bin"); if (!file1.exists()) { FileOutputStream fo=new FileOutputStream("student.bin"); oo=new ObjectOutputStream(fo); } else { oo = new AppendableObjectOutputStream (new FileOutputStream ("student.bin", true)); //FileOutputStream fo=new FileOutputStream("student.bin",true); //oo=new ObjectOutputStream(fo); } System.out.println("Please enter n how many student details you want to enter"); n=sc.nextInt(); Student s; for (int i=0;i