projts
projts
projts
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
@WebServlet("/cookieDemo")
public class CookieDemo extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// Set the content type
resp.setContentType("text/html");
// Create a cookie
Cookie cookie = new Cookie("username", "JohnDoe");
cookie.setMaxAge(60 * 60); // Set cookie to expire in 1 hour
resp.addCookie(cookie); // Add the cookie to the response
import java.io.*;
import javax.servlet.*;
// Write a response
pw.println("<h1>HELLO WORLD!!!!!</h1>");
try {
// Load the JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Create PreparedStatement
pst = con.prepareStatement(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// Close the PreparedStatement and Connection
if (pst != null) pst.close();
if (con != null) con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
try {
// Load the JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Create PreparedStatement
pst = con.prepareStatement(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// Close the PreparedStatement and Connection
if (pst != null) pst.close();
if (con != null) con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
Q7. Write a JDBC program to delete record from the table ‘student’.
// JDBC program to delete a record from student1 table
import java.sql.*;
class JdbcDelete {
public static void main(String[] args) {
Connection con = null;
Statement st = null;
try {
// Load the JDBC driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
import java.net.*;
class Server4 {
System.out.println("Client connected!");
String s1 = "";
String s2 = "";
// Chat loop
while (true) {
s1 = in.readLine();
// If the client sends "stop", break the loop and end the connection
if (s1.equals("stop")) {
break;
}
// Read message to send to client
s2 = br.readLine();
out.println(s2);
in.close();
out.close();
br.close();
soc.close();
ss.close();
Client:
import java.io.*;
import java.net.*;
class Client4 {
String s1 = "";
String s2 = "";
// Chat loop
while (true) {
s1 = br.readLine();
// If the message is "stop", exit the loop and end the connection
if (s1.equals("stop")) {
System.out.println("Ending chat...");
break;
s2 = in.readLine();
in.close();
out.close();
br.close();
soc.close();
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MouseDragExample extends JFrame {
public MouseDragExample() {
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseMotionListener(new MouseMotionAdapter() {
@Override
int x = e.getX();
int y = e.getY();
});
frame.setVisible(true);
}
}
import java.net.*;
import java.util.Scanner;
try {
} catch (UnknownHostException e) {
} finally {
scanner.close();
Q.12 Write a program using URL class to retrieve host, protocol, port
and file of URL.
import java.net.*;
try {
// Create a URL object with the target URL
connection.connect();
} catch (Exception e) {
e.printStackTrace();
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
</applet>
*/
JTextField tf1;
JPasswordField pass;
JButton b;
Container c;
c = getContentPane();
// Create components
tf1.setToolTipText("Enter Username");
pass.setToolTipText("Enter Password");
b = new JButton("Login");
c.add(l1);
c.add(tf1);
c.add(l2);
c.add(pass);
c.add(b);
// Add action listener for the button
b.addActionListener(this);
if (password.length() < 6) {
} else {
showStatus("Correct Password");
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
</applet>
*/
addKeyListener(new KeyAdapter() {
switch (keyCode) {
case KeyEvent.VK_F1:
break;
case KeyEvent.VK_F2:
break;
case KeyEvent.VK_UP:
break;
case KeyEvent.VK_DOWN:
break;
default:
break;
});
setFocusable(true);
}
public void paint(Graphics g) {
import javax.swing.*;
JProgressBar jb;
public ProgressBar() {
// Frame settings
setSize(250, 150);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
} catch (Exception e) {
e.printStackTrace();
m.setVisible(true);
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
}
public static void main(String[] args)
{
new JTableExample();
}
}
Q18. Develop a program to use of tree components in swing.
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.event.*;
public class MenuExample extends Frame
{
MenuBar mb;
MenuItem m1,m2,m3,m4;
Menu file,edit;
public MenuExample()
{
setTitle("MenuBar Example");
setSize(500,500);
setVisible(true);
mb=new MenuBar();
setMenuBar(mb);
file=new Menu("File");
edit=new Menu("Edit");
mb.add(file);
mb.add(edit);
m1=new MenuItem("New...");
m2=new MenuItem("Open...");
m3=new MenuItem("Save As...");
m4=new MenuItem("Exit");
file.add(m1);
file.add(m2);
file.add(m3);
file.addSeparator();
file.add(m4);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args)
{
new MenuExample();
}
}
import javax.swing.*;
import java.awt.*;
// Set GridBagLayout
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
// Add a label
JLabel label = new JLabel("GridBagLayout Example");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.insets = new Insets(5, 5, 10, 5);
frame.add(label, gbc);
// Add Button 1
JButton button1 = new JButton("Button 1");
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
frame.add(button1, gbc);
// Add Button 2
JButton button2 = new JButton("Button 2");
gbc.gridx = 1;
gbc.gridy = 1;
frame.add(button2, gbc);
// Add Button 3
JButton button3 = new JButton("Button 3");
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
frame.add(button3, gbc);
Q23. Write a program to create Button with caption OK, RESET and
CANCEL.
import java.awt.*;
import java.applet.*;
add(b1);
add(b2);
add(b3);
}
}