Assignment 5 CSE 2year 1sem AY 2024 25
Assignment 5 CSE 2year 1sem AY 2024 25
Class.forName(“oracle.jdbc.driver.OracleDriver”);
2-B DriverManager.registerDriver()
DriverManager is a Java inbuilt class with a static member register. Here we call the constructor
of the driver class at compile time. The following example uses
DriverManager.registerDriver()to register the Oracle driver as shown below:
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver())
Step 3: Establish a connection using the Connection class object
After loading the driver, establish connections as shown below as follows:
user: Username from which your SQL command prompt can be accessed.
password: password from which the SQL command prompt can be accessed.
con: It is a reference to the Connection interface.
Url: Uniform Resource Locator which is created as shown below:
String url = “ jdbc:oracle:thin:@localhost:1521:xe”
Where oracle is the database used, thin is the driver used, @localhost is the IP Address where a
database is stored, 1521 is the port number and xe is the service provider. All 3 parameters above
are of String type and are to be declared by the programmer before calling the function. Use of
this can be referred to form the final code.
Statement st = con.createStatement();
con.close();
Example:
// Java Program to Establish Connection in JDBC
// Importing database
import java.sql.*;
// Importing required classes
import java.util.*;
// Main class
class Main {
System.out.println("enter name");
String name = k.next();
// Registering drivers
DriverManager.registerDriver(
new oracle.jdbc.OracleDriver());
// Creating a statement
Statement st = con.createStatement();
// Executing query
int m = st.executeUpdate(sql);
if (m == 1)
System.out.println(
"inserted successfully : " + sql);
else
System.out.println("insertion failed");
ResultSet maintains cursor/pointer which points to a single row of the query results. Using
navigational and getter methods provided by ResultSet, we can iterate and access database
records one by one. ResultSet can also be used to update data.
The above diagram shows the place of ResultSet in the JDBC Framework. ResultSet can be
obtained by executing SQL Query
using Statement, PreparedStatement or CallableStatement.
AutoCloseable, Wrapper are super interfaces of ResultSet. Now we will see how to work with
ResultSet in our Java programs.
ResultSet Example
We will be using MySQL for our example purpose. Use below DB script to create a database and
table along with some records.
package com.journaldev.examples;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
empId:2
firstName:Josh
lastName:Martin
dob:1988-10-22
empId:3
firstName:Ricky
lastName:Smith
dob:1999-05-11
// create a image
Image i = new Image(input);
// create a label
Label b = new Label("AITS-TIRUPATI", iw);
// create a scene
Scene sc = new Scene(r, 500, 500);
s.show();
}
Layouts are the top level container classes that define the UI styles for scene graph objects.
Layout can be seen as the parent node to all the other nodes. JavaFX provides various layout
panes that support different styles of layouts.
Layout Classes
javafx.scene.layout Package provides various classes that represents the layouts. The classes are
described in the table below.
Class Description
JavaFX BorderPane
BorderPane arranges the nodes at the left, right, centre, top and bottom of the screen. It is
represented by javafx.scene.layout.BorderPane class. This class provides various methods
like setRight(), setLeft(), setCenter(), setBottom() and setTop() which are used to set the
position for the specified nodes. We need to instantiate BorderPane class to create
the BorderPane layout.
Properties
The properties of BorderPane class along with their setter methods are given in the table below.
When mouse event occurs, the top-most node under cursor is picked and the event is delivered to
it through capturing and bubbling phases described at EventDispatcher.
The mouse (pointer's) location is available relative to several coordinate systems: x,y - relative to
the origin of the MouseEvent's node, sceneX,sceneY - relative to to the origin of the Scene that
contains the node, screenX,screenY - relative to origin of the screen that contains the mouse
pointer.
Write a JavaFX program that listens for mouse clicks and displays a message when the left
mouse button is clicked.
//Main.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Mouse Click App");
// Create a scene
Scene scene = new Scene(vbox, 400, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
}
we create a JavaFX application with a 'VBox' layout containing a 'Label' component to display
the mouse click status. We then create a scene and register a mouse click event handler. The
event handler checks if the left mouse button (primary button) is clicked and updates the
'clickLabel' accordingly.
Finally when we run the program and left-click the mouse, we will see the message "Mouse
Click: Left button clicked."
Sample Output: