0% found this document useful (0 votes)
19 views7 pages

SR

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)
19 views7 pages

SR

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/ 7

public class Main {

public static void main(String[] args) {

Systemout.println("Hello froma package!");

class Thread Demo extends Thread {

public void run() {

try{

//Moving thread to Timed Waiting state

Thread.sleep(150),

} catch (InterruptedException e) {

e.printStackTrace();

Systemout.println("State after completion "+Thread.currentThread().getState());

public static void main(String[] args) throws InterruptedException {

ThreadDempt1 = new ThreadDemo◊);

Systemout.println("State when created "+tl.getState());

t1.start();

Systemout.println("State when started "+t1.getState());

//waiting for thread to die

t1.join();

Systemout.println("State after thread ended its task "+tl.getState());

import java.awt.*

import java.awt.event.*

public class AwtExample extends Frame{

Awt Example◊ {
// create a button

Button b = new Button("Click Me");

b.setBounds(30, 100, 80, 30); // setting button position

// add button to the frame

add(b);

setSize(300, 300); //frame size 300 width and 300 height

setLayout(null); // no layout manager

setVisible(true); // now frame will be visible

// close the frame when close button is clicked addWindowListener(new WindowAdapter({

public void windowClosing(WindowEvent e) {

dispose◊);

});

public static void main(String args[]) {

new AwtExample◊); // creating instance

class Inner Class{//package-private class, not accessible outside 'comexample.project' package

void display() {

Systemout.println("Hello from Inner Class!");

public class Main {

public static void main(String[] args) {

Inner Class obj = new Inner Class(); obj.display();

import java.sql.Connection,

import java.sql.DriverManager,

import java.sql Result Set;

import java.sql.Statement;
public class JdbcExample {

public static void main(String[] args) {

try{

String url="jdbcmysql://localhost:3306/yourDatabase",

String user="username",

String password="password";

// Establish Connection

Connection conn =DriverManager.getConnection(url, user, password),

// Create a statement

Statement stmt =com.createStatement();

// Execute a query

ResultSet rs=stmt.executeQuery("SELECT * FROM yourTable"),

//Iterate through the result set

while (rs.next()) {

Systemout.println(rs.getString("columnName"));

// Clean up environment

rsclose◊);

stmt.close();

conn.close();

} catch (Exception e) {

e printStackTrace();

Make sure to replace "jdbc:mysql://localhost:3306/yourDatabase", "username", "password",

"yourTable", and "columnName" with actual values applicable to your database.

import java applet.Applet;


import java.awt.Graphics,

public class Hello Applet extends Applet {

public void paint (Graphicsg) {

gdrawstring("HELLO JAVA", 20, 20),

To run this applet, you would need an HTML file:

<html>

<body>

<applet code="Hello Applet.class" width="300" height="300"></applet>

<body> </html>

This HTML file would then be opened in a browser that supports Java applets or used with an applet
viewer tool provided with the JDK.

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

public class SimpleJDBCConnection {

public static void main(String[] args) {

try {

// Load the JDBC driver

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

Connection connection = DriverManager.getConnection(

"jdbc:mysql://localhost:3306/testdb", "root", "password");

Statement statement = connection.createStatement();

ResultSet resultSet = statement.executeQuery("SELECT * FROM users");

while (resultSet.next()) {
System.out.println(resultSet.getString("name"));

resultSet.close();

statement.close();

connection.close();

} catch (Exception e) {

e.printStackTrace();

Replace "jdbc:mysql://localhost:3306/testdb", "root", and "password" with your database details.

class Printer extends Thread {

private String message;

public Printer(String msg) {

this.message = msg;

public void run() {

for (int i = 0; i < 5; i++) {

System.out.println(message);

try {

Thread.sleep(100);

} catch (InterruptedException e) {

System.out.println("Thread interrupted.");

public class MultiThreadingDemo {

public static void main(String[] args) {


Printer thread1 = new Printer("Thread 1 is running");

Printer thread2 = new Printer("Thread 2 is running");

thread1.start();

thread2.start();

import java.applet.Applet;

import java.awt.Graphics;

public class LifecycleApplet extends Applet {

public void init() {

System.out.println("Applet initialized");

public void start() {

System.out.println("Applet starting");

public void stop() {

System.out.println("Applet stopping");

public void destroy() {

System.out.println("Applet destroyed");

public void paint(Graphics g) {

g.drawString("See console for life cycle messages.", 10, 20);

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

public class HelloServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

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

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

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

This servlet would need to be deployed on a servlet container such as Apache Tomcat. You would also
need to configure it in the web.xml file or use annotations in newer Java versions to specify URL

patterns.

You might also like