0% found this document useful (0 votes)
13 views

Sample Code

The document provides code examples for 5 different Java technologies: 1) a simple servlet that prints text, 2) basic JDBC code to connect to a database and retrieve data, 3) an AWT/Swing program with a window and button, 4) a basic applet that displays text, and 5) use of the Collection Framework with an ArrayList. Each code snippet demonstrates fundamental aspects of the given technology.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Sample Code

The document provides code examples for 5 different Java technologies: 1) a simple servlet that prints text, 2) basic JDBC code to connect to a database and retrieve data, 3) an AWT/Swing program with a window and button, 4) a basic applet that displays text, and 5) use of the Collection Framework with an ArrayList. Each code snippet demonstrates fundamental aspects of the given technology.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Servlet Program:

A simple servlet that prints "Hello, Servlet!" on the web page.

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<html><body>");

out.println("<h1>Hello, Servlet!</h1>");

out.println("</body></html>");

2. JDBC Code:

A basic JDBC code snippet to connect to a database and retrieve data.

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

public class JdbcExample {

public static void main(String[] args) {

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

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yourdb",


"username", "password");

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("SELECT * FROM your_table");

while (rs.next()) {

System.out.println(rs.getInt(1) + " " + rs.getString(2));

con.close();

} catch (Exception e) {

System.out.println(e);

3. AWT/Swing Program:

A simple AWT/Swing program that displays a window with a button.

import javax.swing.JButton;

import javax.swing.JFrame;

public class MySwingApp {

public static void main(String[] args) {

JFrame frame = new JFrame("My Swing App");

JButton button = new JButton("Click me");

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

frame.add(button);

frame.setSize(400, 500);
frame.setLayout(null);

frame.setVisible(true);

4. Applet Code:

A basic applet code that displays "Hello, Applet!" in the applet window.

import java.applet.Applet;

import java.awt.Graphics;

public class MyApplet extends Applet {

public void paint(Graphics g) {

g.drawString("Hello, Applet!", 20, 20);

5. Collection Framework:

A short example demonstrating the use of the Collection Framework with ArrayList.

import java.util.ArrayList;

import java.util.Iterator;

public class CollectionExample {

public static void main(String[] args) {

ArrayList<String> list = new ArrayList<>();

list.add("Java");

list.add("Servlet");

list.add("JDBC");

// Using Iterator to traverse the elements

Iterator<String> iterator = list.iterator();


while (iterator.hasNext()) {

System.out.println(iterator.next());

You might also like