0% found this document useful (0 votes)
62 views10 pages

Nama: Isti Maya Sari Kelas: Xii - RPL C Tugas: PBO: 1. Com - Controller

The document describes a Java program that implements a class called Form_Teman for managing contact data. It includes packages for the controller, model, view and database connection. The model class contains methods for saving, updating, deleting and retrieving contact data from a MySQL database table. The view class initializes the user interface elements including a JTable to display the data.

Uploaded by

Felica putri
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)
62 views10 pages

Nama: Isti Maya Sari Kelas: Xii - RPL C Tugas: PBO: 1. Com - Controller

The document describes a Java program that implements a class called Form_Teman for managing contact data. It includes packages for the controller, model, view and database connection. The model class contains methods for saving, updating, deleting and retrieving contact data from a MySQL database table. The view class initializes the user interface elements including a JTable to display the data.

Uploaded by

Felica putri
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/ 10

Nama : Isti Maya Sari

Kelas : XII – RPL C


Tugas : PBO

1. com.controller
package com.controller;
import com.view.Form_Teman; //
import java.sql.SQLException;

/**
*
* @author User
*/
public interface controller_teman {
public void Simpan(Form_Teman tmn) throws SQLException; //Jika kita
menggunakan perintah sql
public void Ubah(Form_Teman tmn) throws SQLException;
public void Hapus(Form_Teman tmn) throws SQLException;
public void Tampil(Form_Teman tmn) throws SQLException;
public void Baru(Form_Teman tmn); // kita melakukan perintah
dasar saja
public void KlikTabel(Form_Teman tmn) throws SQLException;

2. com.koneksi
package com.koneksi;

import java.sql.Connection;
import java.sql.DriverManager;
/**
*
* @author User
*/
public class koneksi {
private static Connection koneksi;
public static Connection getKoneksi()
{
if(koneksi== null)
{
try {
String url="jdbc:mysql://localhost/tblteman";
String username= "root"; // default username
String password= ""; // jika ada password bisa anda masukkan

DriverManager.registerDriver(new com.mysql.jdbc.Driver());
koneksi =DriverManager.getConnection(url, username, password);
} catch (Exception e) {
System.out.println(e);
}
}

return koneksi;
}
}

3. com.model
package com.model;

import com.controller.controller_teman;
import com.koneksi.koneksi;
import com.view.Form_Teman;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
*
* @author User
*/
public class Model_Teman implements controller_teman {
String jk;

public void Simpan(Form_Teman tmn) throws SQLException {


if(tmn.rbLaki.isSelected()){
jk = "Laki-laki";
}
else{
jk = "Perempuan";
}

try {

Connection con = koneksi.getKoneksi();


String sql = "insert tblnama values(?,?,?,?)";
PreparedStatement prepare= con.prepareStatement(sql);

prepare.setString(1, tmn.txtNim.getText());
prepare.setString(2, tmn.txtNama.getText());
prepare.setString(3, jk);
prepare.setString(4, (String) tmn.cbHobi.getSelectedItem());
prepare.executeUpdate();

JOptionPane.showMessageDialog(null, "Data Berhasil di Simpan");


prepare.close();
} catch (Exception e) {
System.out.println(e);
}
finally{
Tampil(tmn);
tmn.setLebarKolom();
}
}

public void Ubah(Form_Teman tmn) throws SQLException {


if(tmn.rbLaki.isSelected()){
jk = "Laki-laki";
}
else{
jk = "Perempuan";
}

try {
Connection con = koneksi.getKoneksi();
String sql = "update tblnama set Nama= ?, Jenis_Kelamin= ?, Hobi= ? where
NIM= ?";
PreparedStatement prepare= con.prepareStatement(sql);

prepare.setString(4, tmn.txtNim.getText());
prepare.setString(1, tmn.txtNama.getText());
prepare.setString(2, jk);
prepare.setString(3, (String) tmn.cbHobi.getSelectedItem());
prepare.executeUpdate();

JOptionPane.showMessageDialog(null, "Data Berhasil di Ubah");


prepare.close();
} catch (Exception e) {
System.out.println(e);
}
finally{
Tampil(tmn);
tmn.setLebarKolom();
Baru(tmn);
}
}

public void Hapus(Form_Teman tmn) throws SQLException {


try {

Connection con = koneksi.getKoneksi();


String sql = "delete from tblnama where NIM= ?";
PreparedStatement prepare= con.prepareStatement(sql);

prepare.setString(1, tmn.txtNim.getText());
prepare.executeUpdate();

JOptionPane.showMessageDialog(null, "Data Berhasil di Ubah");


prepare.close();
} catch (Exception e) {
System.out.println(e);
}
finally{
Tampil(tmn);
tmn.setLebarKolom();
Baru(tmn);
}
}

public void Baru(Form_Teman tmn) {


tmn.txtNim.setText("");
tmn.txtNama.setText("");
tmn.rbLaki.setSelected(true);
tmn.cbHobi.setSelectedIndex(0);
}
public void Tampil(Form_Teman tmn) throws SQLException {
tmn.tblmodel.getDataVector().removeAllElements();
tmn.tblmodel.fireTableDataChanged();
try {
Connection con = koneksi.getKoneksi();
Statement stt = con.createStatement();
String sql = "select * from tabel order by NIM asc";
ResultSet res = stt.executeQuery(sql);
while(res.next())
{
Object[] ob= new Object[8];
ob[0] = res.getString(1);
ob[1] = res.getString(2);
ob[2] = res.getString(3);
ob[3] = res.getString(4);
tmn.tblmodel.addRow(ob);
}
} catch (Exception e) {
System.out.println(e);
}
}

public void KlikTabel(Form_Teman tmn) throws SQLException {


try {
int pilih = tmn.Tabel.getSelectedRow();
if (pilih == -1 ){
return;
}
tmn.txtNim.setText(tmn.tblmodel.getValueAt(pilih, 0).toString());
tmn.txtNama.setText(tmn.tblmodel.getValueAt(pilih, 1).toString());
tmn.cbHobi.setSelectedItem(tmn.tblmodel.getValueAt(pilih, 3).toString());
jk = String.valueOf(tmn.tblmodel.getValueAt(pilih, 2));

} catch (Exception e) {
}
if(tmn.rbLaki.getText().equals(jk)){
tmn.rbLaki.setSelected(true);
}
else{tmn.rbPerempuan.setSelected(true);}

public void Chart(Form_Teman aThis) {


throw new UnsupportedOperationException("Not supported yet."); //To change
body of generated methods, choose Tools | Templates.
}

4. com.view
package com.view;

import com.model.Model_Teman;
import java.awt.Component; // Kita masukkan kan lib yang di
butuhkan
import java.sql.SQLException; // jangan ada yang ketinggalan
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
/**
*
* @author User
*/
public class Form_Teman extends javax.swing.JFrame {
public DefaultTableModel tblmodel;
String header[] ={"Nim", "Nama", "Jenis kelamin", "Hobi"};
/**
* Creates new form Form_Teman
*/
public Form_Teman() throws SQLException {
initComponents();

tblmodel = new DefaultTableModel(null, header);


Tabel.setModel(tblmodel);
Tabel.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
model.Tampil(this);
setLebarKolom();

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
Model_Teman model = new Model_Teman();

public void setColomnWidth(int kolom)


{
DefaultTableColumnModel dtcm = (DefaultTableColumnModel) Tabel.
getColumnModel();
TableColumn kolomtabel = dtcm.getColumn(kolom);
int lebar = 0;
int margin = 10;
int a;

TableCellRenderer renderer = kolomtabel.getHeaderRenderer();


if (renderer ==null){
renderer = Tabel.getTableHeader().getDefaultRenderer();
}
Component komponen = renderer.getTableCellRendererComponent(Tabel,
kolomtabel.getHeaderValue(), false, false, 0, 0);
lebar = komponen.getPreferredSize().width;
for (a = 0; a< Tabel.getRowCount(); a++){
renderer = Tabel.getCellRenderer(a, kolom);
komponen = renderer.getTableCellRendererComponent(Tabel,
Tabel.getValueAt(a, kolom), false, false, a, kolom);
int lebarKolom = komponen.getPreferredSize().width;
lebar = Math.max(lebar, lebarKolom);
}
lebar = lebar + margin;
kolomtabel.setPreferredWidth(lebar);

}
public void setLebarKolom(){
int a;
for(a =0; a< Tabel.getColumnCount(); a++){
setColomnWidth(a);
}
}

private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
try {
model.Hapus(this);
} catch (SQLException ex) {
Logger.getLogger(Form_Teman.class.getName()).log(Level.SEVERE, null, ex);
}
}

private void btnSimpanActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
try {
// TODO add your handling code here:
model.Simpan(this);
} catch (SQLException ex) {
Logger.getLogger(Form_Teman.class.getName()).log(Level.SEVERE, null, ex);
}
}

private void btnBaruActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
model.Baru(this);
}

private void btnUbahActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
try {
// TODO add your handling code here:
model.Ubah(this);
} catch (SQLException ex) {
Logger.getLogger(Form_Teman.class.getName()).log(Level.SEVERE, null, ex);
}
}

private void btnKeluarActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
dispose();
}

private void TabelMouseClicked(java.awt.event.MouseEvent evt) {


try {
// TODO add your handling code here:
model.KlikTabel(this);
} catch (SQLException ex) {
Logger.getLogger(Form_Teman.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code
(optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look
and feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(Form_Teman.class.getName()).log(java.util.loggin
g.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(Form_Teman.class.getName()).log(java.util.loggin
g.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(Form_Teman.class.getName()).log(java.util.loggin
g.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(Form_Teman.class.getName()).log(java.util.loggin
g.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try{
new Form_Teman().setVisible(true);
}catch (SQLException ex) {
Logger.getLogger(Form_Teman.class.getName()).log(Level.SEVERE, null,
ex);
}}

});
}

5. Output

*Mohon maaf bu dalam project ini masih belum bisa sempurna yakni table belum bisa
keluar. Saya sudah berusaha dengan tanya keteman dan lainnya untuk mengatasinya tapi
saya masih belum bisa. Sekali lagi mohon maaf dan terimakasih

You might also like