Java Program Development
Java Program Development
Development:
User and
Authentication
Database
A database is a structured collection of
data that is organized in a way that
allows for efficient storage, retrieval,
manipulation, and management of
information. It acts as a centralized
repository where data can be stored,
accessed, and updated by authorized
users or applications.
Database
Examples of popular DBMS include MySQL,
PostgreSQL, Oracle Database, Microsoft SQL Server,
and MongoDB.
On the right side we’ll see the Component Palette a graphical tool
that provides developers with a convenient way to access and use
various user interface (UI) components when designing graphical
user interfaces (GUI) for their Java applications.
Panel A generic lightweight container.
Label A display area for a short text string or an image or both.
Radio Button An item that can be selected or deselected.
Combo Box A component that combines a button or editable fields and a drop-down list.
Text Field A lightweight component that allows the editing of a single line text.
Scroll Bar A component that allows to adjust the contents of the viewing area of another
component.
Progress Bar A progress bar typically communicates the progress of some work by displaying its
percentage of completion and possibly a textual display of this percentage.
Password Field A component that allows the editing of a single line of text where the view indicates
something was typed, but does not show the original character.
Table A component used to display and edit regular two-dimensional tables of cells.
In our Login Frame, we have created two (2) buttons. Login and Register. Let’s code our Register button, so that when we click
it, it’ll proceed in our Register Frame.
Double click the REGISTER button and enter the following code:
Do the same thing with the Registration Frame so that both frames are now linked.
import java.sql.Statement; This import statement is used to import the Statement interface
from the java.sql package. The Statement interface represents a SQL
statement that is sent to the database for execution. It allows Java
programs to execute SQL queries, updates, and other database
operations.
import javax.swing.JFrame; This import statement is used to import the JFrame class from the
javax.swing package. The JFrame class represents a window in a Java
GUI application. It provides the basic functionalities of a window,
such as borders, title bars, and menus.
import javax.swing.JOptionPane; This import statement is used to import the JOptionPane class from
the javax.swing package. The JOptionPane class provides methods
for creating and managing dialog boxes, including message dialogs,
input dialogs, and option dialogs, in Java GUI applications.
Let’s start coding the Register button.
// Method to handle the action when the register button is clicked
private void btnRegRegisterActionPerformed(java.awt.event.ActionEvent evt) {
// Declaration of variables to store URL, username, and password for database connection
String SUrl, SUser, SPass;
// Setting the URL, username, and password for the database connection
SUrl = "jdbc:MySQL://localhost:3306/shs_db";
SUser = "root";
SPass = "";
try {
// Loading the MySQL JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
} else {
// If both fields are filled, retrieving the username and password
username = txtRegUsername.getText();
// Creating an SQL query to insert the username and password into the database
query = "INSERT INTO accountdetails(accountUsername, accountPassword)"+
"VALUES('"+username+"', '"+password+"')";
// Catching any exceptions that might occur during database connection or query execution
} catch (Exception e){
JOptionPane.showMessageDialog(this, "Error occurred during registration. Please try again later.");
}
}
What is a try-catch block?
a try-catch block is a mechanism used to handle exceptions, which are unexpected
events that occur during the execution of a program.