Java MySQL JDBC Tutorial Using NetBeans
Java MySQL JDBC Tutorial Using NetBeans
NetBeans (Part 1)
Are you asking ‘Could I connect Java application/applet to database like MySQL?’ and
the answer is definitely Yes.
You can use JDBC (Java Database Connectivity) to connect your Java application/applet
with database. So Let’s start.
First, Create new Project named anything you want, for example Javasql by click File-
>New Project.
newProject
then click next, then give Project Name and set Project Localtion
nameProject
then finish.
Second, you must have JDBC MySQL Driver before you can start to connect your Java
program to database. But since we use Netbeans , this has been done. Just simply add
the library to your project library. Right click in Library on the Project you
use.
So now we can connect to MySQL database. Here is the example how you can connect
to MySQL.
import com.mysql.jdbc.Driver;
import java.sql.*;
/**
*/
makeConnection();
if (koneksi == null) {
new Driver();
// buat koneksi
koneksi = DriverManager.getConnection(
"jdbc:mysql://localhost/databasename",
"username",
"password");
return koneksi;
try {
System.out.println("Connectionblished");
catch (SQLException e) {
e.printStackTrace();
System.err.println("Connectionure");
In example above we create connection to MySQL from creating object of Driver and
get connection from DriverManager (get Connection will return Connection Object
type), with parameter the url, username, and password. The url above means the
database is located in localhost and the name isdatabasename.You can also add port
for MySQL so the url looks : “jdbc:mysql://localhost:3306/databasename”. (See port
3306).
So now we can make a connection to MySQL database. Now, how can we do SQL
statement like update, delete, insert, etc?
We will discuss this in Java MySQL JDBC Tutorial using NetBeans (Part 2).
Make sure that you have read the previous part of the tutorial. In this tutorial, we
will use Project that we have created in previous tutorial and Statement interface
from java.sql.Statement.
First we must have a database and a table in MySQL. For example, we create a
database school with attribute name and number (student number). Start your MySQL
and make such database and table using this command :
In Statement interface, we can execute any SQL statemnt using our Java program.
First, make a class named Sqlstatement in our previous tutorial Project. The content
of Sqlstatement.java is :
package javasql;
import java.sql.*;
/**
* @author ferdiansyah.dolot
*/
makeStatement();
statement = conn.createStatement();
return statement;
"+number+")");
s.insert("Ferdi",1);
s.insert("Anca",2);
System.out.println("Success }
catch(SQLException e){
System.out.println("Failed e.printStackTrace();
On this part :
statement = conn.createStatement();
You can see the value you insert using Sqlstatment class will be in table values.