x Database
Contents:
1. Creating a Database Using MySQL
2. Making a Connection to MySQL Database
3. Creating a Table
4. Inserting a Sample Data
5. Related references
Before we proceed to the next tutorial, upgrading our skill level, we would like to recap
the NetBeans and MySQL database related thingy. We start learning to develop Java
GUI application and then make a connection to a database using Java DB (Derby).
Then we will try to change the database to MySQL while retaining other aspects of the
Java GUI application. During the process in completing the Java GUI application, we
have learned a lot of things regarding the Java technologies and at the same time got
familiar with NetBeans 6.x.x. We will try to developed Java application up to the web
applications while experiencing all the related Java technologies. Stay tune!
Creating a Database
Firstly let create a new database by using the following SQL statement through
MySQL Command Line Client Tool. In this exercise, our database name will
beMycompany. Take note that MySQL is not case sensitive. If you want to retain the
case sensitiveness, try using the double quotes. We found that using the double
quotes also not working all the times.
For the next steps, we are using the NetBeans. Launch NetBeans. Click Services tab >
expand Database node > expand Drivers node > select MySQL (Connector/J driver) >
right click mouse > select Connect Using.
Use the following connection string and key in the username as root and its password.
Click OK.
jdbc:mysql://localhost:3306/mycompany
If the connection to the database was established successfully, it will be confirmed as
shown in the following Figure. Click OK.
Next, expand the connection node as shown below. You can see the Tables, Views and
Procedures folders. These are common database objects that act as containers for
other database objects that will be created later.
Creating a Table
Next, execute the following SQL script to create a table. Select the database connection
node > right click mouse > select Execute Command to launch SQL query editor.
The following table summarized the table that we want to create. You can create the
table shown in the Table using the following SQL script. Copy, paste and execute the
following SQL script.
The following Figure shows the SQL script in the SQL query editor/SQL Command
editor.
Make sure the connection to the correct database as shown in the following Connection:
text field.
Next, click the Run SQL button ( ) or Ctrl+Shift+E to execute the SQL script and
notice the progress in the Output window at the bottom.
Next, expand the Tables > employees. MySQL is not case sensitive. If you want to
retain the case sensitiveness, use double quotes.
The Employees table contains the following columns:
Verify our task by issuing the following SQL statement (describe a table).
DESC Employees;
The following Tables list MySQL numerical and character data types information. A
complete information can be found in MySQL documentation and you can
also download it for offline reading.
n MySQL
Whole 7-bit numbers in the range -128 to 127.
Whole 8-bit numbers in the range -32758 to 32,757.
Whole 16-bit numbers in the range -8,388,608 to 8,388,607.
Whole 32-bit numbers in the range -2,147,483,548 to 2,147,483,547.
Whole 64-bit numbers in the range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
Decimal values with s as the scale and p as the precision.
Double-precision values with s as the scale and p as the precision.
Floating point numbers with a precision of 8 or less.
SQL
Fixed-length character type that holds exactly n characters. Shorter strings are padded with spaces to n characters.
Same as CHAR, except for Unicode strings.
Variable-length strings that may store up to n characters. Any excess characters are discarded.
Same as VARCHAR, except for Unicode strings.