Connect Netbeans With MySQL
Connect Netbeans With MySQL
• 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::
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.
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.
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.