Rifky Apriadi

March 25, 2008

koneksi database MySQL

Filed under: Java

sebelumnya gw post koneksi dalam PHP, sekarang koneksi menggunakan java. ya, mudah2an aja ada yg bisa memanfaatkan ini!

koneksi.java

import java.sql.*;
import javax.swing.JOptionPane;

public class koneksi
{ public Connection conn;

public koneksi() {}

public Connection openKoneksi()
{
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost/coba?user=root&password=");
            return conn;
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            return null;
        }
        catch(ClassNotFoundException et)
        {
            et.printStackTrace();
            return null;
        }
    }
    public void tutupKoneksi() throws SQLException
    {
    try{
        if(conn!=null)
        System.out.print("Connection Close");

    }
    catch(Exception ex){
        ex.printStackTrace();
    }

    }
}

itu d buat dengan nama kelas koneksi.java, nama database coba, username root dan passwordnya kosong!! dan jangan lupa, kalau untuk java kita harus sertakan pula mysql-connectornya!

ok, mudah2an bermanfaat!! amin…. emoticon 

Comments »

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.

Get free blog up and running in minutes with Blogsome
Theme designed by Ian Main