Experiment:-1: Aim: - Code

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

Experiment:- 1

Aim:- Draw ten red circles in a vertical column in the center of the applet.
Code:
import java.applet.*;
import java.awt.*;
public class p1b extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
for(int i=0;i<10;i++)
{
g.fillOval(135,i*30,30,30);
}

}
}
/* <applet code="p1b.class" width="300" height="300"></applet>*/

Output:

1|Page
Experiment:- 2
Aim:- Create an application that displays a frame with a menu bar. When a user selects any menu or
menu item, display that selection on a text area in the center of the frame
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class p5b extends Applet implements ActionListener
{
static JMenuBar mb;
static JMenu menu;
static JMenuItem m1,m2,m3;
static JTextArea txt;
public void init()
{
mb=new JMenuBar();
add(mb);
menu=new JMenu("File");
mb.add(menu);
txt=new JTextArea(null,1,5);
add(txt);

m1 = new JMenuItem("Save");
m2 = new JMenuItem("Open");
m3 = new JMenuItem("Save AS");

menu.add(m1);
menu.add(m2);
menu.add(m3);

m1.addActionListener(this);
m2.addActionListener(this);
m3.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== m1)
{
txt.setText("Save");
}
else if(e.getSource()== m2)
{
txt.setText("Open");
}
else if(e.getSource()== m3)
{
txt.setText("saveAS");
}
}
}
/* <applet code="p5b.class" width="200" height="200"></applet>*/

2|Page
Output:

3|Page
Experiment:- 3
Aim:-
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
public class example extends Applet implements ActionListener {
JTextField txt;
JButton btn;
JLabel disp;
public void init()
{
txt = new JTextField(10);
add(txt);
btn = new JButton("Submit");
add(btn);
disp = new JLabel();
add(disp);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String str = txt.getText();
disp.setText("Your Name is: "+str);
}
}
/* <applet code="example.class" width="200" height="200"></applet>*/

Output:

4|Page
Experiment:- 4
Aim:- Develop JDBC application that uses any JDBC drivers to insert a record. OR Create User
interface that accept user information name, age and gender from user and insert it into MySQL
database.
Code:
import java.sql.*;
public class Main
{
public static void main(String[] args) throws ClassNotFoundException
{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection("jdbc:mysql://localhost/sv", "root", "");
Statement s = c.createStatement();
s.executeUpdate("Insert into stud VALUES ('2','Hello')");
System.out.println("Inserted");
c.close();
}
catch (SQLException e){
e.printStackTrace();
}
}
}
Output:

5|Page
Experiment:- 5
Aim:- Develop JDBC application that uses any JDBC drivers to update a record.
Code:
import java.sql.*;
public class Main
{
public static void main(String[] args) throws ClassNotFoundException
{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection("jdbc:mysql://localhost/sv", "root", "");
Statement s = c.createStatement();
s.executeUpdate("update stud set name='XYZ' where id=1");
System.out.println("Updated");
c.close();
}
catch (SQLException e){
e.printStackTrace();
}
}
}
Output:

6|Page

You might also like