MYSQL OPERATIONS

by David de Almeida Bezerra Júnior


MySQL is the default TetrisIDE's database manager, bringing to us a simple and fast integration to your applications. The Java development became easy with this RAD solution!

We will create a simple customer registration this time.

TetrisIDE provides to you a fast way to make database windows, but we will make from the hard way.

If you do not know the SQL (Structured Query Language) syntax, take a look here.


SQL on TetrisIDE

Usually, the select operation in SQL is formed by clause select, columns_to_select, clause from, table_to_select, clause where, condition, clause order by and columns_to_order.
Ex.:
select id, name from customer where name='David' order by id;

TetrisIDE brings to us functions that receives the parameters to execute database operations. You do not have to make the connection, or load a resultset and execute a statement. TetrisIDE makes it for you.

Create a project named MySQLOperations and change the property Title, width and height to MySQL Operations, 500 and 400, respectively from JFrameMain.



Adding objects



Add 2 Labels, 2 TextFields, 3 Buttons and 1 Table from Object Palette.

Change the property Text of the jLabel1 and jLabel2 to Id and Name, respectively and change the names of jTextField1 and jTextField2 to jTextFieldId and jTextFieldName and erase the property Text. Change the property Text of the jButton1, jButton2 and jButton3 to Insert, Update and Delete and the names to jButtonInsert, jButtonUpdate and jButtonDelete. Change the name of the jTable1 to jTableCustomer.


Creating the database table

Press the Add table button in the MySQL Database Manager and add a table named customer.



Note the manager already created the column id in the table. We will use it.



Now, press Add column button in the MySQL Database Manager and add a column named name of the type varchar(50).



The creation and changes on database will persist when we run the application.


Adding Events

Select the jButtonInsert and perform double click on OnClick event in the Events tab of the Object Inspector.

Add the function insertRecord with the Parameters:
customer
id, name
'"+jTextFieldId.getText()+"', '"+jTextFieldName.getText()+"'



Look at the tip below the Parameters field. This explains every parameter inserted.

Add the function changeValue with the Parameters:
jTextFieldId /*Object to change the property*/
Text /*Property of the object to change*/
  /*Value*/

Repeat the procedure to jTextFieldName and press OK.

Select the jButtonUpdate and perform double click on OnClick event in the Events tab of the Object Inspector.

Add the function alterRecord with the Parameters:
customer /*Database table*/
name='"+jTextFieldName.getText()+"' /*Fields to change*/
where id='"+jTextFieldId.getText()+"' /*Condition*/


Select the jButtonDelete and perform double click on OnClick event in the Events tab of the Object Inspector.

Add the function deleteRecord with the Parameters:
customer /*Database table*/
where id='"+jTableCustomer.getSelectedValue("Id")+"' /*Condition*/


Filling Table

Select the jTableCustomer and change the property Columns to:
id
name


Do the same with property Titles.

Add a Procedure from the Object Palette and change the name to fillTable.



Perform double click on OnExecute event in the Events tab and add the function fillTable with the Parameters:
jTableCustomer /*Object jTable on window*/
customer /*Database table*/
id, name /*columns*/
order by id /*Condition/Order*/


Enter in the OnClick event of each button and add the function runProcedure with the parameter:
fillTable /*Object Procedure*/

Do the same with the OnShow event of the window.



Save and Run.



First time running, your application will require the connection data. If you installed MySQL on your machine, fill in the fields correctly.
SERVER: localhost
DATABASE: MySQLOperations
USER: root
PASSWORD: YOUR_ROOT_PASSWORD

Run it again and test.





CopyRight © 2023 Analisa Software.All Rights Reserved.