Vous êtes sur la page 1sur 8

Accessing MySQL on NetBeans using JDBC, Part I: Create

a connection
Java, NetBeans

Introduction

This tutorial show you how to use NetBeans to connect MySQL by using MySQL Connector/J,
MySQL AB’s JDBC Driver for MySQL.

I’ll divide into 2 parts:

1. Part I : Create a connection


This part which you’re reading shows about how to establish a connection between
NetBeans and MySQL.
2. Part II : Perform SQL Operations
This part show how to perform some basic operations

from NetBeans to MySQL. For instance, send querys as SELECT, INSERT, UPDATE to
a database.

Requirements

 MySQL Connector/J, licensed under the GPL or a commercial license


from MySQL AB.
 NetBeans with JRE (Java Runtime Environment).

Step-by-Step guide

1. Installation
o Install NetBeans.
o Download MySQL Connector/J, name ‘mysql-connector-java-5.0.6.zip’. (The
file name may differs depends on the version if you’ve downloaded from the
Official Site at here.)
o Extract the zip file to a folder, you’ll see file ‘mysql-connector-java-5.0.6-bin.jar’
which is the library file that we want. Just copy the file to the library folder, for
example to “C:\Program Files\Java\jdk1.6.0_02\lib” directory.

2. Add JDBC Driver to the project on NetBeans (Add a library).


Next, I create a new Java project on NetBeans named ‘TestMySQL’ and add ‘mysql-
connector-java-5.0.6-bin.jar’ that I’ve just extracted from previous step to the project’s
library.
1. Create New Project called TestSQL.
2. In Projects window, right click the project name and select Properties.
3. Project Properties window appears. The Categories on left side, select Libraries.
And on right side in Compile tab, click Add JAR/Folder.

4. New Window appears, browse to the file ‘mysql-connector-java-5.0.6-bin.jar’ and


click Open.
5. You’ll see the .jar file was added to the project. Click OK to finish.

Note: You should keep mysql-connector-java-5.0.6-bin.jar in the directory that


you won’t delete it (ex. not in temp folder). May be in the same directory that
keep common library files. If you delete the file without delete a link from the
project, the project will show error about missing library.
. Connect to the database.
Now I’m going to write some code to connect to MySQL database. I have configured
MySQL service on localhost.
1. I’m going to use Connection and DriverMapper Classes so I need to import
libraries.
import java.sql.*;

2. I’ll connect to MySQL Server on local machine, the mysql database(a default
database in MySQL). In main method, add the following code.
3. try {
4. Class.forName("com.mysql.jdbc.Driver");
5. String connectionUrl = "jdbc:mysql://localhost/mysql?"
+
6. "user=root&password=123456";
7. Connection con =
DriverManager.getConnection(connectionUrl);
8. } catch (SQLException e) {
9. System.out.println("SQL Exception: "+ e.toString());
10. } catch (ClassNotFoundException cE) {
11. System.out.println("Class Not Found Exception: "+
cE.toString());
}

The code explanation:

 Class.forName(“com.mysql.jdbc.Driver”); means load the MySQL driver.


 “jdbc:mysql://localhost/mysql?” + “user=root&password=123456″; is a
connection string that tells to connect MySQL on localhost, select
database named ‘mysql’ and user/password for MySQL server.
If you would like to connecto to other database, simply change text
‘mysql’ after ‘localhost/’ to your database name.
12. Compile and run the project. If no error occurs, it means that the connection has
established successfully.

Vous aimerez peut-être aussi