0% found this document useful (0 votes)
17 views7 pages

Code./

Uploaded by

nabil.22lhf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

Code./

Uploaded by

nabil.22lhf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Class Koneksi

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import javax.swing.JOptionPane;

private static Connection mysqlkonek;

public static Connection koneksiDB() throws SQLException {

if (mysqlkonek==null){

try {

String DB="jdbc:mysql://localhost:3306/dbdata";

String user="root";

String pass="";

DriverManager.registerDriver(new com.mysql.jdbc.Driver());

mysqlkonek=(Connection) DriverManager.getConnection(DB,user,pass);

} catch (Exception e){

JOptionPane.showMessageDialog(null, "koneksi gagal");

} return mysqlkonek;

2.jframe Form Apk

import java.sql.*;

import java.text.Format;

import javax.swing.*;

import javax.swing.table.*;
import java.text.SimpleDateFormat;

import java.util.Date;

import javax.swing.JOptionPane;

import java.awt.HeadlessException;

import java.sql.Connection;

import java.sql.SQLException;

##membuat tgl##

================================================================================

//public String tgl_lahir; kode ini ada di luar area scope event tgl

if (tgl.getDate()!=null){

SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");

tgl_lahir=format.format (tgl.getDate());

================================================================================

##tampilkan data##

================================================================================

public void tampil_data(){

DefaultTableModel tabel=new DefaultTableModel();

tabel.addColumn("NIM");

tabel.addColumn("NAMA");

tabel.addColumn("GENDER");

tabel.addColumn("TEMPAT");

tabel.addColumn("TGL LAHIR");

tabel.addColumn("ALAMAT");
tabel.addColumn("TELP/HP");

try {

java.sql.Connection conn = (java.sql.Connection) projek. Koneksi.koneksiDB();

String sql = "select * from tbdata";

java.sql.PreparedStatement pst= conn.prepareStatement ( sql);

ResultSet rs = pst.executeQuery( sql);

while(rs.next())

tabel.addRow(new Object[]{

rs.getString(1),

rs.getString(2),

rs.getString(3),

rs.getString(4),

rs.getString(5),

rs.getString(6),

rs.getString(7)});

jTable1.setModel(tabel);

catch (Exception e){

================================================================================

##tombol simpan##

================================================================================

try{
String sql="insert into tbdata values('"

+no.getText()+"','"

+nama.getText()+"','"

+gender.getSelectedItem()+"','"

+tempat.getText()+"','"

+tgl_lahir+"','"

+alamat.getText()+"','"

+hp.getText()+"')";

java.sql.Connection conn=(java.sql.Connection)projek.Koneksi.koneksiDB();

java.sql.PreparedStatement pst=conn.prepareStatement(sql);

pst.execute();

JOptionPane.showMessageDialog(null, "Berhasil disimpan");

tampil_data();

catch (Exception e){

JOptionPane.showMessageDialog(null, "Gagal disimpan");

System.out.println(e.getMessage());

================================================================================

##tombol cari##

================================================================================

try {

java.sql.Connection conn=(java.sql.Connection)projek.Koneksi.koneksiDB();

String sql = "Select * from tbdata where no='"+cari.getText()+"' or nama='"+cari.getText()+"'";

java.sql.PreparedStatement pst = conn.prepareStatement(sql);

ResultSet rs = pst.executeQuery(sql);
if (rs.next()){

no.setText(rs.getString(1));

nama.setText(rs.getString(2));

gender.setSelectedItem(rs.getString(3));

tempat.setText(rs.getString(4));

tgl.setDate(rs.getDate(5));

alamat.setText(rs.getString(6));

hp.setText(rs.getString(7));

JOptionPane.showMessageDialog(null,"Data tbdata : "

+ cari.getText() + " ditemukan, Apakah Anda Ingin Mengedit atau Menghapus?");

} catch (Exception e){}

cari.requestFocus();

================================================================================

##tombol edit##

================================================================================

try {

java.sql.Connection conn = (java.sql.Connection) projek.Koneksi.koneksiDB();

String sql = "UPDATE tbdata SET nama=?, gender=?, tempat=?, tgl=?, alamat=?, hp=? WHERE no=?";

java.sql.PreparedStatement pst = conn.prepareStatement(sql);

// Mengatur nilai untuk parameter

pst.setString(1, nama.getText());

pst.setString(2, (String) gender.getSelectedItem());

pst.setString(3, tempat.getText());

pst.setString(4, tgl_lahir);
pst.setString(5, alamat.getText());

pst.setString(6, hp.getText());

pst.setInt(7, Integer.parseInt(no.getText()));

// Menjalankan perintah update

pst.executeUpdate();

JOptionPane.showMessageDialog(null, "Data berhasil di Koreksi");

tampil_data();

} catch (Exception e) {

JOptionPane.showMessageDialog(null, "Proses Edit data Gagal");

System.out.println(e.getMessage());

================================================================================

##tombol hapus##

================================================================================

try{

java.sql.Connection conn=(java.sql.Connection)projek.Koneksi.koneksiDB();

String sql = "delete from tbdata where no='"+no.getText()+"' ";

java.sql.PreparedStatement pst = conn.prepareStatement(sql);

pst.executeUpdate(); JOptionPane.showMessageDialog(null,"Data berhasil di hapus");

tampil_data();

catch (Exception e){

JOptionPane.showMessageDialog(null,"Proses Penghapusan Gagal");

System.out.println(e.getMessage());
}

================================================================================

##tombol batal##

================================================================================

no.setText("");

nama.setText("");

gender.setSelectedItem("");

tempat.setText("");

tgl.setDate(null);

alamat.setText("");

hp.setText("");

no.requestFocus();

================================================================================

You might also like