JDBC
JDBC
OUTPUT
Length of strob1: 12
Char at index 3 in strob1: s
strob1 != strob2
strob1 = = strob3
Program: 14(b)
OUTPUT
Excersise
for
good
health
is
Program: 14(C)
OUTPUT
Now is the time for all the good mento come to the aid of their country.
indexof(t) = 7
lastIndexof(t) = 68
indexof(the) = 7
lastIndexof(the) = 58
indexof(t,10) = 11
lastIndexof(t,60) = 58
indexof(the,10) = 24
lastIndexof(the,60) = 58
Program: 15(A)
OUTPUT
OUTPUT
Prime
Program :15(c)
OUTPUT
i is zero
i is one
i is two
i is three
i is greater than 3
i is greater than 3
Program :15(d)
OUTPUT
hi 12
hi 11
hi 10
hi 9
hi 8
hi 7
hi 6
hi 5
hi 4
hi 3
hi 2
hi 1
Program: 15(e)
OUTPUT
Help on:
1: if:
2: switch:
3: while:
4: do-while:
5: for:
Choose one:
2
The Switch:
switch(expression) {
Case contant:
Statement sequence
break;
//.....
}
Program: 16(a)
OUTPUT
OUTPUT
01234
56789
10 11 12 13 14
15 16 17 18 19
Program : 17(a)
Integer Arithmetic
a=2
b = 12
c=1
d = -3
e=3
Floating point arithmetic
da = 2.0
db = 6.0
dc = 1.5
dd = -0.5
de = 0.5
Program : 17(b)
OUTPUT
a= 0011
b= 0110
c= 0111
d= 0010
e= 0101
f= 0101
g= 1100
Program : 17(c)
OUTPUT
Absolute value of
10 is 10
Absolute value of
-10 is 10
Program : 18
class A1
{
int i,j;
A1(int a,int b)
{
i=a;
j=b;
}
void show()
{
System.out.println("i and j: "+i+" "+j);
}
}
class B1 extends A1
{
int k;
B1(int a,int b,int c)
{
super(a,b);
k=c;
}
void show()
{
System.out.println("k: "+k);
}
}
public class Override
{
public static void main(String[] args)
{
B1 ob=new B1(1,2,3);
ob.show();
}
OUTPUT
k: 3
Program : 19
class C1
{
void callme()
{
System.out.println("Inside A's callme method");
}
}
class D extends C1
{
void callme()
{
System.out.println("Inside B's callme method");
}
}
class E extends C1
{
void callme()
{
System.out.println("Inside C's callme method");
}
}
public class Dispatch
{
public static void main(String[] args)
{
C1 c=new C1();
D d=new D();
E e=new E();
C1 f;
f=c;
f.callme();
f=d;
f.callme();
f=e;
f.callme();
}
}
OUTPUT
class Box2
{
double width,height,depth;
Box2(double w,double h,double d)
{
width=w;
height=h;
depth=d;
}
double volume()
{
return width*height*depth;
}
}
public class BoxDemo7
{
public static void main(String[] args)
{
Box2 a=new Box2(10,20,30);
Box2 b=new Box2(2,4,6);
double vol;
vol=a.volume( );
System.out.println("Volume is "+vol);
vol=b.volume( );
System.out.println("Volume is "+vol);
}
}
OUTPUT
Volume is 6000.0
Volume is 48.0
Program : 21
class OverloadDemo
{
void test()
{
System.out.println("No Parameters");
}
void test(int a)
{
System.out.println("a : "+a);
}
void test(int a,int b)
{
System.out.println("a and b : "+a+" "+b);
}
double test(double a)
{
System.out.println("a : "+a);
return a*a;
}
}
public class Overload
{
public static void main(String[] args)
{
OverloadDemo ob=new OverloadDemo();
double result;
ob.test();
ob.test(20);
ob.test(20,30);
result=ob.test(225.26);
System.out.println("Result of ob.test(225.26) : "+result);
}
}
OUTPUT
No Parameters
a : 20
a and b : 20 30
a : 225.26
Result of ob.test(225.26) : 50742.0676
Program : 22
import java.io.*;
public class TinyEdit
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String str[]=new String[100];
System.out.println("Enter lines of text");
System.out.println("Enter stop to quit");
for(int i=0;i<100;i++)
{
str[i]=br.readLine();
if(str[i].equals("stop"))
break;
}
System.out.println("\n Here is your file");
for(int i=0;i<100;i++)
{
if(str[i].equals("stop"))
break;
System.out.println(str[i]);
}
}
}
OUTPUT
package p1;
OUTPUT
J.J.Jaspers: $99.88
Program : 23(b)
OUTPUT
Stack in mystack1
4
3
2
1
0
Stack in mystack2
7
6
5
4
3
2
1
0
Program : 24
myInter.java
import java.rmi.*;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.*;
import java.rmi.server.*;
C:>cd beans
C:/beans>cd beanbox
C:/beans/beanbox>run.bat
C:\beans\beanbox>if "Windows_NT" == "Windows_NT" setlocal
C:\beans\beanbox>set
CLASSPATH=classes;..\lib\methodtracer.jar;..\infobus.jar
C:\beans\beanbox>java sun.beanbox.BeanBoxFrame
Program : 26
import javax.swing.*;
public class Logo1 extends JPanel
{
private String sname=" MY LOGO";
JLabel lname;
JTextField tname;
public Logo1()
{
lname=new JLabel(sname);
tname=new JTextField(10);
add(lname);
add(tname);
}
public void setSname(String str)
{
sname=str;
lname.setText(sname);
}
public String getSname()
{
return sname;
}
}
Now write the following code in the separate notepad and save it with .mft extension
Name: Logo1.class
Java-Bean: true
C:>cd beans
C:/beans>cd beanbox
C:/beans/beanbox>run.bat
C:\beans\beanbox>if "Windows_NT" == "Windows_NT" setlocal
C:\beans\beanbox>set
CLASSPATH=classes;..\lib\methodtracer.jar;..\infobus.jar
C:\beans\beanbox>java sun.beanbox.BeanBoxFrame
After execute run file we have following output
<html>
<head>
<title>
JSP Example
</title>
</head>
<body>
<h1> WELCOME<h1>
<% out.println(“hello”); %>
</body>
<html>
OUTPUT
WELCOME
Hello
Program : 28
Write a program to generate current date and time with the use of JSP pages
<html>
<h1>Welcome to JSP</h1>
<hr>server Time :
<% java.util.Date dt=new java.util.Date(); %>
<%=dt.getHours() %> : <%=dt.getMinutes() %> : <%=dt.getSeconds()%>
</html>
OUTPUT
class Info
{
int pid;
char branch;
char year;
void display()
{
System.out.println("\nPID\t: "+pid);
System.out.print("Branch\t: ");
if(branch == 'i')
System.out.println("Information Technology");
if(branch =='e')
System.out.println("Electronics and Telecommunication");
if(branch =='c')
System.out.println("Computer Science");
System.out.print("Year\t: ");
if(year == 'f')
System.out.println("FE");
if(year == 's')
System.out.println("SE");
if(year == 't')
System.out.println("TE");
}
}
void fdisplay()
{
display();
System.out.println("Performance:");
System.out.println("\tC\t"+c);
System.out.println("\tC++\t"+cpp);
}
}
void sdisplay()
{
display();
System.out.println("Performance:");
System.out.println("\tVB\t"+vb);
System.out.println("\tHTML\t"+html);
}
}
class Language
{
public static void main(String args[])
{
Fe F = new Fe(1074,'i','f',9,8);
Se S = new Se(1064,'e','s',6,8);
Te T = new Te(1054,'c','t',9,9);
F.fdisplay();
S.sdisplay();
T.tdisplay();
}
}
OUTPUT
PID : 1074
Branch : Information Technology
Year : FE
Performance:
C 9
C++ 8
PID : 1064
Branch : Electronics and Telecommunication
Year : SE
Performance:
VB 6
HTML 8
PID : 1054
Branch : Computer Science
Year : TE
Performance:
MATLAB 9
SJava 9
OUTPUT
Output
Output
No. of Columns 4
Column name ID
column name employee name
1
2
3
4
Program:11
import java.sql.*;
class DataRead
{
public static void main(String []args)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:hce");
Statement stat=con.createStatement();
ResultSet rs=stat.executeQuery("select price from geetu");
ResultSetMetaData rm=rs.getMetaData();
System.out.println("No. of Columns "+rm.getColumnCount());
System.out.println("Column name "+rm.getColumnName(1));
//System.out.println("column name "+rm.getColumnLabel(3));
while(rs.next())
{
System.out.println(rs.getString(1));
}
}
catch(Exception e){
System.out.println("Exception "+e);
}
}
}
Program:12
Write a program to extract the data from database using jdbc connectivity.
import java.sql. *;
class DataRead1
{
public static void main(String [] args)
{
try{
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =DriverManager.getConnection("jdbc:odbc:hce");
Statement stat = con.createStatement();
ResultSet rs =stat.executeQuery("select * from geetu");
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
}
}
catch(Exception e){
System.out.println("Exception"+e);
}
}
}
Program:7
Write a program to handle all type of exception using try catch block.
import java.io.*;
class MultiCatch{
public static void main(String args[]){
try{
int a = args.length;
System.out.println("a="+a);
int b =42/a;
int c[]={1};
c[42]=99;
}catch(ArithmeticException e){
System.out.println("divide by 0:"+e)
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("array index oob:"+e);
}finally
{
System.out.println("other exception");
}System.out.println("after try/catch blocks.");
}}
Program: 12
Write a program to insert data into a table using prepared statements through GUI.
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
//<applet code=DataInApp.class width="500" height="200"></applet>
public class DataInApp extends JApplet
{
JPanel p;
JLabel leid,lename,lesalary;
JTextField teid,tename,tesalary;
JButton b;
public void init()
{
p=new JPanel();
leid=new JLabel("EMPLOYEE ID");
lename=new JLabel("EMPLOYEE NAME");
lesalary=new JLabel("EMPLOYEE SALARY");
teid=new JTextField(10);
tename=new JTextField(10);
tesalary=new JTextField(10);
b=new JButton("Insert");
getContentPane().add(p);
p.add(leid);
p.add(teid);
p.add(lename);
p.add(tename);
p.add(lesalary);
p.add(tesalary);
p.add(b);
b.addActionListener(eobj);
}
class EventHandle implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:hce");
PreparedStatement s=con.prepareStatement("insert into
geetu values(?,?,?)");
s.setString(1,teid.getText());
s.setString(2,tename.getText());
s.setString(3,tesalary.getText());
s.executeUpdate();
getAppletContext().showStatus("done");
}
catch(Exception e)
{
getAppletContext().showStatus("Exception "+ e);
}
}
}
}
Program: 10
Write a program to create client server communication in each you have to show how to
connect to server & how to implement server using programming.
CLIENT
import java.net.*;
import java.io.*;
public class Client {
public static void main(String[] args) {
if(args.length != 2)
System.out.println("Usage : not done");
else {
String inp;
try
{
Socket sock = new
Socket(args[0],Integer.valueOf(args[1]).intValue());
DataInputStream is =new
DataInputStream(sock.getInputStream());
System.out.println("address :"+sock.getInetAddress());
System.out.println("port :"+sock.getPort());
System.out.println("Local
address :"+sock.getLocalAddress());
System.out.println("Local port :"+sock.getLocalPort());
while((inp =is.readLine()) != null )
{
System.out.println(inp);
}
}catch (UnknownHostException e)
{
System.out.println("Known
Host:"+e.getMessage());
}catch (IOException e)
{
System.out.println("error IO:"+e.getMessage());
}
finally
{
System.out.println("end of program");
}
}
}
}
SERVER
import java.net.*;
import java.io.*;
import java.util.*;
public class server {
public static void main(String[] args) {
try {
ServerSocket sock = new ServerSocket(1111);
Socket sock1 = sock.accept();
System.out.println(sock1.toString());
System.out.println("address :"+sock1.getInetAddress());
System.out.println("ports :"+sock1.getPort());
DataOutputStream out = new
DataOutputStream(sock1.getOutputStream());
out.writeBytes("Welcome"+sock1.getInetAddress().getHostName()+"We
are"+new Date());
sock1.close();
sock.close();
}catch(IOException err) {
System.out.println(err.getMessage());
}
finally {
System.out.println("end of program");
}
}
Program: 9
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code ="MouseEvents"width=300 height=100>
</applet>
*/
class ThreadDemo{
public static void main(String args[]){
new NewThread();
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");
}}
Program: 8
import java.awt.*;
import java.awt.event.*;
import.java.applet.*;