Java Cie 2
Java Cie 2
Has a fixed width, but can be resized Can be resized to display multiple
lines of text
Does not support multiple lines of Supports multiple lines of text, with
text line wrapping and scrolling
Supports basic editing features, such Supports advanced editing features,
as cut, copy, and paste such as text formatting, font styles,
and colors
Can be used for password fields, Can be used for displaying read-only
with asterisks (*) or dots (·) masking text, such as help messages or
the input instructions
LAQ
01,BUILD a Java program to copy the content of one file to another file?
Ans:
import java.io.*;
try {
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
fis.close();
fos.close();
import java.io.*;
class Studentinfo implements Serializable {
String name;
int rid;
static String contact;
Studentinfo(String n, int r, String c) {
this.name = n;
this.rid = r;
Studentinfo.contact = c;
}
}
public class SerializableDemo {
public static void main(String args[]) throws Exception {
Studentinfo si = new Studentinfo("Abhi", 104, "110044");
System.out.println("Serialization started");
FileOutputStream fos = new FileOutputStream("student.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(si);
System.out.println("Serialization ended");
System.out.println("Deserialization started");
FileInputStream fis = new FileInputStream("student.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
si = (Studentinfo) ois.readObject();
System.out.println("Deserialization ended");
System.out.println("Student object details:");
System.out.println(si.name);
System.out.println(si.rid);
System.out.println(Studentinfo.contact);
}
}
Output:
Serialization started
Serialization ended
Deserialization started
Deserialization ended
Student object details:
Abhi
104
110044
Q5, create an application program in java to demonstrate mouse event, handling key
events ?
Ans: mouse:
import java.awt.*;
importjava.awt.event.*;
importjava.applet.*;
/*
<applet code="Mouse" width=300 height=100>
</applet>
*/
public class Mouse extends Applet implements MouseListener, MouseMotionListener
{
String msg = "";
int mouseX = 0, mouseY = 0; // coordinates of mouse
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
mouseX = 0;
mouseY = 10;
msg = "Mouse clicked.";
repaint();
}
public void mouseEntered(MouseEvent me)
{
mouseX = 0;
mouseY = 10;
msg = "Mouse entered.";
repaint();
}
public void mouseExited(MouseEvent me)
{
mouseX = 0;
mouseY = 10;
msg = "Mouse exited.";
repaint();
}
public void mousePressed(MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
msg = "Down";
repaint();
}
public void mouseReleased(MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
msg = "Up";
repaint();
}
public void mouseDragged(MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
msg = "*";
showStatus("Dragging mouse at " + mouseX + ", " + mouseY);
repaint();
}
public void mouseMoved(MouseEvent me)
{
showStatus("Moving mouse at " + me.getX() + ", " + me.getY());
}
public void paint(Graphics g)
{
g.drawString(msg, mouseX, mouseY);
}
}
Ans:
import java.sql.*; class Create
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con;
con=DriverManager.getConnection("jdbc:odbc:csec”,”system”,”mlrit”) Statement
stmt=con.createStatement();
String sql = "CREATE TABLE REGISTRATION " + "(id INTEGER not NULL, " +
" first VARCHAR(255), " + " last VARCHAR(255), " + " age INTEGER, " +
" PRIMARY KEY ( id ))";
stmt.executeUpdate(sql);
System.out.println("Created table in given database...");
con.close();
}//try
catch(Exception e)
{
e.printStackTrace();
}
}
}
Q7, Difference between generic servlet and http servlet
Ans:
Generic Servlet HttpServlet
Location Works with multiple protocols (HTTP, Specifically designed for handling
FTP, SMTP, etc.). HTTP protocol.
Functionality Generic, protocol-independent Extends Generic Servlet to handle
servlet implementation. HTTP-specific tasks.
Methods Implements the Extends Generic Servlet and adds
javax.servlet.Servlet interface. HTTP-specific methods.
Requests Handles requests from various Handles specifically HTTP requests
protocols. and related tasks.
HttpServletRequest Not available. Provides additional methods for
handling HTTP requests.
HttpServletResponse Not available. Provides additional functionality
for HTTP responses.
Common Usage Used for servlets handling non- Used for servlets handling HTTP
HTTP protocols. requests and responses.
1.JLabel: JLabel is a class of Java Swing. It is used to display a short string or an image icon. JLabel can display
text, image or both. JLabel is only a display of text or image and it cannot get focus. JLabel is inactive to input
events such as mouse focus or keyboard focus.
2.ImageIcon: ImageIcon is a class of Java Swing. It is used to display an image in a GUI application.
ImageIcon can be created from an image file or from an array of bytes.
3.JTextField: JTextField is a class of Java Swing. It is used to create a single-line text field for accepting user input.
4.JScrollPane: JScrollPane is a class of Java Swing. It is used to create a scrollable view of a component that is
larger than the visible area.
5.JList: JList is a class of Java Swing. It is used to create a list of items that can be selected by the user.
6.JComboBox: JComboBox is a class of Java Swing. It is used to create a drop-down list of items that can be
selected by the user
Example:
import javax.swing.*;
import java.awt.*;
// Add a list
String[] items = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
JList<String> list = new JList<>(items);
panel.add(new JScrollPane(list), BorderLayout.WEST);