NetBeans JDBC MySQL Tutorial

Written by corey bieber
  • Share
  • Tweet
  • Share
  • Pin
  • Email
NetBeans JDBC MySQL Tutorial
Connecting applications to MySQL through JDBC allows for robust data persistence. (Hemera Technologies/AbleStock.com/Getty Images)

Java Database Connectivity (JDBC) enables software developers to connect Java applications with MySQL, an open source database server which allows you to add data persistence to your applications. To connect the two technologies, you will need to add the Connector/J driver to your application class path. In NetBeans, this is done through the "Project Properties" window.

Skill level:
Challenging

Other People Are Reading

Instructions

    Acquiring the Latest Version of Connector/J

  1. 1
  2. 2

    Select the most recent version of Connector/J and click "Download." Notice the package is available as a platform independent tar.gz and zip.

  3. 3

    Unzip the compressed file to the location in which you store your Java libraries.

    Adding the JDBC Java Archive (JAR) to Your Applications Build Path

  1. 1

    Open NetBeans. From the Project pane, right-click the project you intend to connect to MySQL.

  2. 2

    Select "Properties" at the bottom of the pop-up window. The "Project Properties" window will appear.

  3. 3

    Select "Libraries" from the list on the left hand side of the "Project Properties" window.

  4. 4

    Select the "Run" tab from the available options. On the far, right-hand side of the screen, select "Add JAR/Folder."

  5. 5

    Select the Connector/J JAR file you unzipped earlier. Leave all default selections as you found them.

  6. 6

    Click "Open," then click "OK" in the "Project Properties" window. The driver has now been added to your class path. You will likely need to build the project prior to using JDBC.

    Writing a Class to Access MySQL Through JDBC

  1. 1

    Create a new Java class "DBMySqlImpl " to test the connection between your application and MySQL.

  2. 2

    Add the following code to your new class:

    public class DBMySqlImpl implements DBFacade {

       private Connection connection;
    
    
    
    public void connect(String URL, String uname, String psswd) throws DBException{
    
       try{
    
       Class.forName("com.mysql.jdbc.Driver");
    
       connection = DriverManager.getConnection(URL, uname, psswd);
    
       }
    
       catch(ClassNotFoundException e){
    
    
    
           throw new DBException("DBException encountered", e);
    
       }
    
       catch( SQLException s ){
    
    
    
           throw new DBException("DBException encountered", s);
    
    
    
       }
    
    }
    

    public void disconnect() throws DBException{

       try{
    
       connection.close();
    
       }
    
       catch(SQLException e){
    
           throw new DBException("DBException encountered", e);
    
       }
    
    
    
    }
    

    public void initialise() throws DBException{

       try{
    
           Statement s = connection.createStatement();
    
    
    
    
    
          s.executeUpdate("Drop database if exists testDB");
    
           s.executeUpdate("Create database testDB");
    
           s.close();
    
       }
    
    
    
       catch (SQLException e){
    
           throw new DBException( "DBException Encountered", e );
    
       }
    
     }
    
  3. 3

    Create a new class to implement the "DBMySqlImpl " class by calling the connect, initialise and disconnect methods. The code will create a new MySQL database called "testDB."

Tips and warnings

  • Alternatively, some versions of NetBeans come standard with the MySQL JDBC driver. Check if this is the case with your installation by selecting "Add Library" from the "Run" tab. Under "Global Libraries" look for "MySQL JDBC Driver." If it is present, select the driver and click "Add Library."

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.