Has satisfactory background in Java, Swing, and Oracle Database.
Has used Windows operating system a few or many times.
Has decent reading comprehension to understand what it is written on this blog.
Integrating Oracle Database into a Java application adds a persistent link between the database and the application and provides access to the data from the database server. Persisting the stability and completeness of the database server requires careful administration and management.
Look for “Get Started” in the Start Menu or Desktop that has the logo of a database and XE in the bottom right, and open it by clicking once (Start Menu) or twice (Desktop).
Once opened, click the "Application Express" tab, then this redirects you to the login page if not yet logged in.
Enter your credentials, in this instance, “sys” for username and “password” for password.
Once logged in, enter the following information to create an APEX workspace for the database. The password of it is "password". Then, click "Create Workspace".
After creation, click "click here" to navigate to the workspace login page.
Once redirected, just enter the password for the workspace, in this instance: password, as the values on both workspace and username fields are already present. Then, click "Login". This navigates you to the database management page, where you can additionally use SQL queries to test the database and other important tools.
After login, open NetBeans to establish the connection from the database server. At the Services tab, right click on Databases, then click New Connection. This opens up the wizard (dialog) where you can set up or customize the connection and change its additional properties.
After login, open NetBeans to obtain the connection from the database. Make sure to select "Oracle Thin" as a type of driver to use in a connection. Then, click "Add" to add the driver file, which is a JAR file. Once the dialog appeared, browse for the file.
Enter the credentials of your recently created workspace, in this instance, "ACLCDATABASE" for the username field and "password" for the password field. Click Test Connection to ensure the server activity and the correctness of the credentials you just entered. When the testing is successful, click Finish to create the database connection.
After creation of it, you can modify the database by creating tables, changing the behavior of it, as it is connected automatically.
To create a table in the database, right click on Tables, then the table creation dialog will appear.
In this dialog, change the table name of your desire, and add columns as many as you want. You may edit a column if you need to alter the column's attributes, constraints, etc., or move it up or down.
In this dialog opened by the Add column button, name the column whatever you like, change the data type to what it is needed, add default value in case of missing value, and check all the constraints that apply to the column.
After that, then you've successfully created your own table, where you can read, insert, update, and delete data.
From your project, right click on Libraries, then click Add Library.
Click Create, then name the library, in this instance, Oracle JDBC.
Click Add JAR/Folder, once the browse dialog popped up, browse for the driver file.
Design a JFrame form that looks exactly like this. The form consists of two text panes and a button. It does the insertion of data that contains tiny information, such as ID and name, and it will be implemented later. These graphical components can be reaped by dragging it from the Palette tab and dropping it into the form.
At the Source tab of the main form, insert these four lines of imports above the class and below the package statement if it is within the package.
Now, back to the Design tab, select the button and right click on it, and click Change Variable Name. Changing the variable name of the component can neatly eliminate confusion and mess. You also need to change the variable name of the text fields. In this instance, txtId for the ID text field and txtName for the name text field.
After renaming the variables of the components, double-click the "insert" button to navigate to its source code.
This is where the code within the method (function) gets executed when a user presses the button. We will get in touch with this method later.
Above initComponents(), declare five instance variables: null connection, null prepared statement, a JDBC URI, username, and password. For the values of username and password, use the database workspace's credentials (ACLCDatabase & password) for those.
A method is a function that is declared or present within the class. Nevertheless, it can still be called as a function.
Instance variables are variables that sit beside the declared methods and constructor. These are the variables that can be accessed by all declared or defined methods, unlike local variables that are declared inside the method.
To copy the database URL, follow these steps below:
Right-click on the newly created connection, then click Properties.
Click the three dots at the Database URL.
Select the URL and copy, lastly close the window.
Now, back to the button's method, insert this block of code that consists of the try-catch block, which handles exceptions to prevent abrupt interruption of the program.
The first statement inside the try block establishes the connection from the database server using the URL, username, and password. If not connected successfully, then it will be handled by the catch block, which shows the dialog with the message from the exception displayed that is converted to string.
Now, insert the remaining six lines of code, which contains the logic or functionality for inserting the data into the database. These statements are explained below to get to know how these work sequentially.
The first two statements, after the statement that establishes a connection, are the ones that create a prepared SQL statement. A prepared statement is a special kind of SQL statement used to ensure the sanitization of the raw inputs, making it beneficial for preventing SQL injection attacks. From the sql variable's value, specifically a SQL string, those question marks are the placeholders used for replacing them with the respective values. The SQL string basically contains the INSERT query, and the data insertion is aimed to the users table. Then, assigns it to the instance variable prep to set a value for it.
The next two statements are the ones that put particular values on the placeholders. The first one replaces the first question mark in the query string, then the second one does the same, but at the second question mark. The setString method has two parameters: first is the position, second is the value. If the position is 1, then the provided value will be set at the first column. The position parameter corresponds with the numerical order of the columns, starting from 1.
The statement prep.executeUpdate(); does the actual execution of the prepared SQL statement. Without it, nothing is executed.
The last statement just shows the message dialog that the data is successfully inserted.
Run the application, enter values in the respective text fields, and press the button. Once pressed, the data provided in the text fields should be inserted into the database successfully as expected. If an error occurs, you may try to fix it or seek for help.
To check if the data is truly inserted, proceed to the main page of APEX (from the browser), then click SQL Workshop. If not logged in, log in with the workspace credentials. Once navigated to SQL Workshop, click SQL Commands to navigate to the area where you can run SQL queries or commands.
Insert the following query from the photo, then click Run to execute the query.
After query execution, the results are obtained by very less than a second, but the execution time varies depending on the host device performance. As displayed from the results, it indicates that we've actually inserted the data successfully.
And voilà! You've created your Java application that has the database integrated in it, where you can manage dynamic data for its extended functionality.