Java MySQL JDBC Tutorial using NetBeans (Part 1)
June 14, 2009 at 3:05 pm | In Java, Programming | 12 CommentsTags: java, jdbc, mysql
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 you’ll be on this frame

javaapps
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. 
Then choose MySQL JDBC Driver

Then Click Add Libary.
So now we can connect to MySQL database. Here is the example how you can connect to MySQL.
Here, we use interface Connection.
package javasql;
import com.mysql.jdbc.Driver;
import java.sql.*;
/**
*
* @author Ferdiansyah Dolot
*/
public class Connect {
public Connect() throws SQLException{
makeConnection();
}
private Connection koneksi;
public Connection makeConnection() throws SQLException {
if (koneksi == null) {
new Driver();
// buat koneksi
koneksi = DriverManager.getConnection(
"jdbc:mysql://localhost/databasename",
"username",
"password");
}
return koneksi;
}
public static void main(String args[]) {
try {
Connect c = new Connect();
System.out.println("Connection established");
}
catch (SQLException e) {
e.printStackTrace();
System.err.println("Connection Failure");
}
}
}
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 is databasename.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) (I’ll write it soon). ^_^.
Thanks for reading.
12 Comments »
RSS feed for comments on this post. TrackBack URI
Leave a comment
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.


mantap lot!
nanti gw submit ke regional yah linknya..
kalo bisa pake bahasa indo next time, biar bantu orang2 indo
Comment by deni — June 14, 2009 #
^_^ thx
ok bos, jangan lupa oleh2 ya
Comment by Ferdiansyah Dolot — June 14, 2009 #
Wah, bung ferdi, keren dah blognya,
sana ada pertanyaan nih,
berhubung saya pernahnya pake C# buat aplikasi, saya cuma dengar dari orang lain klo java tuh koneksi ke databasenya repot, coba bung ferdi kasi pembahasannya sedikit,
thx ya
,
Comment by Sely Haudy — June 16, 2009 #
oo paling kalo koneksi ke database dari java memerlukan jdbc driver . . .kalo di netbeans tinggal di add aja itu
dicoba dulu aja bung baru bilang repot . . XD
~bung sely kan jagooo . . XD
~cewenya banyak lagi . .XD
Comment by Ferdiansyah Dolot — June 16, 2009 #
Hey its really Helpful to me
Thanks alot
Comment by nikita — July 15, 2009 #
welcome
Comment by Ferdiansyah Dolot — August 9, 2009 #
Thanks for this tutorial, a lot of tutorials on the web are really lengthly and confusing. I was looking for how to connect MySQL to Java and without this tutorial, I might still be searching. Keep up the good work.
Comment by Pleased student — August 8, 2009 #
you’re welcome . . .
thanks for reading . .:)
Comment by Ferdiansyah Dolot — August 9, 2009 #
this is perfect.. for those who are having problems such as:
java.sql.sqlexception no suitable driver not found
||
java.lang.classnotfoundexception com.mysql.jdbc.Driver.
this might work on your application too.
Thanks for this post.
You are awesome..
Comment by ahmmy — August 9, 2009 #
Thanks.
Glad I could help.
Comment by Ferdiansyah Dolot — August 9, 2009 #
Hi everyone !!!
Plz i don’t know if my post is in accordance with your policy…for any problem please i present all my desolations.
I’m a bsc student in CIT . I have a project i which i want to use java applet and jdbc in a sort of host/client :
My applet is working properly in the host but when i deploy it on apache webserver i lose the jdbc connection.
Questions:
1-Is it possible to use applet with jdbc ?
2-If yes how to do it ?
3-Is there any web server in which to deploy applet and that support jdbc connection ?
my system : fedora 8 , netbeans 6.5 , xampp, mysql .
Comment by soulemane — August 9, 2009 #
hope this could help :
JavaApplet Tutorial
Thanks.
Comment by Ferdiansyah Dolot — August 10, 2009 #