Skip to content

Instantly share code, notes, and snippets.

@resarahadian
Created July 11, 2013 07:29
Show Gist options
  • Save resarahadian/5973290 to your computer and use it in GitHub Desktop.
Save resarahadian/5973290 to your computer and use it in GitHub Desktop.
Simpan Data dari JTable
import java.sql.DriverManager;
import java.sql.Connection;
public class Koneksi
{
private static Connection koneksi;
public static Connection getKoneksi()
{
if(koneksi == null)
{
try
{
String url = "jdbc:mysql://localhost/Pegawai";
String username = "root";
String password = "root";
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
koneksi = DriverManager.getConnection(url,username,password);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
return koneksi;
}
}
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import java.awt.Color;
public class simpanDataTabel extends JFrame
{
private JPanel contentPane;
private JTextField txtNoPegawai;
private JTextField txtNama;
private JLabel lblAlamat;
private JTextField txtAlamat;
private JButton btnTambah;
private JTable tabel;
private JButton btnSimpan;
private JButton btnKeluar;
String header[] = {"No.Pegawai","Nama","Alamat"};
DefaultTableModel tabelModel;
private JLabel lblWall;
/**
* Create the frame.
*/
public simpanDataTabel()
{
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 514, 337);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
setVisible(true);
setLocationRelativeTo(null);
JLabel lblNopegawai = new JLabel("No.Pegawai : ");
lblNopegawai.setForeground(new Color(255, 255, 255));
lblNopegawai.setBounds(12, 12, 98, 15);
contentPane.add(lblNopegawai);
txtNoPegawai = new JTextField();
txtNoPegawai.setBounds(117, 10, 114, 19);
contentPane.add(txtNoPegawai);
txtNoPegawai.setColumns(10);
JLabel lblNama = new JLabel("Nama : ");
lblNama.setForeground(new Color(255, 255, 255));
lblNama.setBounds(12, 39, 70, 15);
contentPane.add(lblNama);
txtNama = new JTextField();
txtNama.setBounds(117, 41, 298, 19);
contentPane.add(txtNama);
txtNama.setColumns(10);
lblAlamat = new JLabel("Alamat : ");
lblAlamat.setForeground(new Color(255, 255, 255));
lblAlamat.setBounds(12, 73, 70, 15);
contentPane.add(lblAlamat);
txtAlamat = new JTextField();
txtAlamat.setBounds(117, 73, 206, 19);
contentPane.add(txtAlamat);
txtAlamat.setColumns(10);
btnTambah = new JButton("Tambah");
btnTambah.setIcon(new ImageIcon("/home/lubuntu/workspace/TesApp/src/Gambar/Tambah.png"));
btnTambah.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent act)
{
Object obj[] = new Object[3];
obj[0] = txtNoPegawai.getText();
obj[1] = txtNama.getText();
obj[2] = txtAlamat.getText();
tabelModel.addRow(obj);
txtNoPegawai.setText("");
txtNama.setText("");
txtAlamat.setText("");
}
});
btnTambah.setBounds(117, 104, 136, 37);
contentPane.add(btnTambah);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(117, 153, 345, 83);
contentPane.add(scrollPane);
tabelModel = new DefaultTableModel(null,header);
tabel = new JTable();
tabel.setModel(tabelModel);
scrollPane.setViewportView(tabel);
btnSimpan = new JButton("Simpan");
btnSimpan.setIcon(new ImageIcon("/home/lubuntu/workspace/TesApp/src/Gambar/Simpan.png"));
btnSimpan.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent act)
{
try
{
Connection konek = Koneksi.getKoneksi();
Statement state = konek.createStatement();
int baris = tabel.getRowCount();
for (int a=0; a<baris; a++)
{
String query = "INSERT INTO Data (NoPegawai,Nama,Alamat) VALUES( '"+tabel.getValueAt(a,0)+"','"+tabel.getValueAt(a,1)+"','"+tabel.getValueAt(a,2)+"')";
state.executeUpdate(query);
}
state.close();
JOptionPane.showMessageDialog(null,"Data berhasil disimpan","Pesan",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"Data gagal disimpan","Pesan",JOptionPane.ERROR_MESSAGE);
System.out.println(ex);
}
}
});
btnSimpan.setBounds(117, 254, 117, 41);
contentPane.add(btnSimpan);
btnKeluar = new JButton("Keluar");
btnKeluar.setIcon(new ImageIcon("/home/lubuntu/workspace/TesApp/src/Gambar/Keluar.png"));
btnKeluar.setBounds(275, 254, 117, 41);
contentPane.add(btnKeluar);
lblWall = new JLabel("");
lblWall.setIcon(new ImageIcon("/home/lubuntu/workspace/TesApp/src/Gambar/Wall.png"));
lblWall.setBounds(0, 0, 512, 307);
contentPane.add(lblWall);
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new simpanDataTabel();
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment