Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 52

1.

Write a program to Retrieving information from database using


JDBC
import java.sql.*;
class retrdata
{
public static void main(String arg[]) throws Exception
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Driver class loaded");
String cs="jdbc:oracle:thin:@localhost:1521:XE";
Connection con=DriverManager.getConnection(cs,"system","swapna");
System.out.println("Connection Established");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from student");
while(rs.next())
{
System.out.println("------------------------------");
System.out.println("**** Records are below *******");
System.out.println("------------------------------");
System.out.println("student name is "+rs.getString(1));
System.out.println("student roll no is "+rs.getInt(2));
System.out.println("student marks are "+rs.getInt(3));
System.out.println("******************************");
}
rs.close();
st.close();
con.close();
}

Execution
C:\ravi>javac retrdata.java
C:\ravi>java retrdata

Driver class loaded


Connection Established

**** Records are below ****


student name is

swapna

student roll no is

101

student marks are

850

**** Records are below***


student name is

santhu

student roll no is

102

student marks are

750

2. Write a JDBC Program by using Type 4 Driver


import java.sql.*;
class connection
{
public static void main(String arg[]) throws Exception
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Driver class loaded");
String cs="jdbc:oracle:thin:@localhost:1521:XE";
Connection con=DriverManager.getConnection(cs,"system","sairam");
System.out.println("Connection Established");
con.close();
}

Execution
First we need to set the class path
C:\Eswar\set classpath=.;C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib
\ojdbc14.jar
C:\eswar>javac connection.java
C:\eswar>java connection
Driver class loaded
Connection Established

3.Write a program which will insert the values into employee table using
JDBC.
First we have to create table in database
SQL> create table student
2 (
3 sname varchar2(10),
4 sid number(4),
5 marks number(5)
6 );
Table created.
import java.sql.*;
class insert
{
public static void main(String arg[]) throws Exception
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Driver class loaded");
String cs="jdbc:oracle:thin:@localhost:1521:XE";
Connection con=DriverManager.getConnection(cs,"system","sarith");
System.out.println("Connection Established");
Statement st=con.createStatement();
int n=st.executeUpdate("insert into student values('ravi',062,450)");
System.out.println(n+"record created");
int n1=st.executeUpdate("insert into student values('santhu',114,550)");
System.out.println(n+"record created");
st.close();
con.close();
}

Execution
C:\ravik>javac insertdata.java
C:\ravik>java insertdata

Driver class loaded


Connection Established
1record created
1record created

Database output
SQL> select * from student;
SNAME

SID

MARKS

----------

----------

naresh

057

750

ravi

062

450

santhu

114

550

----------

4.Write a program to update the employee table using JDBC.


import java.io.*;
import java.sql.*;
public class Update
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the empid: ");
int id=Integer.parseInt(br.readLine());
System.out.print("Enter the firstname: ");
String name=br.readLine();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection("jdbc:odbc:MCA");
PreparedStatement ps=c.prepareStatement("update Registration set cFirst_name=? where
emp_id=?");
Statement s=c.createStatement();
ps.setString(1,name);
ps.setInt(2,id);
boolean b=ps.execute();

System.out.println("\nRecord Updated\n");
ResultSet rs=s.executeQuery("select * from Registration");
System.out.println("emp_id\tF_name\tL_name\tAddress\tA_type");
System.out.println("--------------------------------------------------");
while(rs.next())
{
System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.getString(4)
+"\t"+rs.getString(5));
}

Execution
C:\ravik>javac Update.java
C:\ravik>java Update
Enter empid: 101
Enter the firstname: kiran
Record updated.

Database output
SQL> select * from Registration;
SNAME

SID

MARKS

----------

----------

----------

kiran

101

750

ravi

062

450

santhu

114

550

5.Write a program to delete a row from the employee table using JDBC.
import java.io.*;
import java.sql.*;
public class Delete
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the empid: ");


int id=Integer.parseInt(br.readLine());
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection("jdbc:odbc:MCA");
PreparedStatement ps=c.prepareStatement("delete from Registration where
emp_id=?");
Statement s=c.createStatement();
ps.setInt(1,id);
boolean b=ps.execute();
System.out.println("\nRecord Deleted\n");
System.out.println("emp_id\tF_name\tL_name\tAddress\tA_type");
System.out.println("--------------------------------------------------");
ResultSet rs=s.executeQuery("select * from Registration");
while(rs.next())
{
System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs
.getString(4)+"\t"+rs.getString(5))
}

