Java Lab Programs - 3
Java Lab Programs - 3
Java Lab Programs - 3
import java.awt.*;
import java.applet.*;
/*<applet code=helloparam.class height=300 width=250>
<param name="string" value="Applet!!!!!">
</applet>*/
public class helloparam extends Applet
{
String str;
public void init()
{
str=getParameter("string");
if(str==null)
str="5BCA";
str="hello"+" "+str;
}
public void paint(Graphics g)
{
g.drawString(str,10,100);
}
}
To run the applet application:
C:\java program>javac helloparam.java
C:\java program>appletviewer helloparam.java
y=x%2;
if(y==0)
s=x+" is Even number";
else
s=x+" is Odd number";
g.drawString(s,10,75);
}
public Boolean abc(Event event,Object object)
{
repaint();
return true;
}
}
Output: To run the program:
F:\java pro\java\applet>javac displayeven.java
F:\java pro\java\applet>appletviewer displayeven.java
Q21. Create a Java applet application to demonstrate arithmetic operation using
Event Handling.
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
/*<applet code="readapplet.class" width=300 height=200></applet>*/
public class readapplet extends Applet implements ActionListener
{ Label l1, l2, l3;
intx,y,z;
TextField t1, t2,t3;
Button b1;
public void init()
{
setLayout(new GridLayout(4, 2));
t1 =new TextField(8);
t2 = new TextField(8);
t3 = new TextField(8);
b1=new Button("Calculate");
l1 = new Label("Number 1");
l2 = new Label("Number 2");
l3 = new Label("Result");
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b1);
t1.setText("0");
t2.setText("0");
t3.setText("0");
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b1)
{
x = Integer.parseInt(t1.getText());
y = Integer.parseInt(t2.getText());
z = x + y;
t3.setText(String.valueOf(z));
}
}
}
To run the program:
F:\java pro\java\applet\New Folder>javac readapplet.java
F:\java pro\java\applet\New Folder>appletviewer readapplet.java
Q22. Create a Java applet application to demonstrate free hand drawing on applet.
import java.applet.*;
import java.awt.*;
public class freehand extends Applet
{
private intlast_x=0;
private intlast_y=0;
public booleanmouseDown(Event e, int x, int y)
{
last_x=x;
last_y=y;
return true;
}
public booleanmouseDrag(Event e, int x, int y)
{
Graphics g=getGraphics();
g.drawLine(last_x, last_y, x, y);
last_x=x;
last_y=y;
return true;
}
}
/* <applet code=freehand width=300 height=300></applet>*/
To run the program:
F:\java pro\java\applet>javac freehand.java
Note: freehand.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Step 1: Create the MS-Access Database with table name as: “jstudent”
Column_Name Datatype
rollno Number
s_name Text
jdsnstud
In this window , enter the Data Source Name( in this case it is “jdsnstud”) .
To select the database, Click on Select button that opens the following window and there you have
to select the database. And Click Ok button.
import java.sql.*;
ps.executeUpdate();
con.close();
System.out.println("Data Inserted Successfully");
}
catch(Exception e)
{
System.out.println("Some problem..........");
}
}
}
/*
C:\java\jdbc>javac accjava.java
C:\java\jdbc>java accjava 1 Riya Mysore
Data Inserted Successfully */
Q24. Create a Java application program to display all the rows in the database.
import java.sql.*;
public class jdatasel
{
public static void main(String args[])
{
String str, str1,str2;
PreparedStatement ps;
ResultSet rs;
Connection con=null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection = DriverManager.getConnection("jdbc:odbc:jdsnstud");
ps = connection.prepareStatement("select
nection.prepareStatement("select * from jstu
jstudent");
rs=ps.executeQuery();
while (rs.next())
{
str = rs.getString(1);
System.out.println(str);
str1 = rs.getString(2);
System.out.println(str1);
}
connection.close();
}
catch(Exception e)
{
System.out.println("Some problem......");
}
}
}