You are on page 1of 9

NetBeans 6.x.x and MySQL 5.x.

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

The machine specifications used in this task are:
 Intel Pentium Core 2 Duo, 2.2 GHz,
 Windows XP Pro SP2 + periodical patches + periodical updates
 2 GB DDR2 RAM
 160 GB SATA HDD
 17” SyncMaster 713N monitor.
 Pre requirement: NetBeans 6.x.x and MySQL Database 5.x.x

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.

CREATE DATABASE Mycompany;

jdbc:mysql://localhost:3306/mycompany . Launch NetBeans.Making a Connection For the next steps. we are using the NetBeans. Use the following connection string and key in the username as root and its password. Click Services tab > expand Database node > expand Drivers node > select MySQL (Connector/J driver) > right click mouse > select Connect Using. Click OK.

. it will be confirmed as shown in the following Figure.If the connection to the database was established successfully. Click OK.

expand the connection node as shown below. Views and Procedures folders. Creating a Table Next. execute the following SQL script to create a table. You can see the Tables.Next. These are common database objects that act as containers for other database objects that will be created later. Select the database connection node > right click mouse > select Execute Command to launch SQL query editor. .

empl_start_date DATE NOT NULL. The following table summarized the table that we want to create. empl_addr_state VARCHAR(2) NOT NULL. empl_dept_id VARCHAR(10) NOT NULL. empl_position VARCHAR(5) NOT NULL. The structure for a Employees table Column name Data type Constraints userID INT UNSIGNED NOT NULL PRIMARY KEY name CHAR(10) NOT NULL UNIQUE INDEX lastName VARCHAR(30) - firstName VARCHAR(30) - office CHAR(2) NOT NULL CREATE TABLE IF NOT EXISTS Employees( empl_id INT. empl_last_name VARCHAR(32) NOT NULL. empl_first_name VARCHAR(32) NOT NULL. empl_addr_street VARCHAR(32) NOT NULL. paste and execute the following SQL script. empl_addr_zip VARCHAR(10) NOT NULL. PRIMARY KEY (empl_id) . empl_birth_date DATE NOT NULL. Copy. empl_addr_city VARCHAR(32) NOT NULL. You can create the table shown in the Table using the following SQL script.

Next. The following Figure shows the SQL script in the SQL query editor/SQL Command editor. . 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. use double quotes. MySQL is not case sensitive. Make sure the connection to the correct database as shown in the following Connection: text field. Next. If you want to retain the case sensitiveness. expand the Tables > employees. ) ENGINE=innodb.

DESC Employees. a foreign key reference to a row in the Departments table which will be used for the department in which the employee works  empl_start_date: The employee's start date with the company  empl_position: The identifier. and empl_addr_state: The employee's complete address  empl_dept_id: The identifier.The Employees table contains the following columns:  empl_id: The employee identifier number. . which uniquely identifies each employee and is the primary key for these records  empl_first_name and empl_last_name: The employee's first and last names  empl_addr_street. a foreign key reference to a Positions table record which will be used for the employee's current job or position  empl_birth_date: The employee's date of birth Verify our task by issuing the following SQL statement (describe a table). empl_addr_zip. empl_addr_city.

854.483. Same as CHAR. Level 3'.372.147.'Jodie'. SQL Fixed-length character type that holds exactly n characters. The following Tables list MySQL numerical and character data types information.223.'32000'.608 to 8.'2008-07-04'. except for Unicode strings.854. Any excess characters are discarded. .807. Decimal values with s as the scale and p as the precision.372. Whole 8-bit numbers in the range -32758 to 32. Same as VARCHAR. except for Unicode strings. n MySQL Whole 7-bit numbers in the range -128 to 127.147. Whole 16-bit numbers in the range -8.547.036. Inserting a Sample Data Next. Whole 64-bit numbers in the range -9.388.223. A complete information can be found in MySQL documentation and you can also download it for offline reading.'1970-08-05'). INSERT INTO Employees VALUES( '1234'.'45th Street.'Foster'.'Dir'.757.775.775. Whole 32-bit numbers in the range -2. let insert a record by running the following SQL statement.036.'New York'. Shorter strings are padded with spaces to n characters.548 to 2.607. Double-precision values with s as the scale and p as the precision. Floating point numbers with a precision of 8 or less. 'NY'.'IT'.388.483. Variable-length strings that may store up to n characters.808 to 9.

verify our task by issuing the following SQL statement.Then. . SELECT * FROM Employees.