list of programs (advance java) - · web viewmanav rachna college of engineering advance java...
TRANSCRIPT
Manav Rachna College of Engineering
Advance Java Lab Manual
Submitted By:-
Kirti Aggarwal
List Of Programs (Advance Java)
1. WAP to swap two numbers without using third variable.
2. WAP to check whether a number is Armstrong or not.
3. WAP to implement the Concept of Function Overloading.
4. WAP to implement the Concept of Function Overriding.
5. WAP to implement the Exceptional Handling.
6. WAP of an applet that receives two numerical values as the input from user and displays the sum of these two numbers.
7. WAP for displaying product list along with their prices and then allow user to buy any1 item from them with required quantity.
8. WAP to implement multithreading(three threads using single run method).
9. WAP to implement the calculator.
10. WAP to implement the URL.
11. WAP to implement the InetAddress.
12. WAP for Sending e-mail in Java.
13. WAP to implement Single Client-Server Communication.
14. WAP to implement the Login_Id Form using JDBC.
15. WAP to implement the SQL commands using JDBC.
16. WAP to implement the List.
17. WAP to implement the JTrees.
18. WAP to implement the JTable.
19. WAP to create the table using JDBC.
20. WAP to implement Remote Method Invocation.
Program:-1
WAP for swapping of two numbers.
package swap;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int a,b;
System.out.println("Enter the two numbers: ");
Scanner s= new Scanner(System.in);
a = s.nextInt();
b = s.nextInt();
System.out.println("Number before swapping:" +a+" " +b);
a=a+b;
b=a-b;
a=a-b;
System.out.println("Number after swapping:" +a+" " +b);
}
}
OUTPUT
Enter the two numbers:
34 56
Number before swapping:34 56
Number after swapping:56 34
Program:-2
WAP to find whether number is Armstrong or not.
package armstrng;
import java.util.*;
public class ARMSTRONG
{
public static void main(String[] args)
{
int a,r,b=0;
System.out.println("Enter the number: ");
Scanner s= new Scanner(System.in);
a = s.nextInt();
int n=a;
while(a>0)
{
r=a%10;
a=a/10;
b= b + (r*r*r);
}
if(n==b)
{
System.out.println("Number is Armstrong");
}
Else
{
System.out.println("Not Armstrong");
}
}
}
OUTPUT
Enter the number:
153
Number is Armstrong
Enter the number:
234
Not Armstrong
Program:-3
WAP to implement the concept of function overloading.
package funcover;
import java.util.*;
class Main
{
void Area()
{
int r=5;
System.out.println("Area of circle with no argument: "
+(3.14*r*r));
}
void Area(int r)
{
System.out.println("Area of circle: " +(3.14*r*r));
}
void Area(int a,int b)
{
System.out.println("Area of rectangle: " +(a*b));
}
void Area(float a,float b)
{
System.out.println("Area of rectangle: " +(a*b));
}
}
public class func_overloading
{
public static void main(String[] args)
{
Main m=new Main();
Int a,b;
System.out.println("Enter the two numbers: ");
Scanner s= new Scanner(System.in);
a = s.nextInt();
b = s.nextInt();
m.Area();
m.Area(a);
m.Area(a,b);
m.Area(5.5f,6.5f);
}
}
OUTPUT
Enter the two numbers: 7 6
Area of circle with no argument: 78.5
Area of circle: 153.86
Area of rectangle: 42
Area of rectangle: 35.75
Program:-4
WAP to implement function overriding.
package funcoverr;
class A
{
void show()
{
System.out.println("welcome to class A");
}
}
class B extends A
{
void show()
{
super.show();
System.out.println("welcome to class B");
super.show();
}
}
public class func_overriding
{
public static void main(String[] args)
{
B b=new B();
b.show();
}
}
OUTPUT
welcome to class A
welcome to class B
welcome to class A
Program:-5
WAP to implement the Exceptional Handling.
package excptnhng;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
try
{ int c[]={1,2};
c[3]=10;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array out of bound");
}
try
{
int a=3;
int b;
System.out.println(“Enter the no.”);
Scanner s=new Scanner(System.in);
b=s.nextInt();
int d=a/b;
}
catch(ArithmeticException e)
{ System.out.println("divide by zero");
}
Finally
{ System.out.println("welcome");
}}}
OUTPUT
Array out of bound
Enter the no. 3
Welcome
Program:-6
WAP of an applet that receives two numerical values as the input from user and displays the sum of these two numbers.
package javaapplication6;
import java.applet.Applet;
import java.awt.*;
public class NewApplet extends Applet
{
TextField t1,t2;
public void init()
{
t1=new TextField(8);
t2=new TextField(8);
add(t1);
add(t2);
t1.setText("0");
t2.setText("0");
}
public void paint (Graphics g)
{ int a,b;
a=Integer.parseInt(t1.getText());
b=Integer.parseInt(t2.getText());
int c=a+b;
String s=Integer.toString(c);
g.drawString("the sum is",20,30);
g.drawString(s,50,50);
}}
OUTPUT
Program:-7
WAP for displaying product list along with their prices and then allow user to buy
any1 item from them with required quantity.
package product_list;
import java.util.*;
public class Main
{
int price[] = new int[5];
String name[] = new String[5];
String g="";
int d;
int f;
int h;
public void items()
{
for(int j=0;j<5;j++)
{
System.out.println("Entr the"+j+"th products name: ");
Scanner s= new Scanner(System.in);
name[j] = s.next();
}
}
public void price()
{
for(int i=0;i<5;i++)
{
Scanner s= new Scanner(System.in);
System.out.println("Entr the "+i+"products price: ");
price[i] = s.nextInt();
}
}
public void display()
{
for(int j=0;j<5;j++)
{
System.out.println(“Product_Name”+” “ + “Product_Price”);
System.out.println(name[j]+” “+price[j]);
}
}
public void choice()
{
Scanner s= new Scanner(System.in);
System.out.println("Enter product name which you want to buy");
g=s.next();
for(int i=0;i<5;i++)
{
if(g.equals(name[i]))
{
System.out.println("your product is:"+name[i]);
d=price[i];
break;
}
}
}
void total_price()
{
System.out.println("Enter quantity");
Scanner s= new Scanner(System.in);
f=s.nextInt();
h=f*d;
System.out.println("your total price is"+h);
}
public static void main(String[] args)
{
Main obj=new Main();
obj.items();
obj.price();
obj.display();
obj.choice();
obj.total_price();
}
}
OUTPUT
Entr the 0th products name:
Bread
Entr the 1th products name:
Jam
Entr the 2th products name:
Milk
Entr the3th products name:
Butter
Entr the4th products name:
Curd
Entr the 0products price:
20
Entr the 1products price:
100
Entr the 2products price:
30
Entr the 3products price:
50
Entr the 4products price:
25
Product-Name Produc_Price
Bread 20
Jam 100
Milk 30
Butter 50
Curd 25
Enter product name which you want to buy
Milkyour product is: milk
Enter quantity 4
your total price is 120
Program:-8
WAP to implement multithreading(three threads using single run method).
package thread;
class NewThread extends Thread
{
NewThread()
{
super("Demo Thread");
System.out.println("Child thread: " + this);
start();
}
public void run()
{
try
{
for(int i = 5; i > 0; i--)
{
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
}
catch (InterruptedException e)
{
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
public class Main
{
public static void main(String[] args)
{
new NewThread(); // create a new thread
try
{
for(int i = 5; i > 0; i--)
{
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e)
{System.out.println("Main thread interrupted.");}
System.out.println("Main thread exiting.");
}
}
OUTPUT
New thread: One
New thread: Two
One: 5
New thread: Three
Two: 5
Three: 5
One: 4
Two: 4
Three: 3
One: 2
Two: 2
Three: 2
One: 1
Three: 1
Two: 1
One exiting.
Three exiting.
Two exiting.
Main thread exiting.
Program:-9
WAP to make a calculator.
package calculator;
public class form1 extends javax.swing.JFrame {
int a;
int count=0;
public form1()
{
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
jTextField1.setText(jTextField1.getText()+"1");
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent
evt)
{
jTextField1.setText(jTextField1.getText()+"2");
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent
evt)
{
jTextField1.setText(jTextField1.getText()+"3");
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt)
{
jTextField1.setText(jTextField1.getText()+"4");
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent
evt)
{
jTextField1.setText(jTextField1.getText()+"5");
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent
evt)
{
jTextField1.setText(jTextField1.getText()+"6");
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt)
{
jTextField1.setText(jTextField1.getText()+"7");
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt)
{
jTextField1.setText(jTextField1.getText()+"8");
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt)
{
jTextField1.setText(jTextField1.getText()+"9");
}
private void jButton18ActionPerformed(java.awt.event.ActionEvent evt)
{
jTextField1.setText(jTextField1.getText()+"0");
}
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt)
{
a=Integer.parseInt(jTextField1.getText());
jTextField1.setText("");
count=1;
}
private void jButton11ActionPerformed(java.awt.event.ActionEvent evt)
{
a=Integer.parseInt(jTextField1.getText());
jTextField1.setText("");
count=2;
}
private void jButton12ActionPerformed(java.awt.event.ActionEvent evt)
{
a=Integer.parseInt(jTextField1.getText());
jTextField1.setText("");
count=3;
}
private void jButton13ActionPerformed(java.awt.event.ActionEvent evt)
{
a=Integer.parseInt(jTextField1.getText());
jTextField1.setText("");
count=4; /
}
private void jButton14ActionPerformed(java.awt.event.ActionEvent evt)
{
a=Integer.parseInt(jTextField1.getText());
jTextField1.setText("");
count=5;
}
private void jButton15ActionPerformed(java.awt.event.ActionEvent evt)
{
a=Integer.parseInt(jTextField1.getText());
jTextField1.setText("");
count=6;
}
private void jButton17ActionPerformed(java.awt.event.ActionEvent evt)
{
int b=Integer.parseInt(jTextField1.getText());
int c=0;
if(count==1)
{
c=a+b;
}
else if(count==2)
{
c=a-b;
}
else if(count==3)
{
c=a*b;
}
else if(count==4)
{
c=a/b;
}
else if(count==5)
{
c=a%b;
}
else if(count==6)
{
c=a*b;
}
jTextField1.setText(Integer.toString(c));
}
private void jButton16ActionPerformed(java.awt.event.ActionEvent evt)
{
jTextField1.setText("");
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new form1().setVisible(true);
}
});
} }
OUTPUT
Program:-10
WAP Program to implement the URL.
package url;
import java.net.*;
class url
{
public static void main(String[] args)
{
try
{
String st = "http://www.google.com/main?string#inbox";
URL ur=new URL(st);
System.out.println("Protocol= "+ ur.getProtocol());
System.out.println("Host= "+ ur.getHost());
System.out.println("defaultport= "+ur.getDefaultPort());
System.out.println("path= "+ur.getPath());
System.out.println("file= "+ur.getFile());
System.out.println("reference= "+ur.getRef());
}
catch(MalformedURLException e)
{
System.out.println(e);
}
}}
OUTPUT
run:
Protocol= http
Host= www.google.com
defaultport= 80
path= /main
file= /main?string
reference= inbox
Program:-11
WAP to implement InetAddress.
package clientservercomm;
import java.net.*;
class inet
{
public static void main(String[] args)
{
try
{
InetAddress add = InetAddress.getLocalHost();
String name= add.getHostName();
System.out.println("name of local host"+ name);
String add1= add.getHostAddress();
System.out.println("IP address of local host"+ add1);
InetAddress
address=InetAddress.getByName("www.google.com");System.out.println("
address : "+ address);
InetAddress
add2[]=InetAddress.getAllByName("www.sun.com");
for(int i=0;i<add2.length;i++)
{
System.out.println("All IP Address: "+add2[i]);
}
}
catch (Exception e){
System.out.println(e);
} }}
OUTPUT
run:
name of local hostLab1-pc29
IP address of local host172.16.26.158
address : www.google.com/209.85.231.104
All IP Addrewww.sun.com/137.254.16.57
Program:-12
WAP for Sending Email in java.
import javax.mail.*;
import java.net.*;
import javax.mail.internet.*;
public class Main
{
public static void main(String[] args)
{
String host = "smtp.gmail.com";
String user = "[email protected]";
String pass = "derjfjrf";
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
String from1 = "[email protected]";
String to1 = "[email protected]";
String subject1 = "java";
String messageText = "Hello hw r u?";
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol.", "smtp");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
Session mailSession = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(mailSession);
try
{
InternetAddress fr = new InternetAddress(from1);
msg.setFrom(fr);
InternetAddress[] address = {new InternetAddress(to1)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject1);
msg.setContent(messageText, "text/html");
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, pass);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
System.out.println("Done Mail");
}
catch (Exception err)
{
System.out.println("not done mail");
System.out.println(err);
}
}}
OUTPUT
Done Mail
PROGRAM -13
WAP to implement Client-Server Program
package JavaApplication2;
import java.net.*;
import java.io.*;
public class SimpleClient {
public static void main(String args[]) throws IOException {
// Open your connection to a server, at port 1254
Socket s1 = new Socket("localhost",1254);
// Get an input file handle from the socket and read the input
InputStream s1In = s1.getInputStream();
DataInputStream dis = new DataInputStream(s1In);
String st = new String (dis.readUTF());
System.out.println(st);
// When done, just close the connection and exit
dis.close();
s1In.close();
s1.close();
}
}
package javaapplication2;
import java.net.*;
import java.io.*;
public class SimpleServer1 {
public static void main(String args[]) throws IOException {
// Register service on port 1254
ServerSocket s = new ServerSocket(1256);
//ServerSocket s2=new ServerSocket(1254);
Socket s1=s.accept();
Socket s2=s.accept();
// Wait and accept a connection
// Get a communication stream associated with the socket
OutputStream s1out = s1.getOutputStream();
OutputStream s2out = s2.getOutputStream();
DataOutputStream dos = new DataOutputStream (s1out);
DataOutputStream dos2 = new DataOutputStream (s2out);
// Send a string!
dos.writeUTF("Hi there");
dos2.writeUTF("welcome");
// Close the connection, but not the server socket
dos.close();
dos2.close();
s1out.close();
s2out.close();
s1.close();
s2.close();
}
}
PROGRAM-14
WAP to implement the SQL –login ID commands using JDBC
package javaapplication5;
import java.sql.*;
import java.awt.*;
import javax.swing.*;
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
}
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Login");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
// Connection conn;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "Jdbc:Odbc:g2";
Connection conn = DriverManager.getConnection(url);
ResultSet rs;
Statement stmt=conn.createStatement();
rs=stmt.executeQuery("select * from mytab");
while(rs.next())
{
if(((jTextField1.getText()).equals(rs.getString(1)))&& ((jTextField2.getText()).equals(rs.getString(2)))){
JOptionPane.showMessageDialog(this, "login successfull");
System.exit(0);
}}
JOptionPane.showMessageDialog(this, "login unsuccessfull");
System.exit(0);
conn.close();
}
catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
OUTPUT:
PROGRAM-15
WAP to implement the SQL commands using JDBC.
package opertiondemo;
import java.sql.*;
import javax.swing.*;
public class operationdemo1 extends javax.swing.JFrame {
ResultSet rs1;
/** Creates new form operationdemo1 */
public operationdemo1() {
initComponents();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "Jdbc:Odbc:g4";
Connection conn = DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs1=stmt.executeQuery("select * from student");
}
catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try
{System.out.println("Outside rs1");
if(rs1.next()){
System.out.println("Inside rs1");
String r1=rs1.getString(1);
String n1=rs1.getString(2);
String a1=rs1.getString(3);
jTextField1.setText(r1);
jTextField2.setText(n1);
jTextField3.setText(a1);
}
else
{
JOptionPane.showMessageDialog(this, "eND OF THE RECORD");
}
}catch(Exception e)
{}
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "Jdbc:Odbc:g4";
Connection conn = DriverManager.getConnection(url);
ResultSet rs;
Statement stmt=conn.createStatement();
String qry=("Insert into student values('"+jTextField1.getText()+"','"+jTextField2.getText()+"','"+jTextField3.getText()+"')");
stmt.executeUpdate(qry);
JOptionPane.showMessageDialog(this, "Record inserted");
conn.close();
}
catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
try
{System.out.println("Outside rs1");
if(rs1.previous()){
System.out.println("Inside rs1");
String r1=rs1.getString(1);
String n1=rs1.getString(2);
String a1=rs1.getString(3);
jTextField1.setText(r1);
jTextField2.setText(n1);
jTextField3.setText(a1);
}
}catch(Exception e)
{}
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try
{
// Connection conn;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "Jdbc:Odbc:g4";
Connection conn = DriverManager.getConnection(url);
ResultSet rs;
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery("select * from student");
rs.first();
String r=rs.getString(1);
String n=rs.getString(2);
String a=rs.getString(3);
jTextField1.setText(r);
jTextField2.setText(n);
jTextField3.setText(a);
conn.close();
}
catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try
{
// Connection conn;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "Jdbc:Odbc:g4";
Connection conn = DriverManager.getConnection(url);
ResultSet rs;
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery("select * from student");
rs.last();
String r=rs.getString(1);
String n=rs.getString(2);
String a=rs.getString(3);
jTextField1.setText(r);
jTextField2.setText(n);
jTextField3.setText(a);
conn.close();
}
catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try
{
// Connection conn;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "Jdbc:Odbc:g4";
Connection conn = DriverManager.getConnection(url);
ResultSet rs;
String qry="update student set rollnum="+jTextField1.getText()+",age="+jTextField3.getText()+" where name='"+jTextField2.getText()+"'";
Statement stmt=conn.createStatement();
stmt.executeUpdate(qry);
JOptionPane.showMessageDialog(this, "Record Updated");
conn.close();
}
catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try
{
// Connection conn;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "Jdbc:Odbc:g4";
Connection conn = DriverManager.getConnection(url);
Statement stmt=conn.createStatement();
stmt.executeUpdate("delete * from student where rollnum="+jTextField1.getText()+"");
JOptionPane.showMessageDialog(this, "Record deleted");
jTextField1.setText(" ");
jTextField2.setText(" ");
jTextField3.setText(" ");
conn.close();
}
catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
// TODO add your handling code here:
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(" ");
jTextField2.setText(" ");
jTextField3.setText(" ");
// TODO add your handling code here:
}
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try
{
// Connection conn;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "Jdbc:Odbc:g4";
Connection conn = DriverManager.getConnection(url);
ResultSet rs;
Statement stmt=conn.createStatement();
rs=stmt.executeQuery("Select * from student");
while(rs.next()){
String r=rs.getString(1);
String n=rs.getString(2);
String a=rs.getString(3);
jTextField1.setText(r);
jTextField2.setText(n);
jTextField3.setText(a);
}
conn.close();
}
catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}}}
OUTPUT:
PROGRAM-16
WAP to implement the List.
package list;
import java.sql.*;
import javax.swing.*;
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
DefaultListModel model=new DefaultListModel();
String username=" ";
String password=" ";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:student";
Connection con=DriverManager.getConnection(url,username,password);
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from Table1");
while(rs.next())
{
model.addElement(rs.getString(1)+ " "+rs.getString(2)+" "+rs.getString(3));
// TODO add your handling code here:
}//GEN-LAST:event_jButton1ActionPerformed
jList1.setModel(model);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT:
PROGRAM-17
WAP to implement JTable
package jtreess;
import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
public class JTableComponent{
public static void main(String[] args)
{
new JTableComponent();
}
public JTableComponent(){
JFrame frame = new JFrame("Creating JTable Component Example!");
JPanel panel = new JPanel();
String data[][] = {{"vinod","BCA","A"},{"Raju","MCA","b"},
{"Ranjan","MBA","c"},{"Rinku","BCA","d"}};
String col[] = {"Name","Course","Grade"};
JTable table = new JTable(data,col);
panel.add(table,BorderLayout.CENTER);
frame.add(panel);
frame.setSize(300,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
OUTPUT:
PROGRAM-18
WAP to create a Table using JDBC
package jtreess;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
public class table extends javax.swing.JFrame {
public table() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
DefaultTableModel model= (DefaultTableModel)jTable1.getModel();
int colno=3;
String username=" ";
String password=" ";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:g4";
Connection con=DriverManager.getConnection(url,username,password);
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from student");
while(rs.next())
{
Object[] cell= new Object[colno];
for(int i=0;i<colno;i++)
{
cell[i]=rs.getObject(i+1);// TODO add your handling code here:
}
model.addRow(cell);
}
jTable1.setModel(model);
}
catch(Exception e)
{
System.out.println(e);
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new table().setVisible(true);
}
});
}
}
Output:
PROGRAM-19
WAP to implement Tree
package jtreess;
import javax.swing.*;
import javax.swing.tree.*;
public class TreeComponent{
public static void main(String[] args) {
JFrame frame = new JFrame("Creating a JTree Component!");
DefaultMutableTreeNode parent = new DefaultMutableTreeNode("Color", true);
DefaultMutableTreeNode black = new DefaultMutableTreeNode("Black");
DefaultMutableTreeNode blue = new DefaultMutableTreeNode("Blue");
DefaultMutableTreeNode nBlue = new DefaultMutableTreeNode("Navy Blue");
DefaultMutableTreeNode dBlue = new DefaultMutableTreeNode("Dark Blue");
DefaultMutableTreeNode green = new DefaultMutableTreeNode("Green");
DefaultMutableTreeNode white = new DefaultMutableTreeNode("White");
parent.add(black);
parent.add(blue);
blue.add(nBlue);
blue.add(dBlue);
parent.add(green );
parent.add(white);
JTree tree = new JTree(parent);
frame.add(tree);
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setUndecorated(true);
//frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setSize(200,200);
frame.setVisible(true);
}}
OUTPUT:
Program:-20
WAP to implement the Remote Method Invocation.
INTERFACE:
import java.rmi.*;
interface Bank extends Remote
{
double getAmount(double p,double t) throws RemoteException;
}
BANK SEVER:
import java.rmi.*;
import java.rmi.server;
public class BankImpl extends UnicastRemoteObject implements Bank
{
public BankImpl throws RemoteException
{
}
double getAmount(double p,double t) throws RemoteException
{
return p*Math.pow(1.41,t);
}
}
RMI REGISTRY:
import java.rmi.*;
import javax.naming.*;
public class BankServer
{
public static void main(String args[])
{
BankImpl centralbank=new BankImpl();
Naming.rebind("uti",centralbank);
}
}
BANKCLIENT:
import java.rmi.*;
import javax.naming.*;
public class Bankclient
{
public static void main(String args[]) throws RemoteException
{
String url="rmi://localHost//uti";
Bank b=(Bank) Naming.lookup(url);
System.out.println(b.getAmount(4000,3));
}
}