A Database table using javax.swing and java.awt where you can Add,Remove,Search for Available data based on ID and Sort
- App class
- Entity class
- Table Model class
- Frame class
- Service class
- How to use for you database table
Here is where we use the information for the database to connect to the Service, and also initialize a new Frame with a TableModel.
We hold the information about the rows of an individual database entry.Currently, it's a student data,and every student has an id, first name,last name and department.
This is where the Table with the students is made. Every student is saved in a List of Students.There are methods for adding a student("save" method),editing,deleting,finding based on id and getting the number of students.
The information for the JFrame.We use the TableModel,the DataService, and some buttons,labels,comboBox.
In initComponents() we add the components and make the layout.In initPagination(),we decide which elements are printed to the table and how they are sorted.
We get the entries depending on the button,if possible
This happens in the jButtonAddActionPerformed() method.You have to write the ID first,then first name,last name,and finally the department the student is studying in.This is done using an InputDialog.
You have to write a valid ID,to continue to the next information. A valid ID needs to consist only of numbers,be exactly 6 symbols long and not to be used already. First we search if the ID has any character,not being a number. If it does,we get this message!
If the ID is not exactly 6 symbols:
If the ID is 6 symbols consisting only by numbers,we then look if it already exists in the database,If it does:
Else it's considered valid, and you can continue to write the First Name and then Last Name. They are valid if they are not empty(there are more than 0 characters).Same can be said for the department.
If everything is valid the new student is added to the database and to the table, and you get this message:
You can also cancel adding a new entry whenever you want.
jButtonSearchActionPerformed() is used for that.I use the boolean variable refresh to get only the student with this ID.
If the ID doesn't exist:
In jButtonSortActionPerformed().You get the options to sort by ID,First Name,Last Name or Department.It depends on the value of the input and if the value is not equal to either 0,1,2 or 3 ,nothing happens.The ordering happens in initPagination() (or the call for the order), in this method only the boolean variables are changed.
Using the jButtonRemoveActionPerformed() method.The students are removed by given ID value,because it's unique.
If the ID doesn't exist:
If the student is removed successfully:
It simply makes the boolean variable refresh to false which allows to show all the entries.
Used for more dynamic resizing of the different columns.
This is where we make connection with the database if possible and use it. In every method,first we try to make a connection and when successful ,the intended operation is done,otherwise we get an information about the error. SQL queries are used to manipulate the Database.
In the save(Student student) method we add a new entry to the database,which is executed after we use valid characters for the ID,Names and Department in the Add button.
findall(page,pageSize) is used to get all the students from the database.
find(id) is almost the same,but It's used to get only the student with the given ID. Exists(id) is also simular, but it returns a boolean value(if a student with this ID exists,it returns true ,otherwise its false).
Returns the number of entries in the database.
countOne(id) returns only 1 or 0(if a student with id exists ,its 1 otherwise it returns 0).
Used to order the data based on either ID,First or Last Name ,or Department.
Order by ID:
Delete a student with this ID from the Database - remove(id)) .
-
Change the variables based on your columns in your database table, with the right data types.Also you can modify the getter/setter methods.
-
Change the information in toString() method
-
in jButtonAddActionPerformed() you can keep the ID validator, if you want, but it only accepts exactly 6 digits ,if you want to fix that modify the len variable
-
In jButtonSortActionPerformed() :
Then set them to true, and all other values to false, based on the sort text you wrote from one picture above.You may need to add more boolean variables or cases in the switch ,if you have more sort options.
-
In initPagination() you need to add your sort variables, and also your OrderByColumn() method located in the Service class like this(see the OrderByColumn explanation in In Service class header).
-
In the queries use your Table name and the name of the columns you use. If your table is cars, change students to cars , or if the id is car_id, change the Student_ID to car_id.
-
Also in the ResultSet use your column names and the right types.
-
In the OrderByColumn (where Column is the column you want to order by) just change the table to your table and the column you want to use to order
- In the HEADER variable, add the column names you want.
- In the getValueAt() add the getters of your columns, if you want.
Using other database table: