Java MySQL JDBC Tutorial using NetBeans (Part 2)

In first part, we have already established connection to MySQL using Java. Now, we will discuss about how to modify the record of the database from program we make.

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 :

create database school;
use school;
create table student (name varchar(30), number int);

Set student number to be the primary key on this table:

alter table student add constraint primary key (number);

After finishing with database, we move to our Java program.

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
 */
public class Sqlstatement {
    private Statement statement;
    public Sqlstatement() throws SQLException{
        makeStatement();
    }
    public Statement makeStatement() throws SQLException{
        Connect c = new Connect();
        Connection conn = c.makeConnection();
        statement = conn.createStatement();
        return statement;
    }
    public void insert(String nama,int npm)throws SQLException{
        statement.execute("insert into student values(\""+name+"\",
                          "+number+")");
    }
    public static void main(String arg[]){
        try {
            Sqlstatement s = new Sqlstatement();
            s.insert("Ferdi",1);
            s.insert("Anca",2);
            System.out.println("Success");
        }
        catch(SQLException e){
            System.out.println("Failed");
            e.printStackTrace();
        }
    }
}

In class above, we use the Connect class that we have created in previous tutorial.

On this part :

statement = conn.createStatement();

Variable conn is instance of Connect class that we have made in the previous tutorial. After the connection has estabished, the conn call method createStatement(). The method returns Statement that we will use to send SQL statements to database.

To execute any SQL statement using our Java program, we use execute(String sqlstatement) in interface Statement. In our program above, the execution of the SQL statement can be seen on this part :

statement.execute(“insert into student values(\””+name+”\”,”+number+”)”);

After that, run the Sqlstatement program ( in Netbeans, press Shift+ F6). See what happened in you database record. You can see values in your database and table using command :

select * from student;

You can see the value you insert using Sqlstatment class will be in table values.

API for Statement interface in Java can be seen on http://java.sun.com/javase/6/docs/api/java/sql/Statement.html.

About these ads

9 Responses to Java MySQL JDBC Tutorial using NetBeans (Part 2)

  1. mishbah says:

    sungguh luar biasa!! dahsyatnya! bung ferdi ini memang ahli dalam CS hohoho

  2. Pingback: JavaApplet MySQL JDBC Tutorial Using Netbeans « Ferdiansyah Dolot

  3. Ringo says:

    wah, mantap nih tutorialnya..btw, klo misalkan databasenya di komputer laen (baca: sbg server). Trus di kliennya mw koneksi k databasenya, gmn caranya bung dolot??

  4. Nisha says:

    Hey Thanks! this was quite helpful.
    I’m having trouble with ‘update’ and ‘delete’.
    Can u help me out?
    Thanks again

  5. Vivek Yadav says:

    This tutorial was AWESOME! Thanks and keep up the good work!! :D

  6. Mike says:

    Excellent dude. I had one problem…

    public void insert(String nama,int npm)throws SQLException{
    statement.execute(“insert into student values(\””+name+”\”,
    “+number+”)”);
    }

    1. parameter nama should be name
    2. parameter npm should be number
    3. line starting with “statement execute…”, next line “+number+…) spans 2 lines
    should be on same line

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: