Thursday, September 29, 2011

PrepareStmt_insert operation


PrepareStmt_insert operation

import java.sql.*;
class PrepareStmt
{
public static void main(String[] args) throws Exception
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
PreparedStatement pstmt=con.prepareStatement("insert into emp3 values(?,?)");
pstmt.setInt(1,10);
pstmt.setString(2,"anjali");
int a=pstmt.executeUpdate();
System.out.println("insert record is with Prepare Statement......"+a);
}
}



Using Index Retrive Data From The Table
retrieve-records

Prepare_statement


Prepare_statement

// PrepareStatement retriving the values from table student in a database

import java.sql.*;
public class Prepare_statement

{
public static void main(String args[])
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("thin driver is registerd");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
System.out.println("Connection is established to DB");
PreparedStatement pstmt=con.prepareStatement("select *from student where sno=?");
System.out.println("preparedStatement object is created");
pstmt.setInt(1,15);
ResultSet rs=pstmt.executeQuery();
System.out.println("sno"+" "+"name"+" "+"place");
System.out.println("--------------------------------------");
while(rs.next())
{
System.out.println();
System.out.print(rs.getInt("sno")+" ");
System.out.print(rs.getString("name")+" ");
System.out.print(rs.getString("place"));
}
pstmt.executeQuery();
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Registeration faild");
}
}
}

/*
Explanation:
Table in Database:
create table student(sno number(10),name varchar2(20),place varchar2(20));
prepared statement is improves the performence

*/


Retrieve Records


Retrieve_Records

//Retrieving the values from table student in a database

import java.sql.*;
public class Retrieve_Records

{
public static void main(String args[])
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("thin driver is registerd");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
System.out.println("Connection is established to DB");
Statement stmt=con.createStatement();
System.out.println("Statement object is created");
ResultSet rs=stmt.executeQuery("select *from student");
System.out.println("sno"+" "+"name"+" "+"place");
System.out.println("--------------------------------------");
while(rs.next())
{
System.out.println();
System.out.print(rs.getInt("sno")+" ");
System.out.print(rs.getString("name")+" ");
System.out.print(rs.getString("place"));
}
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Registeration faild");
}
}
}

/*
Explanation:

For Select Querys we have to use the "executeQuery" method
it return the Results into a ResultSet object
by using next() method we can move the next row from one row to anethor
here we used differnt type getter method(we can use getString() for all data types)
but its not recomended
TRy with different type select querys

*/



Using Index Retrive Data From The Table
Using Prepared Statement 

Batch Updates


BatchUpdates

import java.sql.*;
class BatchUpdates
{
public static void main(String[] args) throws Exception
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
Statement stmt=con.createStatement();
String s1="insert into emp3 values(1,'rahul')";
String s2="insert into emp3 values(2,'arun')";
String s3="insert into emp3 values(3,'pavan')";
String s4="insert into emp3 values(4,'gopi')";
String s5="insert into emp3 values(2,'arun')";
String s6="insert into emp3 values(2,'arun')";
stmt.addBatch(s1);
stmt.addBatch(s2);
stmt.addBatch(s3);
stmt.addBatch(s4);
stmt.addBatch(s5);
stmt.addBatch(s6);
int a[]=stmt.executeBatch();
for(int i=0;i<=a.length;i++) { System.out.println("no of rows inserted:------->"+a[i]);
}
}
}




Wednesday, September 28, 2011

Connect To DataBase and Creating a File

Connect To Data Base and Creating a File
import java.sql.*;
class DBConnect
{
public static void main(String[] args) throws Exception
{

//Step 1: register the Driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
//Step 2: get the connetion using the Connection interface

Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
//Step 3: get statement reference variaable

Statement stmt=con.createStatement();
//Step 4: execute Query

stmt.executeUpdate("create table emp3(eno number(5),name varchar2(20))");
System.out.println("table is created......");
//Step 5: close the connetion

con.close();
System.out.println("connection is closed");
}
}

Monday, September 26, 2011

Retrive the data from the table in db using java(jdbc)


DisplayRecord

import java.sql.*;
class DisplayRecord
{
public static void main(String[] args) throws Exception
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select eno,name from emp3");
System.out.println(rs);
while(rs.next())
{
int deno=rs.getInt("eno");
System.out.println("Eno: "+deno);
String s=rs.getString("name");
System.out.println("Nane: "+s);
}
}
}

Sunday, September 25, 2011

search a File in root directorys

