













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














package mypack; public class MyException extends Exception { public MyException(String message) { super(message); } } import mypack.MyException; public class Main { static void checkMarks(int marks) throws MyException { if (marks < 0 || marks > 100) { throw new MyException("Marks must be between 0 and 100."); } System.out.println("Valid marks: " + marks); } public static void main(String[] args) { try { checkMarks(85); checkMarks(120); } catch (MyException e) { System.out.println("Custom Exception: " + e.getMessage()); } }}
6. Output
this.threadName = threadName; } public void run() { for (int i = 0; i < 5; i++) { counter.increment(threadName); } } } public class Main { public static void main(String[] args) throws InterruptedException { Counter counter = new Counter(); MyThread t1 = new MyThread(counter, "Thread-1"); MyThread t2 = new MyThread(counter, "Thread-2"); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println("Final Count = " + counter.getCount()); } }
6. Output
import java.util.ArrayList; import java.util.Iterator; public class Main { public static void main(String[] args) { ArrayList students = new ArrayList<>(); students.add("Amit"); students.add("Neha"); students.add("Rahul"); students.add("Sahil"); System.out.println("Students in ArrayList:"); Iterator itr = students.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } } }
import java.util.HashSet; import java.util.HashMap; import java.util.Set; import java.util.Map; public class Main { public static void main(String[] args) { Set cities = new HashSet<>(); cities.add("Mumbai"); cities.add("Pune"); cities.add("Nashik"); cities.add("Mumbai"); System.out.println("HashSet: " + cities); Map students = new HashMap<>(); students.put(101, "Amit"); students.put(102, "Neha"); students.put(103, "Rahul"); System.out.println("HashMap:"); for (Map.Entry entry : students.entrySet()) { System.out.println(entry.getKey() + " - > " + entry.getValue()); } }}
6. Output
course.add("ENTC"); add(course); Button submit = new Button("Submit"); submit.addActionListener(this); add(submit); result = new Label(" "); add(result); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); } }); setVisible(true); } public void actionPerformed(ActionEvent e) { result.setText("Hello " + nameField.getText() + "! Course: " + course.getSelectedItem()); } public static void main(String[] args) { new AWTForm(); } }
6. Output
import java.awt.; import java.awt.event.; public class AWTComponents extends Frame implements ActionListener { TextField textField; Checkbox agree; Label result; AWTComponents() { setTitle("AWT Components Demo"); setSize(400, 250); setLayout(new FlowLayout()); add(new Label("Enter Name:")); textField = new TextField(20); add(textField); agree = new Checkbox("I agree"); add(agree); Button button = new Button("Submit"); button.addActionListener(this); add(button); result = new Label(" "); add(result); addWindowListener(new WindowAdapter() {
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; public class JDBCCRUD { private static final String URL = "jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1"; private static final String USER = "sa"; private static final String PASSWORD = ""; public static void main(String[] args) { try { Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); System.out.println("Database Connected Successfully!"); Statement stmt = conn.createStatement(); stmt.execute("CREATE TABLE IF NOT EXISTS Student (id INT PRIMARY KEY, name VARCHAR(50), course VARCHAR(50))"); // 1. CREATE (Insert) String insertSQL = "INSERT INTO Student (id, name, course) VALUES (?, ?, ?)"; PreparedStatement pstmtInsert = conn.prepareStatement(insertSQL); pstmtInsert.setInt( 1 , 101 ); pstmtInsert.setString( 2 , "Sahil"); pstmtInsert.setString( 3 , "CSE"); int rowsInserted = pstmtInsert.executeUpdate(); System.out.println("Inserted " + rowsInserted + " record."); // 2. READ (Select) String selectSQL = "SELECT * FROM Student"; PreparedStatement pstmtSelect = conn.prepareStatement(selectSQL); ResultSet rs = pstmtSelect.executeQuery(); System.out.println("--- Current Records ---"); while (rs.next()) { System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name") + ", Course: " + rs.getString("course")); } // 3. UPDATE String updateSQL = "UPDATE Student SET course =? WHERE id = ?"; PreparedStatement pstmtUpdate = conn.prepareStatement(updateSQL); pstmtUpdate.setString( 1 , "Computer Engineering"); pstmtUpdate.setInt( 2 , 101 ); int rowsUpdated = pstmtUpdate.executeUpdate(); System.out.println("Updated " + rowsUpdated + " record."); // 4. DELETE String deleteSQL = "DELETE FROM Student WHERE id = ?"; PreparedStatement pstmtDelete = conn.prepareStatement(deleteSQL); pstmtDelete.setInt( 1 , 101 ); int rowsDeleted = pstmtDelete.executeUpdate(); System.out.println("Deleted " + rowsDeleted + " record."); conn.close(); } catch (Exception e) { System.out.println("JDBC Error: " + e.getMessage()); } } }
import java.io.; import jakarta.servlet.; import jakarta.servlet.http.; import jakarta.servlet.annotation.; @WebServlet("/hello") public class HelloServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println("Hello from Servlet"); out.println("
Request processed successfully.
"); out.println(""); } }import java.io.; import jakarta.servlet.; import jakarta.servlet.annotation.; @WebFilter("/") public class LoggingFilter implements Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("Request received."); System.out.println("Request is being processed..."); chain.doFilter(request, response); System.out.println("Response sent successfully."); } }
import jakarta.servlet.http.; import jakarta.servlet.annotation.; @WebListener public class SessionListener implements HttpSessionListener { public void sessionCreated(HttpSessionEvent event) { System.out.println("Session Created: " + event.getSession().getId()); } public void sessionDestroyed(HttpSessionEvent event) { System.out.println("Session Destroyed: " + event.getSession().getId()); } }
6. Output
Welcome, <%= session.getAttribute("username") %>! Logout
<% } else { %>
Invalid Username or Password Try Again
<% } %>
session.invalidate(); %>
You have been logged out successfully. Login Again
6. Output
JSP Form Handling
JSP Form Handling
Enter Name: Enter Age:
<% String name = request.getParameter("name"); String age = request.getParameter("age"); if (name != null && age != null) { %>
Result
Welcome, <%= name %>!
Your age is: <%= age %>
<% } %>