1. download Netbeans
2. MySQL
3. plugin connector Java
Buat project : File>New Project>Java Application>Finish
Lanjutnya :
edit class koneksi menjadi seperti ini :
Code: |
package koneksi; import java.sql.Connection; import java.sql.DriverManager; /** * * @author Alcatras */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // Kemudian Edit jadi seperti ini try{ Class.forName("com.mysql.jdbc.Driver"); }catch(Exception ex) { System.err.println("Error(1):"+ex); System.exit(1); } //bentuk koneksi Connection koneksi=null; try{ koneksi=DriverManager.getConnection("jdbc:mysql://localhost/data","root","admin"); System.out.println("Koneksi Berhasil"); }catch(Exception ex){ System.err.println("Error(2):"+ex); System.exit(1); } } } |
Kemudian Tekan Shift+F11 untuk mengkompilasi program , apakah program sudah berjalan dengan baik jika error mungkin ada kode yang salah , silakan di cek kembali.
Jika berhasil berikut outputnya :
init:
deps-clean:
Updating property file: D:\Documents and Settings\Alcatras\My Documents\NetBeansProjects\Koneksi\build\built-clean.properties
Deleting directory D:\Documents and Settings\Alcatras\My Documents\NetBeansProjects\Koneksi\build
clean:
init:
deps-jar:
Created dir: D:\Documents and Settings\Alcatras\My Documents\NetBeansProjects\Koneksi\build
Updating property file: D:\Documents and Settings\Alcatras\My Documents\NetBeansProjects\Koneksi\build\built-jar.properties
Created dir: D:\Documents and Settings\Alcatras\My Documents\NetBeansProjects\Koneksi\build\classes
Created dir: D:\Documents and Settings\Alcatras\My Documents\NetBeansProjects\Koneksi\build\empty
Compiling 1 source file to D:\Documents and Settings\Alcatras\My Documents\NetBeansProjects\Koneksi\build\classes
compile:
Created dir: D:\Documents and Settings\Alcatras\My Documents\NetBeansProjects\Koneksi\dist
Building jar: D:\Documents and Settings\Alcatras\My Documents\NetBeansProjects\Koneksi\dist\Koneksi.jar
Not copying the libraries.
To run this application from the command line without Ant, try:
java -jar "D:\Documents and Settings\Alcatras\My Documents\NetBeansProjects\Koneksi\dist\Koneksi.jar"
jar:
BUILD SUCCESSFUL (total time: 0 seconds)
Kemudian kita akan membuat databasenya dengan MySQL tapi sebelumnya kita akan menambahkan dahulu connector mysql untuk menyambungkan java dan dan database mysql :
Klik kanan pada library>add library
Kemudian pilih Driver MySql JDBC
Klik >Add Library
Buka MySQL :
Run>ketik CMD>ketik mysql -u root -p
Code: |
D:\Documents and Settings\Alcatras>mysql -u root -p Enter password: ***** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 5.1.44-community MySQL Community Server (GPL) Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create database data; Query OK, 1 row affected (0.01 sec) mysql> use data; Database changed mysql> create table data_mhs( -> id_mhs INT(4), -> nama VARCHAR(30), -> nim VARCHAR(8), -> alamat VARCHAR(40), -> thn_masuk INT(4)); Query OK, 0 rows affected (0.11 sec) mysql> show tables; +----------------+ | Tables_in_data | +----------------+ | data_mhs | +----------------+ 1 row in set (0.02 sec) mysql> INSERT INTO data_mhs VALUES ('1' , 'Bambang Facturohman' , '07', 'Pekal gan' , '2010'); Query OK, 1 row affected (0.05 sec) mysql> INSERT INTO data_mhs VALUES ('1' , 'Pak Admin Wonosari' , '07', 'Pekalo an' , '2010'); Query OK, 1 row affected (0.02 sec) mysql> show tables; +----------------+ | Tables_in_data | +----------------+ | data_mhs | +----------------+ 1 row in set (0.00 sec) mysql> select * from data_mhs; +--------+---------------------+------+------------+-----------+ | id_mhs | nama | nim | alamat | thn_masuk | +--------+---------------------+------+------------+-----------+ | 1 | Bambang Facturohman | 07 | Pekalongan | 2010 | | 1 | Pak Admin Wonosari | 07 | Pekalongan | 2010 | +--------+---------------------+------+------------+-----------+ 2 rows in set (0.00 sec) mysql> //Terjadi Redudant data pada saat insert ini adalah contoh yang buruk //Tolong jangan di telan mentah2 artikel ini |
Jalankan program SHIFT+F11
Output :
run:
Koneksi Berhasil
BUILD SUCCESSFUL (total time: 1 second)
Tidak ada komentar :
Posting Komentar