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

creating A Table in A Database, Inserting Records, Executing Queries and Displaying //resultset As Event Handling of A Button

This JavaFX program demonstrates creating a database table, inserting records, executing queries, and displaying results when a button is clicked. The button's event handler connects to an SQLite database, creates a table, inserts a sample record, runs a query to select all records, and prints the results to the console. It closes all statements and the connection to practice good database management.

Uploaded by

ramesh kumar
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)
242 views

creating A Table in A Database, Inserting Records, Executing Queries and Displaying //resultset As Event Handling of A Button

This JavaFX program demonstrates creating a database table, inserting records, executing queries, and displaying results when a button is clicked. The button's event handler connects to an SQLite database, creates a table, inserts a sample record, runs a query to select all records, and prints the results to the console. It closes all statements and the connection to practice good database management.

Uploaded by

ramesh kumar
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/ 3

//creating a table in a database,inserting records,executing queries and displaying

//resultset as event handling of a Button

import javafx.application.*;
import javafx.scene.*;
import javafx.stage.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.event.*;
import javafx.geometry.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DatabaseExample extends Application {


Label response;
public static void main(String[] args) {
//Start the JavaFX application by calling launch().
launch(args);
}
//Override the start() method.
public void start(Stage myStage) {
//Give the stage a title.
myStage.setTitle("Demonstrate JavaFX Buttons and Events.");
//Use a FlowPane for the root node. In this case,
//vertical and horizontal gaps of 10.
FlowPane rootNode = new FlowPane(10, 10);
//Center the controls in the scene.
rootNode.setAlignment(Pos.CENTER);
//Create a scene.
Scene myScene = new Scene(rootNode, 500, 300);
//Set the scene on the stage.
myStage.setScene(myScene);
//Create a label.
response = new Label("Push the button for database connection");
//Create two push buttons.
Button btnAlpha = new Button("submit and fetch");
//Handle the action events for the Alpha button.
btnAlpha.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent ae) {
try{ Connection connection =
DriverManager.getConnection("jdbc:sqlite:C:\\DATABASE\\example1.db");
Statement statement = connection.createStatement();
statement.execute("CREATE TABLE IF NOT EXISTS contacts (name TEXT , phone INTEGER ,
email TEXT )");
statement.execute("INSERT INTO contacts VALUES ('Default' , 123456,
'default@gmail.com')");
ResultSet results = statement.executeQuery("SELECT * FROM contacts");
while(results.next()) {
System.out.println(results.getString("name") + " " + results.getInt("phone") + " " +
results.getString("email") );
}
statement.close(); // Always a good practice to close all the open statemnts and the
connections
connection.close(); /* If Connections are not closed then when the progaram is run again
it leads to some
fatal errors like file corruption etc.*/
}catch(SQLException e){
System.out.println("Something went wrong : " +e.getMessage());
}}
});

//Add the label and buttons to the scene graph.


rootNode.getChildren().addAll(btnAlpha, response);
//Show the stage and its scene.
myStage.show();
}
}
OUTPUT

vij 123456 default@gmail.com


vij 123456 default@gmail.com
Default 123456 default@gmail.com
Default 123456 default@gmail.com
Default 123456 default@gmail.com
Default 123456 default@gmail.com
Default 123456 default@gmail.com
Default 123456 default@gmail.com

Note: Dispatch a separate thread for such event handlers

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy