Java Lab Final
Java Lab Final
sort.java
import java.io.*;
class sort
{
public static void main(String args[])throws IOException
{
int a[];
int p,n,c,t,i;
DataInputStream ds=new DataInputStream(System.in);
System.out.println("Enter the number of element");
n=Integer.parseInt(ds.readLine());
a=new int[n];
System.out.println("Enter the "+n+" number");
for(i=0;i<n;i++)
{
a[i]=Integer.parseInt(ds.readLine());
}
for(p=0;p<n-1;p++)
{
for(c=0;c<n-1-p;c++)
{
if(a[c]>a[c+1])
{
t=a[c];
a[c]=a[c+1];
a[c+1]=t;
}
}
}
System.out.println("ASCENDING \t DESCENDING");
for(i=0;i<n;i++)
{
System.out.println(a[i]+"\t\t\t"+a[n-1-i]);
}
}
}
OUTPUT
SOURCE CODE:
alpha.java
import java.io.*;
class alpha
{
public static void main(String args[])throws IOException
{
String name[],t;
int i,j,x,n;
DataInputStream ds=new DataInputStream(System.in);
System.out.println("Enter the number of member");
n=Integer.parseInt(ds.readLine());
name=new String[n];
System.out.println("Enter the "+n+" name");
for(i=0;i<n;i++)
{
name[i]=ds.readLine();
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
x=name[j].compareTo(name[j+1]);
if(x>0)
{
t=name[j];
name[j]=name[j+1];
name[j+1]=t;
}
}
}
System.out.println("ALPHABETICAL ORDER");
for(i=0;i<n;i++)
System.out.println(name[i]);
}
}
OUTPUT:
SOURCE CODE:
file.java
import java.io.*;
class file
{
public static void main(String args[])throws IOException
{
int empno,salary;
String name,design;
int more;
FileOutputStream fos=new FileOutputStream("emp.java");
PrintWriter write=new PrintWriter(fos);
DataInputStream ds=new DataInputStream(System.in);
do
{
System.out.println("Enter employee no:");
empno=Integer.parseInt(ds.readLine());
System.out.println("Enter employee name:");
name=(ds.readLine());
System.out.println("Enter employee salary no:");
salary=Integer.parseInt(ds.readLine());
System.out.println("Enter designation:");
design=(ds.readLine());
write.println(empno+"\t" +name+ "\t" +design+"\t"+salary);
System.out.println("add more records=1,exit=0");
more=Integer.parseInt(ds.readLine());
}
while(more==1);
write.close();
}
}
OUTPUT
SOURCE CODE
fileread.java
import java.io.*;
class fileread
{
public static void main(String args[])throws IOException
{
int empno,salary;
String name,design;
FileInputStream fis=new FileInputStream("emp.java");
InputStreamReader isn=new InputStreamReader(fis);
StreamTokenizer tokens=new StreamTokenizer(isn);
while(tokens.nextToken()!=tokens.TT_EOF)
{
empno=(int)tokens.nval;
tokens.nextToken();
name=tokens.sval;
tokens.nextToken();
salary=(int)tokens.nval;
design=tokens.sval;
tokens.nextToken();
salary=(int)tokens.nval;
System.out.println(empno+" "+name+" "+salary+" "+design);
}
}
}
OUTPUT
SOURCE CODE:
except.java
import java.io.*;
import java.lang.*;
import java.util.*;
class except
{
public static void main(String args[])throws IOException
{
int ch;
DataInputStream ds=new DataInputStream(System.in);
do
{
System.out.println("menu");
System.out.println("1.add");
System.out.println("2.str");
System.out.println("3.array");
System.out.println("4.exit");
System.out.println("Enter your choice");
ch=Integer.parseInt(ds.readLine());
switch(ch)
{
case 1:
int a=5,b=0,c;
try
{
c=a/b;
System.out.println("Result"+c);
}
catch(Exception e)
{
System.out.println(e.toString());
e.printStackTrace();
}
break;
case 2:
String s="abcds";
try
{
System.out.println(s.charAt(5));
}
catch(Exception e)
{
System.out.println(e.toString());
e.printStackTrace();
}
break;
case 3:
int x[]=new int[5];
try
{
System.out.println(x[5]);
}
catch(Exception e)
{
System.out.println(e.toString());
e.printStackTrace();
}
break;
case 4:
break;
}
}
while(ch<5);
}
}
OUTPUT:
SOURCE CODE:
Jdbcquery.java
import java.sql.* ;
public class jdbcquery
{
public static void main( String args[] )
{
try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ) ;
Connection conn = DriverManager.getConnection(
"jdbc:odbc:studentdsn" ) ;
Statement stmt = conn.createStatement() ;
ResultSet rs = stmt.executeQuery( "SELECT * FROM studenttable");
while( rs.next() )
{
System.out.print( rs.getString(1) ) ;
System.out.print( rs.getString(2) ) ;
}
rs.close() ;
stmt.close() ;
conn.close() ;
}
catch( SQLException se )
{
System.out.println( "SQL Exception:" ) ;
while( se != null )
{
System.out.println( "State : " + se.getSQLState() ) ;
System.out.println( "Message: " + se.getMessage() ) ;
System.out.println( "Error : " + se.getErrorCode() ) ;
se = se.getNextException() ;
}
}
catch( Exception e )
{
System.out.println( e ) ;
}
}
}
OUTPUT:
Studenttable:
SOURCE CODE:
Jdbcinsertstr.java
import java.sql.* ;
public class jdbcinsertstr
{
public static void main( String args[] )
{
int returnvalue,no1=105; String name1="sema";
try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ) ;
Connection conn =
DriverManager.getConnection("jdbc:odbc:studentdsn" ) ;
Statement stmt = conn.createStatement() ;
// insert into studenttable values(105,'sema');
String str1="insert into studenttable values("+no1+",'"+name1+"')";
returnvalue=stmt.executeUpdate(str1);
System.out.println("inserted "+returnvalue+" row in the database");
stmt.close() ;
conn.close() ;
}
catch( SQLException se )
{
System.out.println( "SQL Exception:" ) ;
while( se != null )
{
System.out.println( "State : " + se.getSQLState() ) ;
System.out.println( "Message: " + se.getMessage() ) ;
System.out.println( "Error : " + se.getErrorCode() ) ;
se = se.getNextException() ;
}
}
catch( Exception e )
{
System.out.println( e ) ;
}
}
}
OUTPUT:
studenttable
SOURCE CODE:
Jdbcupdate.java
import java.sql.* ;
public class jdbcupdate
{
public static void main( String args[] )
{
int returnvalue,no1=103; String name1="Vini";
try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ) ;
Connection conn =
DriverManager.getConnection("jdbc:odbc:studentdsn" ) ;
Statement stmt = conn.createStatement() ;
// update studenttable set studname='Meena' where studid=103;
String sqlbuffer="UPDATE studenttable set studname='"+name1+"'
where studid="+no1;
returnvalue=stmt.executeUpdate(sqlbuffer);
System.out.println("Updated "+returnvalue+" row in the database");
stmt.close() ;
conn.close() ;
}
catch( SQLException se )
{
System.out.println( "SQL Exception:" ) ;
while( se != null )
{
System.out.println( "State : " + se.getSQLState() ) ;
System.out.println( "Message: " + se.getMessage() ) ;
System.out.println( "Error : " + se.getErrorCode() ) ;
se = se.getNextException() ;
}
}
catch( Exception e )
{
System.out.println( e ) ;
}
}
}
OUTPUT:
Jdbcdelete.java
import java.sql.*;
public class jdbcdelete
{
public static void main( String args[] )
{
int returnvalue,no1=103;
try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ) ;
Connection conn = DriverManager.getConnection(
"jdbc:odbc:studentdsn" ) ;
Statement stmt = conn.createStatement() ;
String sqlbuffer="Delete from studenttable where studid="+no1;
System.out.println(sqlbuffer);
returnvalue=stmt.executeUpdate(sqlbuffer);
System.out.println("deleted "+returnvalue+" rows in the database");
stmt.close() ;
conn.close() ;
}
catch( SQLException se )
{
System.out.println( "SQL Exception:" ) ;
while( se != null )
{
System.out.println( "State : " + se.getSQLState() ) ;
System.out.println( "Message: " + se.getMessage() ) ;
System.out.println( "Error : " + se.getErrorCode() ) ;
se = se.getNextException() ;
}
}
catch( Exception e )
{
System.out.println( e ) ;
}}}
OUTPUT:
jdpre.java
import java.sql.*;
import java.io.*;
public class jdpre
{
public static void main(String arg[])
{
DataInputStream x=new DataInputStream(System.in);
PreparedStatement ps; int rno;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:studdsn");
System.out.println("enter the no");
rno=Integer.parseInt(x.readLine());
System.out.println("enter the name");
String sname=x.readLine();
System.out.println("enter the address");
String sadd=x.readLine();
System.out.println("enter the course");
String scourse=x.readLine();
ps=con.prepareStatement("insert into studtable values(?,?,?,?)");
ps.setInt(1,rno);
ps.setString(2,sname);
ps.setString(3,sadd);
ps.setString(4,scourse);
ps.executeUpdate();
System.out.println("1 row is inserted");
ps.close() ;
con.close() ;
}
catch(Exception e)
{
System.out.println(e);
}}}
OUTPUT:
studtable:
SOURCE CODE:
db.java
import java.sql.*;
public class db
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb,
*.accdb)};DBQ=" + "g:\\jdbcdemo.accdb";
Connection con = DriverManager.getConnection(url);
System.out.println("Connection Successful");
Statement select = con.createStatement();
ResultSet result = select.executeQuery("SELECT * FROM student");
while (result.next())
{
String rno = result.getString(1);
String name = result.getString(2);
String m1 = result.getString(3);
String m2 = result.getString(4);
System.out.println(rno + " " + name+" "+m1+" "+m2);
}
}
catch (Exception ex)
{
System.err.println("Got an exception!");
System.err.println(ex.getMessage());
}
}
}
OUTPUT:
studenttable:
SOURCE CODE:
TreeExample.java
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class TreeExample
{
JFrame f;
TreeExample()
{
f=new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultMutableTreeNode style=new DefaultMutableTreeNode("Style");
DefaultMutableTreeNodecolor=new DefaultMutableTreeNode("color");
DefaultMutableTreeNode font=new DefaultMutableTreeNode("font");
style.add(color);
style.add(font);
DefaultMutableTreeNode red=new DefaultMutableTreeNode("red");
DefaultMutableTreeNode blue=new DefaultMutableTreeNode("blue");
DefaultMutableTreeNode black=new DefaultMutableTreeNode("black");
DefaultMutableTreeNode green=new DefaultMutableTreeNode("green");
color.add(red); color.add(blue); color.add(black); color.add(green);
JTree jt=new JTree(style);
f.add(jt);
f.setSize(200,200);
f.setVisible(true);
}
public static void main(String[] args)
{
new TreeExample();
}
}
OUTPUT:
SOURCE CODE:
TextFieldExampleact.java
importjavax.swing.*;
import java.awt.event.*;
public class TextFieldExampleact implements ActionListener
{
JTextField tf1,tf2,tf3;
JButton b1,b2;
TextFieldExampleact()
{
JFrame f= new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tf1=new JTextField();
tf1.setBounds(50,50,150,20);
tf2=new JTextField();
tf2.setBounds(50,100,150,20);
tf3=new JTextField();
tf3.setBounds(50,150,150,20);
tf3.setEditable(false);
b1=new JButton("+");
b1.setBounds(50,200,50,50);
b1.addActionListener(this);
f.getContentPane().add(tf1);f.getContentPane().(tf2);
f.getContentPane().add(tf3);f.getContentPane().add(b1);
f. setSize(300,300);
f.getContentPane().setLayout(null);
f..setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s1=tf1.getText();
String s2=tf2.getText();
int a=Integer.parseInt(s1);
int b=Integer.parseInt(s2);
int c=0;
if(e.getSource()==b1){
c=a+b;
}
String result=String.valueOf(c);
tf3.setText(result);
}
public static void main(String[] args)
{
new TextFieldExampleact();
}
}
OUTPUT:
SOURCE CODE:
lab2.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lab2 extends JFrame
{
private JLabel label1, label2, label3,label4,disp;
private JTextField t,t1;
private JRadioButton r4,r5,r6;
private JCheckBox r1, r2, r3;
private JButton b1,b2;
private ButtonGroup radioGroup;
public lab2()
{
super( "Testing JLabel" );
Container c = getContentPane();
c.setLayout( new FlowLayout() );
label1 = new JLabel( "ENTER YOUR NAME " );
disp= new JLabel( " " );
c.add( label1 );
t = new JTextField( " ");
c.add( t );
label2 = new JLabel( "ENTER YOUR AGE");
c.add( label2 );
t1 = new JTextField( " ");
c.add( t1 );
label3 = new JLabel();
label3.setText( "SELECT YUOR S/W" );
c.add( label3 );
r1 = new JCheckBox( "oracle", false );
c.add( r1 );
r2 = new JCheckBox( "VB", false );
c.add( r2 );
r3 = new JCheckBox( "Java", false );
c.add( r3);
show();
label4 = new JLabel();
label4.setText( "SELECT YUOR CITY" );
c.add( label4 );
r4 = new JRadioButton( "Delhi", false);
c.add( r4 );
r5 = new JRadioButton( "Mumbai", false );
c.add( r5 );
r6 = new JRadioButton( "Culcutta", false );
c.add( r6);
radioGroup = new ButtonGroup();
radioGroup.add( r4 );
radioGroup.add( r5 );
radioGroup.add( r6 );
setSize( 500, 300 );
show();
b1=new JButton("OK");
b2=new JButton("cancel");
c.add(b1);
c.add(b2);
c.add(disp);
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
disp.setText("program will be processed\n");
}
});
b2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
t.setText(" ");
t1.setText(" ");
disp.setText(" ");
}
});
AddServerInt.java
import java.rmi.*;
public interface AddServerInt extends Remote
{
double add(double d1,double d2) throws RemoteException;
}
AddServerImp.java
import java.rmi.*;
import java.rmi.server.*;
public class AddServerImp extends UnicastRemoteObject implements
AddServerInt
{
public AddServerImp() throws RemoteException
{}
public double add(double d1,double d2) throws RemoteException
{
return d1+d2;
}
}
AddServer.java
import java.net.*;
import java.rmi.*;
public class AddServer
{
public static void main(String args[])
{
try{
AddServerImp addServer=new AddServerImp();
Naming.rebind("AddServer",addServer);
}
catch(Exception e)
{
System.out.println("Exception"+e);
}
}
}
AddClient.java
import java.rmi.*;
public class AddClient
{
public static void main(String args[])
{
try
{
String addServerURL="rmi://" + args[0] + "/AddServer";
AddServerInf addServer=(AddServerInf)Naming.lookup(addServerURL);
System.out.println("First number is "+ args[1]);
double d1=Double.valueOf(args[1]).doubleValue();
System.out.println("Second number is"+args[2]);
double d2=Double.valueOf(args[2]).doubleValue();
System.out.println("Sum is"+addServer.add(d1,d2));
}
catch(Exception e)
{
System.out.println("Exception e:"+e.getMessage());
}
}
}
\bin>javac AddServerInt.java
\bin>javac AddServerImp.java
\bin>javac AddServer.java
\bin>javac AddClient.java
\bin>rmic AddServerImp
\bin>rmiregistry