Google

Wednesday, August 22, 2007

First Servlet Example

FirstServlet.java
public class FirstServlet implements javax.servlet.Servlet
{
public void init(javax.servlet.ServletConfig sc) throws javax.servlet.ServletException
{
System.out.println("In init");
}
public void service(javax.servlet.ServletRequest req,javax.servlet.ServletResponse res) throws javax.servlet.ServletException,java.io.IOException
{
System.out.println("In service");

java.io.PrintWriter out=res.getWriter();



out.println("Hello from First Servlet");
}
public void destroy()
{
System.out.println("In destroy");
}//destroy

public String getServletInfo()
{
return "FirstServlet";
}
public javax.servlet.ServletConfig getServletConfig()
{
//should return ServletConfig
return null;
}//getServletConfig

}//class

Tuesday, August 21, 2007

Calender Example in java

import java.util.Calendar;

public class CalendarExample
{
public static void main(String[] args)
{
Calendar cal = Calendar.getInstance();
int day = cal.get(Calendar.DATE);
int month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
int dow = cal.get(Calendar.DAY_OF_WEEK);
int dom = cal.get(Calendar.DAY_OF_MONTH);
int doy = cal.get(Calendar.DAY_OF_YEAR);

System.out.println("Current Date: " + cal.getTime());
System.out.println("Day: " + day);
System.out.println("Month: " + month);
System.out.println("Year: " + year);
System.out.println("Day of Week: " + dow);
System.out.println("Day of Month: " + dom);
System.out.println("Day of Year: " + doy);
}
}

Sunday, August 19, 2007

Program for Calculating Subject Marks in Java

package pkg;
import java.io.*;
public class Subjects
{

public static void main(String[] args)throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("How many numbers ?");
int n=Integer.parseInt(br.readLine());
int marks[]=new int[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter the marks:");
marks[i]= Integer.parseInt(br.readLine());
}
int tot=0;
System.out.println("The marks are :");
for(int i=0;i<n;i++)
{
System.out.println(marks[i]);
tot=tot+marks[i];
}
System.out.println("Total marks are :"+tot);
float percent = (float)tot/n ;
System.out.println("percentage = "+percent);
System.out.println("Length of array is "+marks.length);
}


}

Interfaces in Java

Interfaces
//interface example
interface Inter
{
void connect();
}
//implementation classes
class Oracle implements Inter
{
public void connect()
{
System.out.println("Connecting to Oracle database..........");
}
}
class Sybase implements Inter
{
public void connect()
{
System.out.println("Connecting to sybase database..............");
}
}
class Interface
{
public static void main(String[] args) throws Exception
{
//Accepting database name from commandline args & store it in an obj c

Class c = Class.forName(args[0]);
{
//create an obj to that class whose name is in c

MyInter mi=(MyInter)c.newInstance();
{
//call conect method of obj

mi.connect();

}

}

}

}

Thursday, August 16, 2007

Porting Data From one Database to another Database using JDBC

import java.io.*;
import java.net.*;
import java.sql.*;
public class Porting
{
static final String DRIVER_NAME = "sun.jdbc.odbc.JdbcOdbcDriver";
static Connection conn = null,con1=null;
public static void main(String[] args)
{
try
{
Class.forName(DRIVER_NAME);
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnectio("jdbc:odbc:aaa");
con1=DriverManager.getConnection("jdbc:mysql://localhost:3306/employee/user=null&password=null");
if(con1==null)
{
System.out.println("Connection not established");
}
else
{
System.out.println("Connection established");
}
PreparedStatement prp=null;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from tablename");
int i=1;
while (rs.next())
{
prp=conn.prepareStatement("insert into tablename values (?,?)");
prp.setString(1,rs.getString("uid"));
prp.setString(2,rs.getString("PWD"));
int x=prp.executeUpdate();
if (x!=0)
{
System.out.println(i+" record is inserted");
}
else
{
System.out.println("record is not inserted");
}
i++;
}
rs.close();
stmt.close();
}
catch (ClassNotFoundException cnfe)
{
System.err.println("ClassNotFoundException Was Thrown");
cnfe.printStackTrace();
}
catch (SQLException sqle)
{
System.err.println("SQLException Was Thrown");
sqle.printStackTrace();
}
finally
{
try
{
if(conn!=null)
{
conn.close();
}
if(con1!=null)
{
con1.close();
}
}
catch(Exception e)
}
}
}

Creating Tables in Java using JDBC

import java.sql.*;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.*;
import java.io.*;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;

class CreateTableEx{
public static void main(String args[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection"jdbc:mysql://localhost:3306/test/user=yes&password=null");
Statement stmt=con.createStatement();
stmt.executeUpdate("create table tablename(eno integer,ename varchar(20),esal float,eaddr varchar(30))");
stmt.executeUpdate("insert into table values(01,'ramesh',100,'banjarahills')");
stmt.executeUpdate("insert into table values(02,'ram',5000,'ameerpet')");
stmt.close();
stmt.close();
}
catch(SQLException e)
{
System.out.println(e);
}
catch(ClassNotFoundException e){
System.out.println(e);
}

}
}

sample program for adding two numbers in java

import java.lang.*;

class Sum

{

public Static voidmain(String args[]);

{ int x,y;

x=10;

y=20;

int z=x+y;

System.out.println("The sum of two numbers is "+z);

}

}