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

Saturday, 11 February 2012

Task 1 Description:Create a class LIST which represents a generalized list (By a sequential implementation) where insertions and deletions can be done arbitrarily at any point in the list. Implement all basic operations on it. (Insert, Delete, Isempty, Display etc). Specialize this class to represent STACK data structure (By inheriting the List class) and implement all basic operations (push, pop, Isempty, Display), on it

import java.util.ArrayList;

import java.util.Scanner;

class LIST // class List
{
ArrayList Al;
LIST()
{
Al=new ArrayList();
}
public boolean push(Object ele) // Stack push function
{
Al.add(ele);
System.out.println("The added element is "+ele);
return true;
}

public boolean isEmpty() // Stack is empty function
{
if(size()==-1)
return true;
else
return false;

}

public Object pop() // Stack pop function
{

String s="Sorry Stack is empty";
if(size()==-1)

return s;
else
return Al.remove(Al.size()-1);
}

public int size() // Stack size function
{
int n=Al.size();
if(n==0)
return -1;
else
return n;
}

public void display() // Stack display function
{

int n=Al.size();
if(Al.size()!=-1)
{
for(int i=0;i {
System.out.println(Al.get(n-1));
n=n-1;

}

}
else
System.out.println("Stack is empty");
}

}

class PS_Stack extends LIST // class Main
{
public static void main(String[] args)
{
LIST l=new LIST();
int n;
Scanner sc=new Scanner(System.in);
while(true)
{
System.out.println("\t*************************************************\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \tPlease choose your Option: \t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t1 to push element in to Stack\t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t2 to check Stack is empty \t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t3 to Pop element from stack\t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t4 to get size of the stack \t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t5 to dipslay stack elements \t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t6 to exit\t\t\t\t*\n\t*\t\t\t\t\t\t*");
System.out.println("\t*************************************************");
n=sc.nextInt();
switch (n)
{
case 1:
Object ole;
System.out.println("Enter an element to push in to stack :");
ole=sc.nextInt();
l.push(ole);
break;
case 2:
System.out.println(l.isEmpty());
break;
case 3:
System.out.println(l.pop());
break;
case 4:
System.out.println(l.size());
break;
case 5:
l.display();
break;
case 6:
System.exit(0);
break;
default:
System.out.println("Choose correct option");

}

}


}// End of main
}

Saturday, 4 February 2012

1. Write a C++ program to implement a complex class that contains two data members to store the real and imaginary parts of a complex number. Include member functions to initialize the data members to display the complex objects in the form ‘a+ib’. Use constructor and destructor for data members. Create a menu for the operations. Perform the following operations 1. Addition (use operator overloading) 2. Subtraction(use operator overloading) 3. Multiplication (use operator overloading) 4. Division Store the result in third object in the form of complex number. 2.

// Jayachandra
// addition,substraction,division and multiplication of two complex numbers
#include
using namespace std;

class Complex
{
public:

double getVolume()
{
cout<real + b.real;
com.imag = this->imag + b.imag;

return com;
}
// substraction
Complex operator-(const Complex& b)
{
Complex com;
com.real = this->real - b.real;
com.imag = this->imag - b.imag;

return com;
}
// multiplication
Complex operator*(const Complex& b)
{
Complex com;
com.real = this->real * b.real;
com.imag = this->imag * b.imag;

return com;
}
// division
Complex operator/(const Complex& b)
{
Complex com;
com.real = this->real / b.real;
com.imag = this->imag / b.imag;

return com;
}
private:
double real;
double imag;

};
// Main function for the program
int main( )
{
int n;
double i,r;
Complex Complex1;
Complex Complex2;
Complex Complex3;
cout<<"Plase enter real number for Complex1"<>r;
cout<<"Plase enter imaginary number Complex1"<>i;

Complex1.real_image(r,i);
cout<<"Plase enter real number for Complex2"<>r;
cout<<"Plase enter imaginary number Complex2"<>i;
Complex2.real_image(r,i);
Complex3 = Complex1 + Complex2;
for(;;)
{
cout<<"*****************************************\n*\t\t\t\t\t*"<>n;
switch(n)
{
case 1:
Complex3 = Complex1 + Complex2;
Complex3.getVolume();
break;
case 2:
Complex3 = Complex1 - Complex2;
Complex3.getVolume();
break;
case 3:
Complex3 = Complex1 * Complex2;
Complex3.getVolume();
break;
case 4:
Complex3 = Complex1 / Complex2;
Complex3.getVolume();

break;
case 5:
exit(0);
default:
cout<<"please choose correct option"<