0% found this document useful (0 votes)
8 views5 pages

Connect Netbeans With MySQL

This document provides instructions for connecting a MySQL database with NetBeans. It explains how to install the necessary requirements such as NetBeans, MySQL Server, and the MySQL driver. It then guides the user through the steps to configure NetBeans to recognize the MySQL driver, create a test connection, and test it. Finally, it shows how to implement the Java code to establish and use the MySQL connection in an application, creating a reusable Connection class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Connect Netbeans With MySQL

This document provides instructions for connecting a MySQL database with NetBeans. It explains how to install the necessary requirements such as NetBeans, MySQL Server, and the MySQL driver. It then guides the user through the steps to configure NetBeans to recognize the MySQL driver, create a test connection, and test it. Finally, it shows how to implement the Java code to establish and use the MySQL connection in an application, creating a reusable Connection class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Connect a MySQL database with NetBeans

Obtain what is necessary:


• Official documentation available on their website.

• Additionally, you should have NetBeans installed, preferably its latest stable version.
The moment of this publication is 5.5.1) that comes with the Java Development Kit (JDK) 1.6.

• Once you have both tools installed, you will need the driver or connector to the database.
of MySQL data.
Copy what is necessary::
Once you have NetBeans and MySQL Server installed, what you will need to do is copy the
driver that you downloaded, in my case the mysql-connector-java-3.1.11-bin.jar inside the folder of the
JDK, in my case the following path is:

C:\Program Files\Java\jdk1.6.0_01\jre\lib\ext
NOTE: You must have the connector inside the previous folder before running NetBeans.
In case it is open, close it and open it again.

Now that you have copied it, go ahead and open NetBeans and wait for it to load.

Configure NetBeans::

Once you have opened NetBeans, locate the Runtime section.


it is located on the left side of the screen, as shown below:

When expanding the Databases section, MySQL (Connector/J) should appear.


driver), otherwise you must add it manually as described below:
Right-click on Drivers and select New Driver.
2. In the window that appears, click on the Add or Add button and look for the .jar file that
you downloaded earlier, the same one you copied into the JDK folder.
3. Once you have done the above, click on the Ok button.
When you complete the previous steps, a new item should appear in the Drivers section.
within Databases named MySQL (Connector/J driver).

Create and test a connection:

Within the section of Runtime > Databases > Drivers


Right-click on MySQL (Connector/J driver) and select Connect Using.
using... to display the connection settings screen as shown in
continuation:

In the Name or Name section, select MySQL (Connector/J driver), in the section
Database URL changes the indicated part to the database server address, if it is the
same computer type localhost, in the part that sets the port you defined during installation
MySQL server, by default is 3306, in the section write the name of the database to the
Which one do you want to connect to? A complete example would be jdbc:mysql://localhost:3306/database.

Subsequently, write the username to access the database and the password.
respectively.

Click the checkbox below so that NetBeans remembers the password during the connection.

If everything was done correctly, you will see a new item below the Driver with the
current connection specifications, if you click on the '+' symbol that appears on the left side
from it you will be able to see the database tables and make queries to it.
Implement the code:

So far we have established the connection to the database and tested its functionality from
NetBeans, however, we still need to implement the code directly in an application.
For this example, we will create a class named Connection that can be reused multiple times.
it is necessary in any type of Java application that requires connecting and performing queries to a
database in MySQL.

Let's start from the beginning...


1. Click on the File menu located in the upper left corner of
the NetBeans window.
2. Select the option New Project, then in the section of
CategoriesoCategoríasseleccionaGeneraly enProjectsoProyectosseleccionaJava
Application Java and click Next.

NOTE: The class we will create works with any type of project, for the purposes of this
For example, we use a conventional Java application.
3. Afterwards we name the application and define its location, once done
Now we click on the Finish button.

4. At this moment it appears on the left side in the Projects tab


a hierarchical tree species in which we have 4 folders, when clicking on the '+' symbol of the
source folder PackagesoPaquetes Fuentenos will deploy the only package with which
Our application counts up to this point, within it the Main class is created.
5. Now we need to right-click on Source Packages or Paquetes Fuente and give a
name the package, in my case it is database. This package will contain the class from which
We will be making all the queries to the database.
6. Subsequently, we right-click on the newly created package and select
NewJava Class. Then we give it the name of Connection and click
on the Finish button.
7. Inside the classConnection we import some libraries with the following code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
8. Once we have the libraries, we create the methods to be used described below:

/**
Method used to retrieve the value of the connection attribute
@return connection contains the state of the connection
*
*/
public Connection getConnection()
{
return connection;
}
/**
Method used to establish the connection with the database
The return state returns the connection state, true if the connection was established,
false otherwise
*/
public boolean createConnection()
{
try {
Class.forName("com.mysql.jdbc.Driver");
conexion =
DriverManager.getConnection("jdbc:mysql://host:port/database","user","password"
);
} catch (SQLException ex) {
ex.printStackTrace();
return false;
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
return false;
}

return true;
}

/**
*
Method used to perform INSERT, DELETE and UPDATE instructions
string that contains the SQL statement to execute
*@return estado regresa el estado de la ejecución, true(éxito) o false(error)
*
*/
public boolean executeSQL(String sql)
{
try {
Statement statement = connection.createStatement();
statement.executeUpdate(sql);
} catch (SQLException ex) {
ex.printStackTrace();
return false;
}

return true;
}

/**
*
Method used to perform the SELECT instruction
The parameter sql is a string that contains the SQL instruction to be executed.
@return result returns the records generated by the query
*
*/
public ResultSet ejecutarSQLSelect(String sql)
{
ResultSet result;
try {
Statement statement = connection.createStatement();
result = statement.executeQuery(sql);
} catch (SQLException ex) {
ex.printStackTrace();
return null;
}

return result;
}
9. Finally, we compiled the file by locating it in the hierarchical tree on the left side.
by right-clicking on it and selecting the Compile option. You will be able to
use the class anywhere in your project by creating an instance of it in the
moment that is necessary.

TIP: Create the connection only once using the method crearConexion() from the class
I will pass your connection as a parameter to all the windows and modules of the application
that they use it.

You might also like