/*
This programe searches the given file Which is in "C-directory" only in sub floders also
and also displys the all files and floders in directory and thair path also
if the u want to seach in the anether dir for that change the dir name in the programe
*/
import java.io.*;
public class FileSearch {

public void search( String path,String searchfile) {
File file = new File( path );
File[] filelist = file.listFiles();

for ( File f : filelist ) {
if ( f.isDirectory() ) {
search( f.getAbsolutePath(),searchfile );
System.out.println( "Directory:" + f.getAbsoluteFile() );
if(f.getName().equals(searchfile)){
System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< break;
}
}
else {
System.out.println("------------------------------");
System.out.println( "File:" + f.getAbsoluteFile() );
if(f.getName().equals(searchfile)){
System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< break;
}
}
}
}
public static void main(String[] args) {
FileSearch fs = new FileSearch();
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("press ENTER or enter the file name with extention");
String searchfile=br.readLine();


File[] dir=File.listRoots();
/*
for(int j=0;j<=dir.length;j++)
{
System.out.println(dir[j]);
fs.search(dir[j],searchfile);
}
*/

fs.search("C:/",searchfile); //call the function
}
catch(NullPointerException e)
{
System.out.println("enter abs file "+e);
}
catch(IOException e)
{
System.out.println("enter abs file "+e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("error "+e);
}
catch(Exception e)
{
System.out.println("enter abs file "+e);
}
finally{
System.out.println("you get null pointer if the file not get or not specifiy the absolute dir");
System.out.println("----------------------------------------------------------------------------");
fs=null;
}
}
static{
System.out.println("----------------------------------------------------------------------------");
System.out.println("please enter the file in the c dir only");
System.out.println("----------------------------------------------------------------------------");
}
}

Friday, April 1, 2011

CaseSencitive



/*
RULE: identofiers are case sensitive ( balance and Balance are two different)
*/


class CaseSencitive
{
public static void main(String[] args)
{
int bal;
int Bal;
System.out.println("legal identifiers");
}
}

Illegel

/*
RULE: following are some illegel identifiers
*/

class Illegel
{
public static void main(String[] args)
{
int 7g;
int .j;
int -m;
int :b;
int s#;
int int;
int for;
int enum;
System.out.println("illegal identifiers");
}
}

DontUse

/*
RULE: dont use keywords as identifiers
*/
class DontUse
{
public static void main(String[] args)
{
int if;
int for;
System.out.println(" illegal start of expressition");
}
}

NoLimit

/*
RULE: there is no limit to the number of characters in the identifiers
*/

class NoLimit
{
public static void main(String[] args)
{
int asdf_hghighiggu_jhgjhghgu_kjghuge_njgh$jfhfghaf;
System.out.println("No Limit ");
}
}

After the first character

/*
RULE: After the first character ( $ or _ or character ) identifiers contains
any thing i.e numbers also
*/

class AfterFirst
{
public static void main(String[] args)
{
int a333424;
int _34we;
int $a23deu;
System.out.println("legal identifiers");
}
}

identifyrs starts with characters

/*
RULE: identifyrs starts with characters(a to z or A to Z)
or starts with underscore (_) and currency ($)
not starts with numbers
*/

class StartWith
{
public static void main(String[] args)
{
int $x;
int _a;
int m;
System.out.println("legal identifiers");
}
}

classes and interfaces

/*
RULE: classes and interfaces
the first letter of the word should be Capital and connectig word letter also capital
the name should be nouns
*/

class Dog
{
public static void main(String[] args)
{
System.out.println("Hello Dog!");
}
}

class PrintWriter
{
System.out.println("this is class");
}

interface Car
{
}
interface Runnable
{
}

Methods

/*
RULE: The first letter should be small letter and followed
words shold be capital letters
*/
class Methods
{
int getBalance(int accno)
{
System.out.prinln("balance is")
}
void setBalance(int acc,int amount)
{
}
public static void main(String[] args)
{

System.out.println("Hello World!");
}
}

Variables

/*
RULE: like methods rules
starting letter should be small then next word with capital letter
SUN recomended that it should be small ans good meaning full sound
*/

class Variables
{
public static void main(String[] args)
{
int buttonWidth;
int bal;
double accountNo;
System.out.println("legel convetions");
}
}

Constants

/*
RULE: constants are should be capital letters
they are used final and static keywords
*/

class Constants
{
static final double accountNumber=22323543;
public static void main(String[] args)
{
final float PI=22.7f;
System.out.println("legel constant code convention");
}
}

Friday, February 25, 2011

Final_DataTypes

//RULE:we can use the final keyword with DataTypes but values of variables are not modifies later

public class Final_DataTypes
{
public void testMethod()
{
final int x=10;
System.out.println("ok");
}
}

Final_Methods

//RULE:we can use the final keyword with methods but methods are not overrides

public class Final_Methods
{
public final void testMethod()
{
final int x=10;
System.out.println("ok");
}
}

Final_Interface

//RULE:we can not use the final keyword to the interface

final interface Final_Interface
{
public static void main(String ar[])
{
System.out.println("illegal combination of final and interface");
}
}