Siapkan Databasenya Dan Tabelnya: Type Help ' or /H' For Help. Type /C' To Clear The Buffer
1. The document discusses creating a database connection in Java using JDBC and MySQL. It includes code for connecting to a database, executing queries, and retrieving and displaying results.
2. Form layouts and code are provided for creating user interfaces for inserting, updating, and searching data in database tables.
3. Methods are defined for common database tasks like connecting, executing queries, loading combo boxes, and retrieving/modifying records.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
44 views
Siapkan Databasenya Dan Tabelnya: Type Help ' or /H' For Help. Type /C' To Clear The Buffer
1. The document discusses creating a database connection in Java using JDBC and MySQL. It includes code for connecting to a database, executing queries, and retrieving and displaying results.
2. Form layouts and code are provided for creating user interfaces for inserting, updating, and searching data in database tables.
3. Methods are defined for common database tasks like connecting, executing queries, loading combo boxes, and retrieving/modifying records.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9
Siapkan databasenya dan Tabelnya
badi@localhost ~]$ mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 32 Server version: 5.1.32 Source distribution Type help; or \h for help. Type \c to clear the buffer. mysql> use simpeg; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from pegawai; +-------+-----------------------+--------+------+-------+ | NIP | NAMA | ALAMAT | SEKS | AGAMA | +-------+-----------------------+--------+------+-------+ | 10002 | Endang Susilowati sss | Madiun | P | ISLAM | | 10003 | Made Saputra | Bali | L | HINDU | | 10004 | Bejo | Bantul | L | BUDHA | +-------+-----------------------+--------+------+-------+ 3 rows in set (0.00 sec)
Membuat Program Koneksi dalam Kelas Lihat contoh program berkut : /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package jdbc1a; import java.sql.*; import javax.swing.*; /***************************************** * * @author badiyanto * STMIK AKAKOM Yogyakarta *******************************************/ public class KoneksiDb { public Connection conn; public Statement st; public ResultSet rs; // Fungsi koneksi public void koneksi() { // memangil fungsi konek() untuk melakukan koneksi konek("localhost","simpeg","root",""); } // Program untuk melakukan koneksi public void konek(String server,String db, String user,String passwd){ System.out.println("Keterangan"); //memanggil driver try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println( "Ada kesalahan Driver ODBC tidak barhasil Load"); e.printStackTrace(); return; } System.out.println("Mysql JDBC Driver berhasil di Load"); conn = null; try { conn = DriverManager.getConnection("jdbc:mysql://"+ server+":3306/"+db,user,passwd); } catch (SQLException e) { System.out.println("Tidak bisa koneksi ke database"); e.printStackTrace(); return; } if (conn != null) System.out.println("Berhasil Koneksi!"); else System.out.println("Koneksi Gagal........ !"); } // program mengisikan kombobox public void IsiKombo(JComboBox kombo, String tabel, String isi, String urut ){ koneksi(); String sql="SELECT "+isi+" as hasil FROM "+tabel+" ORDER BY "+urut; kombo.removeAllItems(); try { st = conn.createStatement(); rs = st.executeQuery(sql); } catch (SQLException e){ return; } try{ while (rs.next()) { kombo.addItem(rs.getString("hasil").toString()); } rs.close(); st.close(); } catch (SQLException e){ return; } } // Program mencari data dengan kunci public String CariData(String tabel, String fHasil, String fKunci, String fCari ){ String hs=""; String sql="SELECT "+fHasil+" as hasil FROM "+tabel+" WHERE "+fKunci+"='"+fCari+"'"; koneksi(); try { st = conn.createStatement(); rs = st.executeQuery(sql); } catch (SQLException e){ return(""); } try{ while (rs.next()) { hs = rs.getString("hasil").toString(); } rs.close(); st.close(); } catch (SQLException e){ return(""); } return(hs); } // Program menampilkan isikombo public void CariIsiKombo(JComboBox k,String c){ int i=0; int ketemu=0; while (i<=k.getItemCount()-1 ) { if (c.equals(k.getItemAt(i).toString())) { ketemu=i; } i++; } k.setSelectedIndex(ketemu); } }