NetBeans-SCSNI

1.5 Demonstrate knowledge of working with databases in the IDE, including registering new database connections and tables running SQL scripts.

          database connection
      A database that you have defined in the IDE to be referenced by your application.
You can add database connections in the Services window.

Connecting to a Database
To connect to a database you first need to do the following:
Confirm that a JDBC driver for your database is registered with the IDE. The registered JDBC drivers are visible under the Drivers node in the Services window.
Create a database connection for the database. A database connection is represented by a database connection node (   ) under the Databases node in the Services window.
When you create a database connection, you supply the details needed to connect to a specific database. The details needed to connect to a database include the location of the database, which driver to use and the username and password information.
After you create the database connection you can do the following:
Disconnect and reconnect to the database
Browse the database structure
View data contained in a table in the database

To create a new connection:
Confirm that your database is running.
In the Services window, confirm that the appropriate JDBC driver for your database is listed under the Drivers node.
   Right-click the Databases node and choose New Connection.
Alternatively, expand the Drivers node, right-click your driver's instance node and choose Connect Using.
   In the New Database Connection dialog box, confirm that the correct driver is selected in the Driver field.
Provide the JDBC database URL for the database connection in the Database URL field.
   This URL typically begins with jdbc:and a short code name for the driver, followed by another colon. The rest of the URL depends on the type of driver you are using. For example, you might need to specify a host and port; alternatively, the name of a database file might suffice, or an ODBC DSN. For MySQL, the Database URL might look as follows:
jdbc:mysql://localhost:3306/mydatabase

To help you get started, the IDE provides templates for the correct URL format for the most common drivers. If in doubt, refer to the documentation for the driver that you are using.

Provide the user name and password, if required by your database configuration.

Click the Remember Password checkbox to have the IDE remember your password.
If selected, you will not be prompted for the user name and password on subsequent connections to the database. To change this property, right-click the database connection node in the Services window and choose Properties and unselect the Remember Password property in the Properties window.

Click OK to create the database connection and connect to the database from the IDE.
When connecting to the database, the schema with the same name as the user name is set as default, but if a schema with that name does not exist, you are automatically switched to the Advanced Tab of the Connect dialog box to select a schema.

If the connection is successful, the database connection node appears as a subnode of the Databases node. The database connection node appears whole (   ) indicating the database is connected. When the database is connected, you can expand the database connection node to see the child folders representing lists of tables, views and procedures. For more, see Browsing Structures.

If the connection attempt fails, the IDE displays a dialog box indicating it was unable to add the connection. Check the connection information for the database in the New Database Connection dialog box.
 
To disconnect from the database:
Right-click the database connection node and choose Disconnect.
You cannot browse the database until you reconnect to the database.

Executing SQL Statements and Scripts

You can use the SQL Editor to write and execute SQL statements and SQL scripts within the IDE. The SQL statement or script is executed on the database that is selected in the Connection drop-down menu in the toolbar. If the database connection is closed, the IDE opens the connection to the database.
You can use the SQL Editor to create, edit and execute any SQL files in your project. To open an SQL file, right-click the SQL file (    ) in the Projects or Files window and choose Open. You can also double-click the icon to open the file in the SQL Editor.
To create and execute a SQL statement or script:
Expand the Databases node in the Services window.
Right-click the node for the connected database and choose Execute Command from the pop-up menu to open the SQL Editor.
Make sure your current database connection is selected in the Connection drop-down menu. To see the status of the listed connections, click the Select Connection in Explorer button in the toolbar. When you click Select Connection in Explorer, the selected connection is highlighted in the Services window.

Enter a (DDL or DML) statement in the SQL Editor window.

Click Run SQL (    ) in the toolbar or right-click in the SQL Editor and choose Run Statement from the pop-up menu.
When you execute an SQL statement or script, you see the following:

The status of the SQL execution is displayed in the Output window. Any errors when executing the SQL statement or script are displayed in the Output window.

The output of the statement appears in the results pane in the lower half of the SQL Editor. If the statement has no output, the number of affected rows is displayed in the Output window of the IDE.

Notes:
You can run selected text in the SQL Editor by right-clicking the selected text and choosing Run Selection from the pop-up menu.
You can access the Execute Command pop-up menu item from any subnode of a connected database (   ) in the Services window.
To view the data in a table, right-click a table in the Services window and choose View Data from the pop-up menu. For more, see Viewing Data.