Resultset

ResultSet In JDBC, the ResultSet interface represents the result set of a query. It provides methods to read data returned by a query, typically from a SELECT statement. A ResultSet is created by executing a query using the Statement or PreparedStatement objects, and it allows navigation and extraction of data from the result. Types of ResultSet Forward-only: This is the default type, where you can only move the cursor forward through the result set. Created with: Statement statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); Scrollable: ...

May 8, 2025 · 2 min · Rohan

Database Connectivity Callable Statement

CallableStatement A CallableStatement is an interface in JDBC that allows you to execute stored procedures and functions in a database. Unlike a PreparedStatement, which is used for executing SQL queries, a CallableStatement can execute database stored procedures or functions that may involve complex logic and multiple SQL statements. Benefits of CallableStatement Execute Stored Procedures: Used to call stored procedures in the database, which are precompiled and can perform complex operations. Better Performance: Stored procedures are compiled once and can be reused, improving execution speed. Return Multiple Results: Callable statements can return multiple results, including output parameters. Creating and Using a CallableStatement Create a CallableStatement Use Connection.prepareCall() to create a callable statement. ...

May 8, 2025 · 2 min · Rohan

Database Connectivity Prepared Statement

Prepared Statement A PreparedStatement is an interface in JDBC that allows you to execute SQL queries more securely and efficiently. It is used to execute parameterized SQL queries, preventing SQL injection and improving performance, especially when executing the same query multiple times with different parameters. Benefits of Prepared Statements Prevents SQL Injection: Prepared statements automatically escape user input, protecting against SQL injection attacks. Efficiency: SQL queries are precompiled, making repeated executions faster. Security: No need to manually handle input sanitization. Creating and Using a Prepared Statement Create a PreparedStatement Use Connection.prepareStatement() to create a prepared statement. ...

May 8, 2025 · 2 min · Rohan

Database Connectivity

Database Connectivity In Java, JDBC (Java Database Connectivity) provides an API for connecting to relational databases, executing SQL queries, and processing results. It enables Java applications to interact with databases like MySQL, Oracle, PostgreSQL, and others. Steps to Connect to a Database Load the Database Driver You need to load the database-specific driver class using Class.forName(). Class.forName("com.mysql.cj.jdbc.Driver"); 2. **Establish a Connection** Use the `DriverManager.getConnection()` method to establish a connection. ```java Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "password"); Create a Statement Create a Statement object to execute SQL queries. ...

May 8, 2025 · 2 min · Rohan

Swing Controls

Swing Controls Swing provides a set of controls (also known as components) to create interactive user interfaces. These controls are part of the javax.swing package and can be easily added to containers like JPanel and JFrame. Common Swing Controls Control Description JButton Button that triggers an action when clicked. JLabel Displays a short string or an image. JTextField A single-line text input field. JTextArea A multi-line text input field. JCheckBox A checkbox for binary choices (checked/unchecked). JRadioButton A radio button for selecting one option from a group. JComboBox A dropdown menu for selecting from a list of items. JList A list that allows selecting one or more items. Example: Using JButton and JLabel JFrame frame = new JFrame("Swing Controls"); JButton button = new JButton("Click Me"); JLabel label = new JLabel("Hello, World!"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { label.setText("Button Clicked!"); } }); frame.add(button, BorderLayout.NORTH); frame.add(label, BorderLayout.CENTER); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); Example: JCheckBox JCheckBox checkBox = new JCheckBox("Agree to terms"); checkBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { System.out.println("Checked"); } else { System.out.println("Unchecked"); } } }); Example: JComboBox String[] items = { "Java", "Python", "C++", "JavaScript" }; JComboBox<String> comboBox = new JComboBox<>(items); comboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String selectedItem = (String) comboBox.getSelectedItem(); System.out.println("Selected: " + selectedItem); } }); 🔗 Related Notes Swing Event Handling Event Listeners Swing

May 8, 2025 · 2 min · Rohan

Event Classes and Listener Interfaces

Event Classes and Listener Interfaces Java Swing uses event classes to encapsulate details about events and listener interfaces to respond to those events. This is the basis of Swing’s event delegation model. Event Classes Event classes are found in java.awt.event and javax.swing.event packages. Each class corresponds to a type of user action. Event Class Description ActionEvent Generated by buttons, menus, etc. MouseEvent Mouse actions like click, press KeyEvent Keyboard input WindowEvent Window state changes ItemEvent Checkbox, radio button changes public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); } Listener Interfaces Each event class has a corresponding listener interface. ...

May 8, 2025 · 1 min · Rohan

Event Listeners

Event Listeners Event Listeners are interfaces in Java Swing that define methods for responding to user actions or events. Components generate events, and listeners handle them. Commonly Used Listener Interfaces Listener Interface Description Method Signature ActionListener For button clicks and similar actions void actionPerformed(ActionEvent e) MouseListener Mouse click, enter, exit, etc. void mouseClicked(MouseEvent e) (and others) KeyListener Keyboard key press and release void keyPressed(KeyEvent e) (and others) WindowListener Window open, close, minimize, etc. void windowClosing(WindowEvent e) (and others) Example: ActionListener button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Action performed!"); } }); Example: MouseListener label.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { System.out.println("Mouse clicked!"); } }); MouseAdapter is an abstract adapter class that lets you override only the needed methods. ...

May 8, 2025 · 1 min · Rohan

Swing Event Handling

Swing Event Handling Event Handling in Swing is based on the delegation event model, where an event source (component) notifies one or more listeners when an event occurs. Key Concepts Event Source: The component that generates an event (e.g., JButton) Event Listener: An interface that receives and processes the event (e.g., ActionListener) Event Object: Carries information about the event (e.g., ActionEvent) Example: Handling Button Click import javax.swing.*; import java.awt.event.*; public class EventExample { public static void main(String[] args) { JFrame frame = new JFrame("Event Example"); JButton button = new JButton("Click Me"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Button clicked!"); } }); frame.add(button); frame.setSize(200, 150); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } Lambda Version (Java 8+) button.addActionListener(e -> System.out.println("Clicked!")); Event Flow User interacts with a component (e.g., clicks a button). Component generates an event object. Event object is passed to the registered listener’s method. 🔗 Related Notes Event Listeners Event Classes and Listener Interfaces Swing Controls

May 8, 2025 · 1 min · Rohan

Layout Managers

Layout Managers Layout Managers in Java Swing control the positioning and sizing of components within a container. They help in creating consistent GUI designs that adjust gracefully to different screen sizes. Common Layout Managers 1. FlowLayout (default for JPanel) Places components in a row, wraps to the next row when full. setLayout(new FlowLayout()); 2. BorderLayout (default for JFrame) Divides the container into five regions: NORTH, SOUTH, EAST, WEST, CENTER. setLayout(new BorderLayout()); add(new JButton("North"), BorderLayout.NORTH); 3. GridLayout Places components in a grid with equal-sized cells. ...

May 8, 2025 · 1 min · Rohan

Components and Containers

Components and Containers In Swing, Components are the visual elements like buttons, labels, and text fields, while Containers hold and organize these components. Components Components are instances of classes that inherit from java.awt.Component. Common examples include: JButton JLabel JTextField JCheckBox JComboBox JButton button = new JButton("Click Me"); JLabel label = new JLabel("Hello"); Containers Containers are components that can hold other components (including other containers). Examples: JFrame – Top-level window JPanel – Generic lightweight container JDialog – Dialog window JPanel panel = new JPanel(); panel.add(button); frame.add(panel); Nesting Components You can nest components within containers to create structured layouts. ...

May 8, 2025 · 1 min · Rohan