6. Write a program sum of two nos by using RMI Interface program


import java.rmi.*;
import java.rmi.server.*;
public interface calc extends Remote
{
public int add(int a,int b) throws Exception;
}

Implementation program
import java.rmi.*;
import java.rmi.server.*;
public class calcimp extends UnicastRemoteObject implements cal
{

public calcimp() throws Exception


{

public int add(int a, int b) throws Exception


{
int z=a+b;
return z;
}

Mapping logic (Server Program)


import java.rmi.*;
import java.rmi.server.*;
public class calserver
{
public static void main(String arg[]) throws Exception
{
calcimp c=new calimp();
Naming.bind("rmi://localhost:1099/mca3b",b);
System.out.println("Server is started");
}

Client program
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
public class calcclient
{
public static void main(String arg[]) throws Exception
{
Scanner s=new Scanner(System.in);
int a,b,res;
System.out.println("Enter a Number");
a=s.nextInt();
System.out.println("Enter b value");

b=s.nextInt();
Remote r=Naming.lookup("rmi://localhost:1099/mca3b");
calc c=(calc)r;
res=c.add(a,b);
System.out.println("result is "+res);
}

Execution
First compile all programs if programs are compile it will generate all .class
files
C:\rmi>javac calc.java
C:\rmi>javac calcimp.java
C:\rmi>javac calcserver.java
C:\rmi>javac calcclient.java
C:\rmi>rmic calcimp
C:\rmi>start rmiregistry 1099

C:\rmi>java calcserver
Server is started

After minimum server next open new dos command page


C:\rmi>java calcclient
Enter a Number

20

Enter b value

35

result is

45

7. Write a program to insert employee data into database by using RMI


First we have to create table in database
SQL> create table emp1
2 (
3 ename varchar2(10),
4 esal varchar2(10000)
5 );
Table created.

Interface Program
import java.rmi.*;
import java.rmi.server.*;
public interface DataSet extends Remote
{
public int dataInsert(employee e) throws Exception;
}

Implementation program
import java.rmi.*;
import java.rmi.server.*;
import java.sql.*;
public class DataImpl extends UnicastRemoteObject implements DataSet
{
public DataImpl() throws Exception
{

public int dataInsert(employee e1) throws Exception


{
String name=e1.name;
int esal=e1.esal;
Class.forName("oracle.jdbc.driver.OracleDriver");
String cs="jdbc:oracle:thin:@localhost:1521:XE";
Connection c=DriverManager.getConnection(cs,"system","ravi");
PreparedStatement ps=c.prepareStatement("insert into em1 values(?,?)");

ps.setString(1,name);
ps.setInt(2,esal);
int cnt=ps.executeUpdate();
return cnt;
}

Employee class program


import java.rmi.*;
import java.rmi.server.*;
import java.sql.*;
import java.io.*;
class employee implements Serializable
{
String name;
int esal;

Sever program
import java.rmi.*;
import java.rmi.server.*;
class DataServer
{
public static void main(String arg[]) throws Exception
{
DataImpl d=new DataImpl();
Naming.bind("rmi://127.0.0.1:1099/swapna",b);
System.out.println("server is created");
}

Client Program
import java.rmi.*;
import java.rmi.server.*;
import java.io.*;
class DataClient
{

10

public static void main(String arg[]) throws Exception


{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
employee e=new employee();
System.out.println("enter name");
String name1=br.readLine();
System.out.println("enter esal");
int no1=Integer.parseInt(br.readLine());
e.name=name1;
e.esal=esal1;
Remote r=Naming.lookup("rmi://127.0.0.1:1099/swapna");
DataSet d=(DataSet)r;
int n=d.dataInsert(e);
System.out.println("no of records are inserted "+n);
}

Execution
First compile all programs if programs are compile it will generate all .class
files
C:\rmi>javac DataSet.java
C:\rmi>javac DataImpl.java
C:\rmi>javac DataServer.java
C:\rmi>javac empolyee.java
C:\rmi>javac DataClient.java
C:\rmi>rmic DataImpl
C:\rmi>start rmiregistry 1099

11

C:\rmi>java DataServer
Server is started

After minimum server next open new dos command page


C:\rmi\set classpath=.;C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\o jdbc14.jar
C:\rmi>java DataClient
enter name

ravi

enter esal

10000

no of records are inserted

C:\rmi>java DataClient
enter name

santhu

enter esal

50000

no of records are inserted

C:\rmi>java DataClient
enter name

naresh

enter esal

20000

no of records are inserted

After open the database observer records


SQL> select * from em1;
ENAME

ENO

----------

----------

Ravi

10000

santhu

50000

naresh

20000

8. Write a program to insert student data into database by using RMI


First we have to create table in database
SQL> create table emp1
2 (
3 ename varchar2(10),
4 eno number(5)
5 );
Table created.

12

Interface Program
import java.rmi.*;
import java.rmi.server.*;
public interface DataSet extends Remote
{
public int dataInsert(employee e) throws Exception;
}

Implementation program
import java.rmi.*;
import java.rmi.server.*;
import java.sql.*;
public class DataImpl extends UnicastRemoteObject implements DataSet
{
public DataImpl() throws Exception
{

public int dataInsert(employee e1) throws Exception


{
String name=e1.name;
int no=e1.no;
Class.forName("oracle.jdbc.driver.OracleDriver");
String cs="jdbc:oracle:thin:@localhost:1521:XE";
Connection c=DriverManager.getConnection(cs,"system","ramesh");
PreparedStatement ps=c.prepareStatement("insert into em1 values(?,?)");
ps.setString(1,name);
ps.setInt(2,no);
int cnt=ps.executeUpdate();
return cnt;
}

Employee class program


import java.rmi.*;
import java.rmi.server.*;

13

import java.sql.*;
import java.io.*;
class employee implements Serializable
{
String name;
int no;

Sever program
import java.rmi.*;
import java.rmi.server.*;
class DataServer
{
public static void main(String arg[]) throws Exception
{
DataImpl d=new DataImpl();
Naming.bind("rmi://127.0.0.1:1099/swapna",b);
System.out.println("server is created");
}

Client Program
import java.rmi.*;
import java.rmi.server.*;
import java.io.*;
class DataClient
{
public static void main(String arg[]) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
employee e=new employee();
System.out.println("enter name");
String name1=br.readLine();
System.out.println("enter no");
int no1=Integer.parseInt(br.readLine());

14

e.name=name1;
e.no=no1;
Remote r=Naming.lookup("rmi://127.0.0.1:1099/swapna");
DataSet d=(DataSet)r;
int n=d.dataInsert(e);
System.out.println("no of records are inserted "+n);
}

Execution
First compile all programs if programs are compile it will generate all .class
files
C:\rmi>javac DataSet.java
C:\rmi>javac DataImpl.java
C:\rmi>javac DataServer.java
C:\rmi>javac empolyee.java
C:\rmi>javac DataClient.java
C:\rmi>rmic DataImpl
C:\rmi>start rmiregistry 1099

C:\rmi>java DataServer
Server is started

After minimum server next open new dos command page


C:\rmi\set classpath=.;C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\o jdbc14.jar
C:\rmi>java DataClient
enter name

ravik

enter no

101

15

no of records are inserted

C:\rmi>java DataClient
enter name

santhu

enter no

102

no of records are inserted

C:\rmi>java DataClient
enter name

naresh

enter no

103

no of records are inserted

After open the database observer records


SQL> select * from em1;
ENAME

ENO

----------

----------

ravik

101

santhu

102

naresh

103

9. Write a EJB program by using stateless session bean


Client Program.
Business interface
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Hello extends EJBObject
{
public String sayHello() throws RemoteException;
}

Home Interface program


import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import java.rmi.RemoteException;
public interface HelloHome extends EJBHome

16

{
Hello create() throws RemoteException, CreateException;
}

Client program
import javax.ejb.*;
import javax.naming.*;
import java.util.*;
public class HelloClient
{
public static void main(String[] args)
{
try{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory"
);
env.put(Context.PROVIDER_URL, "t3://localhost:7001");
Context context=new InitialContext(env);
Object obj=context.lookup("FirstJndi");
HelloHome hor=(HelloHome)obj;
Hello ror=hor.create();
System.out.println(ror.sayHello());
}catch(Exception e)
{
e.printStackTrace();
}

Server Program
Business interface
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Hello extends EJBObject
{

17

public String sayHello() throws RemoteException;


}

Home Interface
import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import java.rmi.RemoteException;
public interface HelloHome extends EJBHome
{
Hello create() throws RemoteException, CreateException;
}

Session Beans Program


import javax.ejb.SessionBean;
import javax.ejb.CreateException;
import javax.ejb.SessionContext;
import javax.ejb.EJBException;
public class HelloBean implements SessionBean
{
SessionContext ctx=null;
public HelloBean() {
System.out.println("constructor");
}
public void ejbCreate() throws CreateException
{
System.out.println("ejb create method");
}
public void setSessionContext(SessionContext sessionContext) throws EJBException
{
ctx=sessionContext;
System.out.println("set session context method");
}
public void ejbRemove() throws EJBException

18

{
System.out.println("ejb remove method");
}
public void ejbActivate() throws EJBException
{
System.out.println("ejb activate method");
}
public void ejbPassivate() throws EJBException
{
System.out.println("ejb Passivate method");
}
public String sayHello()
{
return "Hello world";
}

Deployment descriptor xml file


<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>HelloEJB</ejb-name>
<home>HelloHome</home>
<remote>Hello</remote>
<ejb-class>HelloBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>

Vendor specification xml


<?xml version="1.0"?>

19

<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>HelloEJB</ejb-name>
<jndi-name>FirstJndi</jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>

Execution
Compile all programs in that folder
C:\first>javac *.java

How to create jar file


C:\first>jar cf fisrt.jar *.*

After create jar file we need to deploy jar file in server


How to deploy jar file into server

Next server is ready minimum server

Next open internet explorer


Enter url http://localhost:7001/console

20

After open page in that we have to click on


Deployments EJB Modules

Click on deploy a new EJB Module next upload where jar file available
After upload jar check that jar file. then click on my server folder next click on upload folder
next select first.jar click on Target module next click on deploy button.
After deploy jar file in your server then screen show like this

Next minimize the application server


Now open the command prompt execute your client program
C:\client>java HelloClient
Hello world
In server also show the session output

21

Constructor
set session context method ejb create method

10. Write a EJB program by using state full session bean


Client program.
Remote Interface
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Sal extends EJBObject
{
public float calcHRA()throws RemoteException;
public float calcDA()throws RemoteException;
public float calcTA()throws RemoteException;
public float calcPF()throws RemoteException;
public float calcIT()throws RemoteException;
public float calcGrossSal(float hra,float ta,float da,float pf,float it )throws
RemoteException;
public float calcNetSal(float gs,float it,float pf)throws RemoteException;
}

Home Interface
import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import java.rmi.RemoteException;
public interface SalHome extends EJBHome
{
Sal create(int eno,String ename,float bsal) throws RemoteException, CreateException;
}

Cilent program
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Properties;
public class SalaryClient

22

{
public static void main(String[] args)
{
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL, "t3://localhost:7001");
try
{
Context ctx = new InitialContext(p);
SalHome home = (SalHome)ctx.lookup("SalJndi");
System.out.println("lookup commpleted");
float sal=Float.parseFloat(args[0].trim());
Sal s = home.create(101,"raja",sal);
float hra=s.calcHRA();
float da=s.calcDA();
float ta=s.calcTA();
float pf=s.calcPF();
float it=s.calcIT();
float gs=s.calcGrossSal(hra,ta,da,pf,it);
float nets=s.calcNetSal(gs,it,pf);
System.out.println("Bs:"+sal+"Gross:"+gs+"Net Sal:"+nets);
}
catch(Exception e)
{
e.printStackTrace();
}

Server program
Remote Interface
import javax.ejb.EJBObject;
import java.rmi.RemoteException;

23

public interface Sal extends EJBObject


{
public float calcHRA()throws RemoteException;
public float calcDA()throws RemoteException;
public float calcTA()throws RemoteException;
public float calcPF()throws RemoteException;
public float calcIT()throws RemoteException;
public float calcGrossSal(float hra,float ta,float da,float pf,float it )throws
RemoteException;
public float calcNetSal(float gs,float it,float pf)throws RemoteException;
}

Server program
import javax.ejb.SessionBean;
import javax.ejb.CreateException;
import javax.ejb.SessionContext;
import javax.ejb.EJBException;
public class SalBean implements SessionBean
{
int eid;
String ename;
float bsal;
public SalBean()
{
System.out.println("SalBean.SalBean");
}
public void setSessionContext(SessionContext sessionContext) throws EJBException
{
System.out.println("salBean.setSessionContext");
}
public void ejbRemove() throws EJBException
{

24

System.out.println("salBean.ejbRemove");
}
public void ejbActivate() throws EJBException
{
System.out.println("salBean.ejbActivate");
}
public void ejbPassivate() throws EJBException
{
System.out.println("salBean.ejbPassivate");
}
public float calcHRA()
{
System.out.println("salBean.calcHRA");
return (bsal*0.2f);
}
public float calcDA()
{
System.out.println("salBean.calcDA");
return (bsal*0.13f);
}
public float calcTA()
{
System.out.println("salBean.calcTA");
return (bsal*0.1f);
}
public float calcPF()
{
System.out.println("salBean.calcPF");
return (bsal*0.05f);
}
public float calcIT()

25

{
System.out.println("salBean.calcIT");
return (bsal*0.02f);
}
public float calcGrossSal(float hra, float ta, float da, float pf, float it)
{
System.out.println("salBean.calcGrossSal");
return (bsal+hra+ta+pf+it);
}
public float calcNetSal(float gs, float it, float pf)
{
System.out.println("salBean.calcNetSal");
return (gs-(pf+it));
}
public void ejbCreate(int eno, String ename, float bsal) throws CreateException
{
System.out.println("SalBean.ejbCreate");
eid=eno;
this.ename=ename;
this.bsal=bsal;
}

Home Interface
import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import java.rmi.RemoteException;
public interface SalHome extends EJBHome
{
Sal create(int eno,String ename,float bsal) throws RemoteException, CreateException;
}

Deployment descriptor XML File


<?xml version="1.0" encoding="UTF-8"?>

26

<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans


2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>SalEJB</ejb-name>
<home>SalHome</home>
<remote>Sal</remote>
<ejb-class>SalBean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>

Vendor Specific XML File


<?xml version="1.0"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.//DTD WebLogic 7.0.0
EJB//EN" "http://www.bea.com/servers/wls700/dtd/weblogic-ejb-jar.dtd" >
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>SalEJB</ejb-name>
<jndi-name>SalJndi</jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>

Execution
Compile all programs in that folder
C:\first>javac *.java

How to create jar file


C:\first>jar cf second.jar *.*
After create jar file we need to deploy jar file in server
How to deploy jar file into server

27

Next server is ready minimum server

Next open internet explorer


Enter url http://localhost:7001/console

After open page in that we have to click on


Deployments EJB Modules

28

Click on deploy a new EJB Module next upload where jar file available
After upload jar check that jar file. then click on my server folder next click on upload folder
next select first.jar click on Target module next click on deploy button.
After deploy jar file in your server then screen show like this

Next minimize the application server


Now open the command prompt execute your client program
C:\client>java SalaryClient
Lookup is completed
1500
Sal is

1500

Gross

1950

Net

1820

11. Write program retrieve student information from the database by JNDI
Connection Polling.
First we have create student table in your database

29

SQL> create table stu


2 (
3 sno number(10),
4 sname varchar2(10)
5 );
Table created.

Insert some records into database


SQL> insert into stu values(10,'ravi');
1 row created.
SQL> insert into stu values(20,'santhu');
1 row created.

Java program
import javax.naming.*;
import java.util.*;
import java.sql.*;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class test1
{
public static void main(String[] args) throws Exception
{
try
{
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory"
);
env.put(Context.PROVIDER_URL, "t3://localhost:7001");
InitialContext ic=new InitialContext(env);
DataSource ds=(DataSource)ic.lookup("firstjndi");
Connection c=ds.getConnection();
Statement s=c.createStatement();

30

ResultSet rs=s.executeQuery("select * from stu");


while(rs.next())
{
System.out.println("no is "+rs.getInt(1));
System.out.println("name is "+rs.getString(2));
System.out.println("************************");
}

catch(Exception e)
{
e.printStackTrace();
}

Execution
First set class path where web logic jar file in your system
C:\test> set classpath=.;C:\bea\weblogic81\server\lib\weblogic.jar
C:\test>javac test1.java
Next start the application server

Next server is ready minimum server

31

Next open internet explorer


Enter url http://localhost:7001/console

After login click on services on lift side after click on click on jdbc under jdbc click on
connection pool option next right side configure a new jdbc connection pool click on once
next type your data base type

The above diagram I am choosing oracle next choose database driver name .
next click on continue

The above diagram


1. name of the jdbc connection pool we can write own connection pool
2. database name:- write your database name
How to check your database name open oracle in that write query
Select * from global_name now enter it will show your database.
Now your write your database name in application server

32

3. host name :
4. port no

localhost
:

1521

5. database username:- your database user name


6. database password :- your database password
Fill the all information into that blanks. Next click on continue next click on test Driver
Configuration if it is successful it will show connection successful message. Other wise
exception occurred
Next click on create and deploy option. after deploy over it will show like this

Next set the data source


Click on data source option in jdbc service on left side
Next click on configure a new JDBC Data Source
Next write JNDI name , the JNDI name should be match what I am giving in java program
DataSource ds=(DataSource)ic.lookup("firstjndi") this is java statement.
Now my JNDI Name is firstjndi next continue, next choose your pool name if your creating
new pool name at the time of creating connection pool otherwise select default pool name ,
next click continue , next click create .if it is created it will show like this

33

After successfully create jndi connection pool and data source object now open command
prompt
C:\test>java test1

Output :no is

10

name is

ravi

no is

20

name is

santhu

12. CORBA PROGRAM..


Interface Program
package HelloApp;
public interface Hello extends HelloOperations, org.omg.CORBA.Object,
org.omg.CORBA.portable.IDLEntity
{

package HelloApp;
abstract public class HelloHelper
{
private static String _id = "IDL:HelloApp/Hello:1.0";
public static void insert (org.omg.CORBA.Any a, HelloApp.Hello that)
{
org.omg.CORBA.portable.OutputStream out = a.create_output_stream ();
a.type (type ());
write (out, that);

34

a.read_value (out.create_input_stream (), type ());


}
public static HelloApp.Hello extract (org.omg.CORBA.Any a)
{
return read (a.create_input_stream ());
}
private static org.omg.CORBA.TypeCode __typeCode = null;
synchronized public static org.omg.CORBA.TypeCode type ()
{
if (__typeCode == null)
{
__typeCode = org.omg.CORBA.ORB.init ().create_interface_tc
(HelloApp.HelloHelper.id (), "Hello");
}
return __typeCode;
}
public static String id ()
{
return _id;
}
public static HelloApp.Hello read (org.omg.CORBA.portable.InputStream istream)
{
return narrow (istream.read_Object (_HelloStub.class));
}
public static void write (org.omg.CORBA.portable.OutputStream ostream,
HelloApp.Hello value)
{
ostream.write_Object ((org.omg.CORBA.Object) value);
}
public static HelloApp.Hello narrow (org.omg.CORBA.Object obj)
{
if (obj == null)

35

return null;
else if (obj instanceof HelloApp.Hello)
return (HelloApp.Hello)obj;
else if (!obj._is_a (id ()))
throw new org.omg.CORBA.BAD_PARAM ();
else
{
org.omg.CORBA.portable.Delegate delegate =
((org.omg.CORBA.portable.ObjectImpl)obj)._get_delegate ();
HelloApp._HelloStub stub = new HelloApp._HelloStub ();
stub._set_delegate(delegate);
return stub;
}

public static HelloApp.Hello unchecked_narrow (org.omg.CORBA.Object obj)


{
if (obj == null)
return null;
else if (obj instanceof HelloApp.Hello)
return (HelloApp.Hello)obj;
else
{
org.omg.CORBA.portable.Delegate delegate =
((org.omg.CORBA.portable.ObjectImpl)obj)._get_delegate ();
HelloApp._HelloStub stub = new HelloApp._HelloStub ();
stub._set_delegate(delegate);
return stub;
}

package HelloApp;
public final class HelloHolder implements org.omg.CORBA.portable.Streamable
{
public HelloApp.Hello value = null;
public HelloHolder ()

36

public HelloHolder (HelloApp.Hello initialValue)


{
value = initialValue;
}
public void _read (org.omg.CORBA.portable.InputStream i)
{
value = HelloApp.HelloHelper.read (i);
}
public void _write (org.omg.CORBA.portable.OutputStream o)
{
HelloApp.HelloHelper.write (o, value);
}
public org.omg.CORBA.TypeCode _type ()
{
return HelloApp.HelloHelper.type ();
}

package HelloApp;
public interface HelloOperations
{
String sayHello ();
}

Client Program
import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
public class HelloClient
{
public static void main(String args[])
{
try

37

{
ORB orb=ORB.init(args,null);
org.omg.CORBA.Object objRef=orb.resolve_initial_references("NameService");
NamingContext ncRef=NamingContextHelper.narrow(objRef);
NameComponent nc=new NameComponent("Hello","");
NameComponent path[]={nc};
Hello helloRef=HelloHelper.narrow(ncRef.resolve(path));
String hello=helloRef.sayHello();
System.out.println(hello);
}
catch(Exception e)
{
System.out.println("ERROR:"+e);
e.printStackTrace(System.out);
}

Server Program
import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
public class HelloServer
{
public static void main(String args[])
{
try
{
ORB orb=ORB.init(args,null);
HelloServant helloRef=new HelloServant();
orb.connect(helloRef);
org.omg.CORBA.Object objRef=orb.resolve_initial_references("NameService");
NamingContext ncRef=NamingContextHelper.narrow(objRef);

38

NameComponent nc=new NameComponent("Hello","");


NameComponent path[]={nc};
ncRef.rebind(path,helloRef);
java.lang.Object sync=new java.lang.Object();
synchronized(sync)
{
sync.wait();
}

catch(Exception e)
{
System.err.println("ERROR: "+e);
e.printStackTrace(System.out);
}

class HelloServant extends _HelloImplBase


{
public String sayHello()
{
return "\nHello World!!\n";
}

Execution

39

13. Write a .net program read the sname , sno and marks from console
using System;
namespace ConsoleSample
{
class hello
{
static void Main(string[ ] args )
{
Console.WriteLine("");
Console.WriteLine ("***** ");
Console.WriteLine ("Enter your name :");
string name = Console.ReadLine();
Console.WriteLine ("Enter your roll no :");
int rollno = Console.ReadLine();
Console.WriteLine ("Enter type marks :");
int marks = Console.ReadLine();
Console.WriteLine("Output: UR name is : "+ name);
Console.WriteLine("Output: UR roll no is : "+ rollno);
Console.WriteLine("Output: UR marks are : "+ marks);
}
}
}

Execution

14. Write a .net program by using structure concept.


using System;
struct company
{
public string Name;
public string CEO;
public int Year;
}
class TestStructure
{
public static void Main ()
{
Company com;
com.Name ="INFOSYS";
com.CEO ="NARAYANA MURTHY";
com.Year = 1996;

40

Console.WriteLine("COMPANY Name: " +com.Name);


Console.WriteLine("COMPANY CEO: " +com.Model );
Console.WriteLine("COMPANY ESTABLISH: "+com.Year);
}

Output:-

15. Write a .net program to illustrate String Operations.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace string_operations
{
class Program
{
static void Main(string[] args)
{
string s1, s2;
s1 = "hEllo WOrLd";
s2 = " HydErabad";
Console.WriteLine(s1+s2); // concatenation of two strings.
Console.WriteLine("The no. of chars in S1 " + s1.Length);
Console.WriteLine("S1 in Upper Case : " + s1.ToUpper());
Console.WriteLine("S1 in Lower Case : " + s1.ToLower());
Console.WriteLine("Index of 'E' in S1 : " + s1.IndexOf("E"));
Console.WriteLine("Replace World with Friend : " + s1.Replace("W", "F"));
Console.WriteLine("Remove llo from S1 : " + s1.Remove(2, 3));

41

Console.WriteLine("Print only some chars : " + s1.Substring(6, 5));


string name;
name = "

srinivas

";

Console.WriteLine("The Name is : " + name);


Console.WriteLine("Name after trimming : " + name.Trim());
Console.WriteLine("Triming in the first : " + name.TrimStart());
Console.WriteLine("Triming in the last : " + name.TrimEnd());
Console.Read();
}

OUTPUT:

16. Write a .net program to calculate add, sub, mul, div operations using
switch.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a, b, c;
Console.WriteLine("Enter any two numbers");
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
int number;
Console.WriteLine("Enter ur choice");
Console.WriteLine("\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division");
number = Convert.ToInt32(Console.ReadLine());
switch (number)

42

{
case 1:
c = a + b;
Console.WriteLine("The Addition is: "+c);
break;
case 2:
c = a - b;
Console.WriteLine("The Subtraction is: "+c);
break;
case 3:
c = a * b;
Console.WriteLine("The Multiplication is: " + c);
break;
case 4:
c = a / b;
Console.WriteLine("The Division is: " + c);
break;
}
Console.Read();
}
}
}

OUTPUT:

17. Write a .net program to calculate biggest of three nos.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a, b, c;
Console.WriteLine("Enter any three numbers");
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
c = Convert.ToInt32(Console.ReadLine());
if ((a > b) && (a > c))
Console.WriteLine("a is bigger");
else if (b > c)

43

Console.WriteLine("b is bigger");
else
Console.WriteLine("c is bigger");
Console.Read();
}
}
}

OUTPUT:

18. Write a .net program to demonstrate Inheritance concept.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace class_demo
{
class sample
// default class or builtin class
{
static void Main() // builtin method
{
one o = new one();
o.add(); // calling a non-local method of another class
two();
// calling a local method
Console.Read();
}
static void two() // local method to sample class
{
Console.WriteLine("This is a local method");
}
}
class one
// user defined class
{
public void add()
{
int a, b, c;
a = 10;
b = 20;
c = a + b;
Console.WriteLine("The sum is : " + c);
}
}
}

44

OUTPUT:

19.Write a .net program for method overloading.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace constructor_demo1
{
class Program
{
static void Main(string[] args)
{
MyClass mc = new MyClass();
mc = new MyClass(8);
Console.Read();
} }
class MyClass
{
public MyClass() // constructor with no param
{
Console.WriteLine("Constructor with no Param");
}
public MyClass(int r) // constructor with one param
{
Console.WriteLine("Constructor with one parameter : " + r);
}
}
}

OUTPUT:

45

20.Write a .net program Implementing Interfaces.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace interface_demo
{
class Program
{
static void Main(string[] args)
{
abc a = new abc();
a.one();
Console.Read();
} }
interface xyz
{
void one();// declaration of method
}
class abc : xyz // implementing the interface in to class
{
public void one()
{
Console.WriteLine("This is a method declared in the interface");
}
}
}

OUTPUT:

21.Write a .net program illustrate constructors.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace prog6
{

46

class Program
{
static void Main(string[] args)
{
MyConst mc = new MyConst(); // this will creates an object and calls the constructor.
Console.Read();
} }
class MyConst
{
public MyConst()
{

// constructor

Console.WriteLine("This is a constructor");
hello();
}
void hello()
{
Console.WriteLine("This is a normal method");
}

OUTPUT:

22. Write a .net program to demonstrate Exceptions.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace prog8
{
class Program
{
static void Main(string[] args)
{
int a, b, c;

47

Console.WriteLine("Enter the value for a,b");


a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
try // try block
{
c = a / b;
Console.WriteLine("the value of C is : " + c);
}
catch (Exception e) // catch block
{
//Console.WriteLine(e.Message); // this will print only the exception message
//Console.WriteLine(e)// it will entire story of the exception.
Console.WriteLine("Division by zero not possible! Please try again with other"); //
Our defined message
}
Console.Read();
}

OUTPUT:

23.Write a ADO.NET program to Inserting a New Record into DataBase.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace insert_sqlserver_demo
{
class Program
{
static void Main(string[] args)
{

48

//establishing the connection


SqlConnection con = new SqlConnection("Server=solaris;Initial Catalog=sample;
Integrated Security=True");
//opening the connection
con.Open();
//initializing the query
SqlCommand com = new SqlCommand("insert into student values(062,'Ravi
Kumar','MCA')", con);
//execute the query
com.ExecuteNonQuery();
//close the connection
con.Close();
//confirmation statement for the user
Console.WriteLine("New student admission is done.");
Console.Read();
}

OUTPUT:
First create a database and a table consists of
Sno

sname

course

057

naresh

mca

Then come to program then execute

After complete this process goto database and check wether the record is
insert or not.
Select * from student;
Previous

49

Sno

sname

course

057

naresh

mca

After compile of program


Sno

sname

course

057

naresh

mca

062

Ravi kumar

MCA

24.Write a .net program Event Handling example.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("event is performed");
} }
}

OUTPUT:
CLICK THE BUTTON

50

25.Write a .net programe to illustrate Arrays.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace arrays_demo
{
class Program
{
static void Main(string[] args)
{
int[] x = new int[5];
int i;
Console.WriteLine("Enter 5 values into the array");
// reading the values from the user to array
for (i = 0; i < 5; i++)
x[i] = Convert.ToInt32(Console.ReadLine());
// printing the values from the array
Console.WriteLine("The array Elements are : " );
for (i = 0; i < 5; i++)
Console.WriteLine(x[i]);
Console.Read();
}

OUTPUT:

51

26.Write a .net program to illustrate get, set properties.


using System;

class Example
{
int _number;
public int Number
{
get
{
return this._number;
}
set
{
this._number = value;
}
}
}
class Program
{
static void Main()
{
Example example = new Example();
example.Number = 5; // set { }
Console.WriteLine("\nset value using get property is: "+example.Number); // get { }
Console.Read();
}
}

OUTPUT:

52

You might also like