0% found this document useful (0 votes)
65 views4 pages

Form Input Dengan View Tabel-2

This document describes a form input in Java using a table view. The code creates a form with fields to input program study code, name and level. It connects to a database and can save, view and delete records in the table. The form is displayed in a window with labels and text fields for data entry.

Uploaded by

Saepul Bakrie
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views4 pages

Form Input Dengan View Tabel-2

This document describes a form input in Java using a table view. The code creates a form with fields to input program study code, name and level. It connects to a database and can save, view and delete records in the table. The form is displayed in a window with labels and text fields for data entry.

Uploaded by

Saepul Bakrie
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Form input dengan view tabel

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.sql.Connection;
import javax.swing.table.DefaultTableModel;

public class FormProgStudi extends JFrame{


final JTextField fkode = new JTextField(5);
JLabel lkode = new JLabel ("Kode Prodi");
final JTextField fnama = new JTextField(30);
JLabel lnama = new JLabel ("Nama Prodi");
final JTextField fjenjang = new JTextField(5);
JLabel ljenjang = new JLabel ("Jenjang");
JButton btnSave = new JButton ("Simpan");
JButton btnClose = new JButton ("Keluar");
//untuk tabel data
String[] judul={"Kode", "Program Studi","Jenjang"};
DefaultTableModel DTM;
JTable tabel = new JTable();
JScrollPane skrol=new JScrollPane();
JPanel panel = new JPanel();

Statement St;
ResultSet Rs;

public FormProgStudi(){
setTitle ("Input Program Studi");
setDefaultCloseOperation(3);
setSize(320,350);
setLayout(null);
//tabel
DTM=new DefaultTableModel(null, judul);
tabel.setModel(DTM);
skrol.getViewport().add(tabel);
tabel.setEnabled(true);

add (lkode);
add (fkode);
fkode.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Object obj = e.getSource();
if (obj == fkode){
try {
KonekDB Cn = new KonekDB();
Connection kon=Cn.konek();
St=kon.createStatement();
String SQL="select * from prodi where
kdprodi='"+fkode.getText()+"'";
Rs=St.executeQuery(SQL);

if (Rs.next()) {
JOptionPane.showMessageDialog(null, "Data
Sudah Ada") ;
fnama.setText(""+Rs.getString("nmprodi"));

fjenjang.setText(""+Rs.getString("jenjang"));
}
else {
JOptionPane.showMessageDialog(null, "Data
Baru") ;
}
St.close();
kon.close();
Rs.close();
} catch (Exception exc) {

System.err.println(exc.getMessage());
}
fnama.requestFocus();

}
}

});

add (lnama);
add (fnama);
add (ljenjang);
add (fjenjang);

//itemHandler itemHandler = new itemHandler();


add (btnClose);
btnClose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Object obj = e.getSource();
if (obj == btnClose){
//JOptionPane.showMessageDialog(null, "Keluar
");
dispose();
}

}
});

add(btnSave);
btnSave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Object obj = e.getSource();
if (obj == btnSave){
try {
KonekDB Cn = new KonekDB();
Connection kon=Cn.konek();
St=kon.createStatement();
Statement stat=kon.createStatement();
String SQL="insert into prodi values
('"+fkode.getText()+"','"+fnama.getText()+"','"+fjenjang.getText()+"')";
stat.executeUpdate(SQL);
stat.close();
kon.close();
} catch (Exception exc) {

System.err.println(exc.getMessage());
}
fkode.setText("");
fnama.setText("");
fjenjang.setText("");
fkode.requestFocus();
JOptionPane.showMessageDialog(null,
"Menyimpan data OK") ;
hapusdatatabel();
viewtabel();
}
}
});
//tabel
add(skrol);
getContentPane().add(panel);

lkode.setBounds(10, 10, 120, 20);


fkode.setBounds(130, 10, 70, 20);
lnama.setBounds(10, 35, 120, 20);
fnama.setBounds(130, 35, 150, 20);
ljenjang.setBounds(10, 60, 120, 20);
fjenjang.setBounds(130, 60, 70, 20);
btnSave.setBounds(50, 130, 100, 20);
btnClose.setBounds(150, 130, 110, 20);
skrol.setBounds(10,160,300,100);

viewtabel();

Dimension layar = Toolkit.getDefaultToolkit().getScreenSize();


int L = (layar.width - this.getSize().width) / 2;
int T = (layar.height - this.getSize().height) / 2;
this.setLocation(L,T);
setVisible(true);
}

void viewtabel(){
try {
KonekDB Cn = new KonekDB();
Connection kon=Cn.konek();
St=kon.createStatement();
String SQL="select * from prodi";
Rs=St.executeQuery(SQL);

while (Rs.next()) {
String kode=Rs.getString("kdprodi");
String nama=Rs.getString("nmprodi");
String jj=Rs.getString("jenjang");

String[] data={kode,nama,jj};
DTM.addRow(data);
}
St.close();
kon.close();
Rs.close();
} catch (Exception exc) {
System.err.println(exc.getMessage());
}
}

void hapusdatatabel(){
int row=DTM.getRowCount();
for (int i=0;i<row;i++){
DTM.removeRow(0);
}
}

public static void main(String[] args) {


//TODO Auto-generated method stub
new FormProgStudi();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy