How To Connect MySQL Database In Netbeans

How To Connect MySQL Database In Netbeans


Hello friends, the discussion this time is about how to make MySQL database connection using IDE NetBeans. Okay, before we need to set up the database we will connect. For example I have created a database named "dbtoko" as the image below

If the database is ready, now open Netbeans then create a new project by clicking on File menu and select New Project or can use keyboard shortcut Ctrl + Shift + N.
Then click Next and enter your project name and specify a storage location, then click Finish.

Now right click Source Package → New → Java Package.

Then name it as you see fit.

The purpose of the steps above is to create a new folder into a special folder to put the database connection file later. But this is only Optional.

The next step is to right click on the package you created and select Java Class and name it as you wish, for example I give the name ClassDB.

If it is done, now we need to enter the library named MySQL JDBC Driver as a link to our database.
By right-clicking on the package library, then select Add Libraries. Then locate the driver
Driver MySQL JDBC, then click Add Library.


Now enter the following code in ClassDB.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class ClassDB {
    private static Connection koneksi;
    public static Connection getkoneksi() {
        if (koneksi==null) {  
          try {
           String url = new String();
           String user = new String();
           String password = new String();
           url = "jdbc:mysql://localhost:3306/dbtoko";
           user = "root";
           password = "root";
           DriverManager.registerDriver(new com.mysql.jdbc.Driver());
           koneksi = DriverManager.getConnection(url,user,password);
           JOptionPane.showMessageDialog(null,"Connection Successfuly");
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Connection Failed" +e);
        return koneksi;

If the above code is complete and there is no error, we move the main class ie
ConnectionMySQL, then we call the function we have created earlier with the code


then run the project, if successful it will show a successful connection message.

For more details, please watch the tutorial video below


  1. offers high quality DMCA Ignored Hosting. We make offshore hosting simple for you to use with reliable servers and one-click installers.

  2. The database executive will acquire crafted by the information draftsman assuming that one is utilized to plan a database, or a database expansion, as a feature of an undertaking. placement cells database

  3. The most effective way to do this is to guarantee that each goes to the next's plan meetings. Your database engineer can plan an effective database in the event that they know how it will be utilized by the framework and the framework's clients.

  4. There is little distinction between the ranges of abilities moved by information draftsmen and those moved by database overseers.

  5. In such a flooded market with so many different hosting companies and packages out there, how do you know who to trust with your site? You want a reliable company that will make sure your site is always available, you want a support department that actually exists and can help you quickly, but of course it needs to be affordable and fit into your budget, especially if your web site isn't intended to generate any income.


Powered by Blogger.