Sitemap

java

4 min readSep 20, 2026

1.dd1

import java.awt.*;
import java.awt.event.*;

public class dd1 extends Frame {
public dd1() {
setTitle(“My First AWT Window”);
Button clickButton = new Button(“Click Me!”);
setLayout(new FlowLayout());
add(clickButton);
setSize(400, 200);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
setVisible(true);
}
public static void main(String[] args) {
new dd1();
}
}

2.dd2

import java.awt.*;

import java.awt.event.*;

public class dd2 extends Frame implements ActionListener {

private Button clickMeButton;

private Label messageLabel;

public dd2() {

setTitle(“AWT button click event”);

setSize(400, 200);

setLayout(new FlowLayout());

clickMeButton = new Button(“Click Me”);

messageLabel = new Label(“No Button Clicked”);

add(clickMeButton);

add(messageLabel);

clickMeButton.addActionListener(this);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent we) {

System.exit(0);

}

});

setVisible(true);

}

@Override

public void actionPerformed(ActionEvent e) {

if (e.getSource() == clickMeButton) {

messageLabel.setText(“Button was clicked!”);

}

}

public static void main(String[] args) {

new dd2();

}

}

3.AWT

import java.awt.*;

import java.awt.event.*;

public class NameDisplayAWT extends Frame implements ActionListener{

Label label;

TextField textField;

Button button;

NameDisplayAWT()

{

setTitle (“Name Display Example”);

setSize(400,200);

setLayout (null);

setVisible(true);

label=new Label(“Enter your name :”);

label.setBounds(50,50,120,30);

add(label);

textField=new TextField();

textField.setBounds(180,50,150,30);

add(textField);

button=new Button(“Display name: “);

button.setBounds(130,100,100,30);

add(button);

button.addActionListener(this);

addWindowListener(new WindowAdapter(){

public void windowClosing (WindowEvent e)

{

dispose ();

}

});

}

public void actionPerformed (ActionEvent e)

{

String name= textField.getText();

label.setText(“Hello,”+name+”!”);

}

public static void main (String[] args)

{

new NameDisplayAWT();

}

}

4.ip address

import java.net.InetAddress;

import java.net.UnknownHostException;

public class d1 {

public static void main(String[] args) {

try {

InetAddress ipAddress = InetAddress.getLocalHost();

String hostAddress = ipAddress.getHostAddress();

System.out.println(“Local IP address: “ + hostAddress);

} catch (UnknownHostException e) {

System.err.println(“Could not determine local host IP address: “ + e.getMessage());

}

Get Cutiee’s stories in your inbox

Join Medium for free to get updates from this writer.

}

}

5. print ip address

import java.net.*;
import java.util.Scanner;

public class URLInfo {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print(“Enter website URL: “);
String urlString = scanner.nextLine();

try {
URL url = new URL(urlString);

String hostName = url.getHost();
InetAddress address = InetAddress.getByName(hostName);

System.out.println(“Hostname: “ + hostName);
System.out.println(“IP Address: “ + address.getHostAddress());

} catch (MalformedURLException e) {
System.out.println(“Invalid URL format.”);

} catch (UnknownHostException e) {
System.out.println(“Host could not be resolved.”);
}

scanner.close();
}
}

6. sql

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseConnection {

public static void main(String[] args) {

String url = “jdbc:mysql://localhost:3306/college”;
String username = “root”;
String password = “cs@12345”;

try {
Class.forName(“com.mysql.cj.jdbc.Driver”);

Connection connection =
DriverManager.getConnection(url, username, password);

System.out.println(“Database connection successful!”);
connection.close();

} catch (ClassNotFoundException e) {

System.out.println(“MySQL JDBC Driver not found.”);

} catch (SQLException e) {

System.out.println(“Database connection failed.”);
e.printStackTrace();
}
}
}

7.swing labels

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UserInputApp extends JFrame implements ActionListener {

private JLabel nameLabel, ageLabel, displayLabel;
private JTextField nameField, ageField;
private JButton submitButton;

public UserInputApp() {

setTitle(“User Input Application”);
setSize(400, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);

nameLabel = new JLabel(“Name:”);
nameLabel.setBounds(50, 30, 80, 25);
add(nameLabel);

nameField = new JTextField();
nameField.setBounds(140, 30, 200, 25);
add(nameField);

ageLabel = new JLabel(“Age:”);
ageLabel.setBounds(50, 70, 80, 25);
add(ageLabel);

ageField = new JTextField();
ageField.setBounds(140, 70, 200, 25);
add(ageField);
submitButton = new JButton(“Submit”);
submitButton.setBounds(140, 110, 100, 30);
submitButton.addActionListener(this);
add(submitButton);

displayLabel = new JLabel(“”);
displayLabel.setBounds(50, 160, 300, 25);
add(displayLabel);

setLocationRelativeTo(null);
setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {

if (e.getSource() == submitButton) {

String name = nameField.getText();
String age = ageField.getText();

displayLabel.setText(
“Name: “ + name + “, Age: “ + age
);
}
}

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> {
new UserInputApp();
});
}
}

8. swing button

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonTextChanger extends JFrame {

private JButton myButton;

public ButtonTextChanger() {

setTitle(“Button Text Changer”);
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

myButton = new JButton(“Press me”);

myButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
myButton.setText(“Clicked!”);
}
});

add(myButton);

setVisible(true);
}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ButtonTextChanger();
}
});
}
}

9.hello servlet

import java.io.IOException;
import java.io.PrintWriter;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet(“/hello”)
public class HelloServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String userName = request.getParameter(“userName”);
response.setContentType(“text/html”);

PrintWriter out = response.getWriter();

out.println(“<!DOCTYPE html>”);
out.println(“<html>”);
out.println(“<head>”);
out.println(“<title>Hello Message</title>”);
out.println(“</head>”);

out.println(“<body>”);

if (userName != null && !userName.isEmpty()) {
out.println(“<h1>Hello “ + userName + “!</h1>”);
} else {
out.println(“<h1>Hello User!</h1>”);
}

out.println(“</body>”);
out.println(“</html>”);
}
}

10.java servlet

index.html

<!DOCTYPE html>
<html>
<head>
<title>Name Input</title>
</head>

<body>

<h2>Enter Your Name</h2>

<form action=”helloservlet” method=”post”>

<label for=”username”>Name:</label>

<input type=”text” id=”username” name=”username” required>

<br><br>

<input type=”submit” value=”Submit”>

</form>

</body>
</html>

hellojava.servlet

import java.io.IOException;
import java.io.PrintWriter;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet(“/helloservlet”)
public class HelloServlet extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String userName = request.getParameter(“username”);
response.setContentType(“text/html”);

PrintWriter out = response.getWriter();

out.println(“<!DOCTYPE html>”);
out.println(“<html>”);
out.println(“<head>”);
out.println(“<title>Hello Servlet</title>”);
out.println(“</head>”);

out.println(“<body>”);

if (userName != null && !userName.isEmpty()) {
out.println(“<h1>Hello “ + userName + “!</h1>”);
} else {
out.println(“<h1>Please enter your name.</h1>”);
}

out.println(“</body>”);
out.println(“</html>”);
}
}

--

--