Senin, 28 April 2014
Senin, 21 April 2014
Rabu, 21 Desember 2011
Graphic
AnotherSimpleThread
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package grafik;
public class AnotherSimpleThread implements Runnable {
Thread thread;
/** Creates a new instance of AnotherSimpleThread */
public AnotherSimpleThread() {
}
public AnotherSimpleThread(String name) {
thread = new Thread(this, name);
thread.start();
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i + " " + thread.getName());
try {
thread.sleep((long) (Math.random() * 1000));
} catch (InterruptedException e) {
}
}
System.out.println("DONE! " + thread.getName());
}
//}
/* AnotherTwoThreadsTest.java */
//public class AnotherTwoThreadsTest {
public static void main(String[] args) {
new AnotherSimpleThread("Bali");
new AnotherSimpleThread("Jogja");
}
}
Counter
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package grafik;
import javax.swing.JLabel;
public class Counter implements Runnable {
JLabel outputLabel;
private int value;
Thread thread;
private boolean start = true;
/** Creates a new instance of Counter */
public Counter() {
setValue(0);
}
/** Creates a new instance of Counter */
public Counter(JLabel output) {
outputLabel = output;
setValue(0);
thread = new Thread(this);
thread.start();
}
public void run() {
while (isStart()) {
outputLabel.setText("Counter: " + getValue());
try {
thread.sleep(500);
setValue(getValue() + 1);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
public boolean isStart() {
return start;
}
public void setStart(boolean start) {
this.start = start;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
Kelas ImagePanel
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package grafik;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImagePanel extends JPanel {
private BufferedImage logo;
/** Creates a new instance of ImagePanel */
public ImagePanel() {
try {
// buka file
File img = new File(" ");
// baca image
logo = ImageIO.read(img);
// img.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public void paintComponent(Graphics g) {
g.drawImage(logo,
0, 0, 400, 300,
0, 0, logo.getWidth(null), logo.getHeight(null),
null);
}
public static void main(String args[]) {
JFrame frame = new JFrame("Using Java2D");
frame.setLayout(new BorderLayout());
ImagePanel gc = new ImagePanel();
frame.add(gc, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 250);
frame.setVisible(true);
}
}
Kelas MyGraphic
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package grafik;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
/**
*
* @author admin
*/
public class MyGraphic extends JPanel{
@Override
public void paintComponent (Graphics g){
super.paintComponent(g);
g.setColor(Color.red);
g.fillArc(100, 100, 100, 100, 0, 180);
}
}
Kelas MyGraphic1
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package grafik;
import java.awt.Color;
import javax.swing.JPanel;
/**
*
* @author admin
*/
public class MyGraphic1 extends JPanel implements Runnable {
Color warna = Color.red;
public void run() {
try{
Thread.sleep(4000);
warna = Color.green;
this.repaint();
System.out.println("Warna Jadi Berubah");
Thread.sleep(4000);
warna = Color.black;
this.repaint();
System.out.println("Warna Jadi Berubah");
} catch (Interrupted ){
}
}
}
Kelas ShapePanel
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package grafik;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ShapePanel extends JPanel {
public ShapePanel() {
setBackground(Color.white);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.drawLine(0, 10, 50, 60);
g.setColor(Color.red);
g.drawRect(50, 10, 50, 50);
g.setColor(new Color(255, 0, 0));
g.fillOval(100, 10, 50, 50);
g.setColor(new Color(0, 255, 0));
g.fillArc(150, 10, 50, 50, 0, 180);
Graphics2D g2 = (Graphics2D) g;
// fill RoundRectangle2D.Double
GradientPaint redtowhite = new GradientPaint(200, 10, Color.red, 250, 10, Color.black);
g2.setPaint(redtowhite);
g2.fill(new RoundRectangle2D.Double(200, 10, 50, 50, 10, 10));
//g2.setPaint();
g2.drawString("Filled RoundRectangle2D", 200, 80);
}
public static void main(String args[]) {
JFrame frame = new JFrame("Grafik 2 Dimensi");
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setLayout(new BorderLayout());
ShapePanel shapePanel = new ShapePanel();
frame.add(shapePanel, BorderLayout.CENTER);
frame.setSize(450, 250);
frame.setVisible(true);
}
}
Kelas SimpleGUI
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package grafik;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class SimpleGUI extends JFrame {
private JButton clickButton, stopButton;
private JLabel counterLabel;
private Counter counter;
/** Creates a new instance of SimpleGUI */
public SimpleGUI() {
clickButton = new JButton("Start");
stopButton = new JButton("Stop");
counterLabel = new JLabel("Counter: ");
this.setLayout(new FlowLayout());
this.add(clickButton);
this.add(counterLabel);
this.add(stopButton);
this.setTitle("Thread application");
this.setSize(300, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clickButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clickAction();
}
});
stopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stopAction();
}
});
}
public void clickAction() {
counter = new Counter(counterLabel);
}
public void stopAction() {
counter.setStart(false);
}
//}
//public class SimpleGUITester {
/** Creates a new instance of SimpleGUITester */
// public SimpleGUITester() {
// }
public static void main(String[] args) {
SimpleGUI test = new SimpleGUI();
}
}
Kelas SimpleThread
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package grafik;
public class SimpleThread extends Thread {
public SimpleThread(String str) {
super(str);
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i + " " + getName());
try {
sleep((long) (Math.random() * 1000));
} catch (InterruptedException e) {
}
}
System.out.println("DONE! " + getName());
}
public static void main(String[] args) {
new SimpleThread("Bali").start();
new SimpleThread("Jogja").start();
}
}
LATIHAN
No7
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Tugas;
import javax.swing.JButton;
import javax.swing.JTextField;
/**
*
* @author admin
*/
public class No7 extends Thread {
JTextField outputLabel;
JButton StarStop;
private int value;
Thread thread;
private boolean start = false;
/** Creates a new instance of Counter */
public No7() {
}
/** Creates a new instance of Counter */
public No7(JTextField outputLabel, JButton StarStop) {
this.outputLabel=outputLabel;
this.StarStop=StarStop;
}
@Override
public void start(){
thread = new Thread(this);
if (start == false){
thread.start();
start = true;
StarStop.setText("Stop");
}else{
start=false;
StarStop.setText("Start");
}
}
@Override
public void run() {
while (start) {
value = (int) (Math.random()*1000);
outputLabel.setText(String.valueOf(value));
try {
thread.sleep(500);
} catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
}
public boolean isStart() {
return start;
}
public void setStart(boolean start) {
this.start = start;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
Test
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Tugas;
/**
*
* @author admin
*/
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Test_No7 m = new Test_No7();
m.setVisible(true);
}
}
Test_No7
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Tugas;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Test_No7 extends JFrame {
private JButton clickButton, stopButton;
private JTextField counterLabel;
private JPanel A;
boolean start = true;
private No7 counter;
/** Creates a new instance of SimpleGUI */
public Test_No7() {
A = new JPanel();
clickButton = new JButton("Start");
counterLabel = new JTextField(10);
counterLabel.setEditable(false);
this.setLayout(new FlowLayout());
this.add(clickButton);
this.add(A);
A.add(clickButton);
A.add(counterLabel);
this.setSize(300, 200);
this.setVisible(true);
this.setContentPane(A);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clickButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
No7();
}
});
}
public void No7() {
if (start) {
counter = new No7(counterLabel, stopButton);
counter.start();
start = false;
} else if (start == false) {
counter.start();
start = true;
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package grafik;
public class AnotherSimpleThread implements Runnable {
Thread thread;
/** Creates a new instance of AnotherSimpleThread */
public AnotherSimpleThread() {
}
public AnotherSimpleThread(String name) {
thread = new Thread(this, name);
thread.start();
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i + " " + thread.getName());
try {
thread.sleep((long) (Math.random() * 1000));
} catch (InterruptedException e) {
}
}
System.out.println("DONE! " + thread.getName());
}
//}
/* AnotherTwoThreadsTest.java */
//public class AnotherTwoThreadsTest {
public static void main(String[] args) {
new AnotherSimpleThread("Bali");
new AnotherSimpleThread("Jogja");
}
}
Counter
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package grafik;
import javax.swing.JLabel;
public class Counter implements Runnable {
JLabel outputLabel;
private int value;
Thread thread;
private boolean start = true;
/** Creates a new instance of Counter */
public Counter() {
setValue(0);
}
/** Creates a new instance of Counter */
public Counter(JLabel output) {
outputLabel = output;
setValue(0);
thread = new Thread(this);
thread.start();
}
public void run() {
while (isStart()) {
outputLabel.setText("Counter: " + getValue());
try {
thread.sleep(500);
setValue(getValue() + 1);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
public boolean isStart() {
return start;
}
public void setStart(boolean start) {
this.start = start;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
Kelas ImagePanel
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package grafik;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImagePanel extends JPanel {
private BufferedImage logo;
/** Creates a new instance of ImagePanel */
public ImagePanel() {
try {
// buka file
File img = new File(" ");
// baca image
logo = ImageIO.read(img);
// img.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public void paintComponent(Graphics g) {
g.drawImage(logo,
0, 0, 400, 300,
0, 0, logo.getWidth(null), logo.getHeight(null),
null);
}
public static void main(String args[]) {
JFrame frame = new JFrame("Using Java2D");
frame.setLayout(new BorderLayout());
ImagePanel gc = new ImagePanel();
frame.add(gc, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 250);
frame.setVisible(true);
}
}
Kelas MyGraphic
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package grafik;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
/**
*
* @author admin
*/
public class MyGraphic extends JPanel{
@Override
public void paintComponent (Graphics g){
super.paintComponent(g);
g.setColor(Color.red);
g.fillArc(100, 100, 100, 100, 0, 180);
}
}
Kelas MyGraphic1
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package grafik;
import java.awt.Color;
import javax.swing.JPanel;
/**
*
* @author admin
*/
public class MyGraphic1 extends JPanel implements Runnable {
Color warna = Color.red;
public void run() {
try{
Thread.sleep(4000);
warna = Color.green;
this.repaint();
System.out.println("Warna Jadi Berubah");
Thread.sleep(4000);
warna = Color.black;
this.repaint();
System.out.println("Warna Jadi Berubah");
} catch (Interrupted ){
}
}
}
Kelas ShapePanel
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package grafik;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ShapePanel extends JPanel {
public ShapePanel() {
setBackground(Color.white);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.drawLine(0, 10, 50, 60);
g.setColor(Color.red);
g.drawRect(50, 10, 50, 50);
g.setColor(new Color(255, 0, 0));
g.fillOval(100, 10, 50, 50);
g.setColor(new Color(0, 255, 0));
g.fillArc(150, 10, 50, 50, 0, 180);
Graphics2D g2 = (Graphics2D) g;
// fill RoundRectangle2D.Double
GradientPaint redtowhite = new GradientPaint(200, 10, Color.red, 250, 10, Color.black);
g2.setPaint(redtowhite);
g2.fill(new RoundRectangle2D.Double(200, 10, 50, 50, 10, 10));
//g2.setPaint();
g2.drawString("Filled RoundRectangle2D", 200, 80);
}
public static void main(String args[]) {
JFrame frame = new JFrame("Grafik 2 Dimensi");
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setLayout(new BorderLayout());
ShapePanel shapePanel = new ShapePanel();
frame.add(shapePanel, BorderLayout.CENTER);
frame.setSize(450, 250);
frame.setVisible(true);
}
}
Kelas SimpleGUI
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package grafik;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class SimpleGUI extends JFrame {
private JButton clickButton, stopButton;
private JLabel counterLabel;
private Counter counter;
/** Creates a new instance of SimpleGUI */
public SimpleGUI() {
clickButton = new JButton("Start");
stopButton = new JButton("Stop");
counterLabel = new JLabel("Counter: ");
this.setLayout(new FlowLayout());
this.add(clickButton);
this.add(counterLabel);
this.add(stopButton);
this.setTitle("Thread application");
this.setSize(300, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clickButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clickAction();
}
});
stopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stopAction();
}
});
}
public void clickAction() {
counter = new Counter(counterLabel);
}
public void stopAction() {
counter.setStart(false);
}
//}
//public class SimpleGUITester {
/** Creates a new instance of SimpleGUITester */
// public SimpleGUITester() {
// }
public static void main(String[] args) {
SimpleGUI test = new SimpleGUI();
}
}
Kelas SimpleThread
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package grafik;
public class SimpleThread extends Thread {
public SimpleThread(String str) {
super(str);
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i + " " + getName());
try {
sleep((long) (Math.random() * 1000));
} catch (InterruptedException e) {
}
}
System.out.println("DONE! " + getName());
}
public static void main(String[] args) {
new SimpleThread("Bali").start();
new SimpleThread("Jogja").start();
}
}
LATIHAN
No7
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Tugas;
import javax.swing.JButton;
import javax.swing.JTextField;
/**
*
* @author admin
*/
public class No7 extends Thread {
JTextField outputLabel;
JButton StarStop;
private int value;
Thread thread;
private boolean start = false;
/** Creates a new instance of Counter */
public No7() {
}
/** Creates a new instance of Counter */
public No7(JTextField outputLabel, JButton StarStop) {
this.outputLabel=outputLabel;
this.StarStop=StarStop;
}
@Override
public void start(){
thread = new Thread(this);
if (start == false){
thread.start();
start = true;
StarStop.setText("Stop");
}else{
start=false;
StarStop.setText("Start");
}
}
@Override
public void run() {
while (start) {
value = (int) (Math.random()*1000);
outputLabel.setText(String.valueOf(value));
try {
thread.sleep(500);
} catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
}
public boolean isStart() {
return start;
}
public void setStart(boolean start) {
this.start = start;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
Test
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Tugas;
/**
*
* @author admin
*/
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Test_No7 m = new Test_No7();
m.setVisible(true);
}
}
Test_No7
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Tugas;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Test_No7 extends JFrame {
private JButton clickButton, stopButton;
private JTextField counterLabel;
private JPanel A;
boolean start = true;
private No7 counter;
/** Creates a new instance of SimpleGUI */
public Test_No7() {
A = new JPanel();
clickButton = new JButton("Start");
counterLabel = new JTextField(10);
counterLabel.setEditable(false);
this.setLayout(new FlowLayout());
this.add(clickButton);
this.add(A);
A.add(clickButton);
A.add(counterLabel);
this.setSize(300, 200);
this.setVisible(true);
this.setContentPane(A);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clickButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
No7();
}
});
}
public void No7() {
if (start) {
counter = new No7(counterLabel, stopButton);
counter.start();
start = false;
} else if (start == false) {
counter.start();
start = true;
}
}
}
Label:
program java(netbeans)
Program Pengelola Surat
Kelas Datahandler
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kasusakhirsemester3;
import java.sql.*;
import oracle.jdbc.pool.OracleDataSource;
public class DataHandler {
// string „localhost‟ anda ganti dengan IP server yang digunakan
String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:orcl3";
String userid = "kejut";
String password = "105314078";
Connection conn;
Statement stmt;
ResultSet rset;
String query;
String sqlString;
public DataHandler() {
}
public void getDBConnection() {
try {
OracleDataSource ds;
ds = new OracleDataSource();
ds.setURL(jdbcUrl);
conn = ds.getConnection(userid, password);
System.out.println("Koneksi berhasil");
} catch (SQLException ex) {
System.out.println("Masih belum konek");
}
}
public void close() {
try {
conn.close();
} catch (SQLException ex) {
System.out.println("Tidak bisa tutup koneksi");
}
}
public static void main(String[] args) throws SQLException {
DataHandler datahandler = new DataHandler();
datahandler.getDBConnection();
datahandler.close();
}
}
Kelas DataSurat
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kasusakhirsemester3;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class DataSurat {
private int noSurat;
private String perihal;
private String tanggalSurat;
private String tanggalDiterima;
private String pengirim;
private String penerima;
public DataSurat() {
}
public DataSurat(int noSurat, String perihal, String tanggalSurat, String tanggalDiterima, String pengirim, String penerima) {
this.noSurat = noSurat;
this.perihal = perihal;
this.tanggalSurat = tanggalSurat;
this.tanggalDiterima = tanggalDiterima;
this.pengirim = pengirim;
this.penerima = penerima;
}
public int getNoSurat() {
return noSurat;
}
public void setNoSurat(int noSurat) {
this.noSurat = noSurat;
}
public String getPerihal() {
return perihal;
}
public void setPerihal(String perihal) {
this.perihal = perihal;
}
public String getTanggalSurat() {
return tanggalSurat;
}
public void setTanggalSurat(String tanggalSurat) {
this.tanggalSurat = tanggalSurat;
}
public String getTanggalDiterima() {
return tanggalDiterima;
}
public void setTanggalDiterima(String tanggalDiterima) {
this.tanggalDiterima = tanggalDiterima;
}
public String getPengirim() {
return pengirim;
}
public void setPengirim(String pengirim) {
this.pengirim = pengirim;
}
public String getPenerima() {
return penerima;
}
public void setPenerima(String penerima) {
this.penerima = penerima;
}
public static ArrayList searchSurat(String keyword) throws SQLException {
ArrayList result = null;
DataHandler dataHandler = new DataHandler();
dataHandler.getDBConnection();
Connection conn = dataHandler.conn;
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
String query = "SELECT * FROM data_surat where no_surat like '%" + keyword + "%'";
ResultSet rset = stmt.executeQuery(query);
result = new ArrayList();
while (rset.next()) {
DataSurat temp = new DataSurat(rset.getInt(1), rset.getString(2), rset.getString(3), rset.getString(4), rset.getString(5), rset.getString(6));
result.add(temp);
}
conn.close();
return result;
}
public static ArrayList addSurat(int noSurat, String perihal, String tanggalSurat, String tanggalDiterima, String pengirim, String penerima ) throws SQLException {
ArrayList result = null;
DataHandler dataHandler = new DataHandler();
dataHandler.getDBConnection();
Connection conn = dataHandler.conn;
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String query = "insert into data_surat values (" + noSurat + ", '" + perihal + "', '" + tanggalSurat + "', '"+ tanggalDiterima + "', '"+ pengirim + "', '"+ penerima +"')";
result = new ArrayList();
try {
result.add(stmt.execute(query));
} catch (SQLException ex) {
System.out.println("Eror:" + ex.getMessage());
}
conn.close();
return result;
}
public static ArrayList searchSurat1() throws SQLException {
ArrayList result = null;
DataHandler dataHandler = new DataHandler();
dataHandler.getDBConnection();
Connection conn = dataHandler.conn;
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
String query2 = "select * from data_surat";
result = new ArrayList();
ResultSet rset = stmt.executeQuery(query2);
while (rset.next()) {
DataSurat temp = new DataSurat(rset.getInt(1), rset.getString(2), rset.getString(3), rset.getString(4), rset.getString(5), rset.getString(6));
result.add(temp);
}
conn.close();
return result;
}
public static ArrayList removeSurat(String keyword) throws SQLException {
ArrayList result = null;
DataHandler dataHandler = new DataHandler();
dataHandler.getDBConnection();
Connection conn = dataHandler.conn;
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
String query3 = "delete from data_surat where no_surat = '" + keyword + "'";
result = new ArrayList();
ResultSet rset = stmt.executeQuery(query3);
conn.close();
return result;
}
}
GUI DataSurat
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kasusakhirsemester3;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class DataSuratGUI extends JFrame {
public JMenuBar Proyek;
//mengeset ukuran frame yang akan kita buat
private static final int FRAME_WIDTH = 600;
private static final int FRAME_HEIGHT = 800;
private static final int FRAME_X_ORIGIN = 20;
private static final int FRAME_Y_ORIGIN = 20;
private JMenu fileMenu;
private ImageIcon icon = new ImageIcon("AngelDanboDA.jpg");
JLabel label = new JLabel(icon);
//method bertipe void untuk menampilkan frame luar yang telah kita buat tadi
public static void main(String[] args) {
DataSuratGUI frame = new DataSuratGUI();
frame.setSize(FRAME_HEIGHT, FRAME_WIDTH);
frame.setLocation(FRAME_Y_ORIGIN, FRAME_X_ORIGIN);
frame.setVisible(true);
}
//method untuk membuat frame lebih detail dan untuk mengeset
//title dari frame yang telah kita buat tadi
public DataSuratGUI() {
Container contentPane;
setTitle("Data Surat");
contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(label);
createFileMenu();
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
menuBar.add(fileMenu);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//method bertipe void untuk membuat tampilan file dalam menubar
//dan di dalamnya terdapat menuitem employee1 dan quit
private void createFileMenu() {
final JMenuItem Data1, quit, Cari;
JMenu New;
fileMenu = new JMenu("File");
New = new JMenu("New");
Data1 = new JMenuItem("Search Data");
Data1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DataSurat1();
label.setVisible(false);
}
});
Cari = new JMenuItem("Data Surat");
Cari.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SearchData1();
label.setVisible(false);
}
});
New.add(Cari);
New.add(Data1);
fileMenu.add(New);
fileMenu.addSeparator();//membuat garis tampilan pada menuitem
quit = new JMenuItem("Quit");
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int ok = JOptionPane.showConfirmDialog(null, "Apakah Anda Yakin Ingin Menutup Jendela?",
"Konfirmasi Tutup", JOptionPane.OK_CANCEL_OPTION);
if (ok == JOptionPane.OK_OPTION) {
System.exit(0);
}
}
});
fileMenu.add(quit);
fileMenu.setMnemonic(KeyEvent.VK_N);
Data1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
fileMenu.setMnemonic(KeyEvent.VK_N);
Cari.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.CTRL_MASK));
fileMenu.setMnemonic(KeyEvent.VK_P);
quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
}
//method bertipe void untuk menampilkan framenya
private void DataSurat1() {
JIFDataSurat frameEmp = new JIFDataSurat("Data Surat Search");
frameEmp.setVisible(true);
this.add(frameEmp);
}
private void SearchData1() {
JIFDataSuratAdd asd = new JIFDataSuratAdd("Data Surat Add");
asd.setVisible(true);
this.add(asd);
}
//method untuk membuat actionlistener tombol quit
private void quitAction() {
System.exit(0);
}
}
Kelas DataSuratTableModel
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kasusakhirsemester3;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
/**
*
* @author user
*/
public class DataSuratTableModel extends AbstractTableModel {
//employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id
String columNames[] = {"No Surat", "Perihal", "Tanggal Surat", "Tanggal Diterima",
"Pengirim","Penerima"};
ArrayList data;
public DataSuratTableModel(String keyword) throws SQLException {
DataSurat r=new DataSurat();
data = r.searchSurat(keyword);
}
public int getRowCount() {
return data.size();
}
public int getColumnCount() {
return columNames.length;
}
@Override
public String getColumnName(int col) {
return columNames[col];
}
public Object getValueAt(int rowIndex, int columnIndex) {
DataSurat temp = (DataSurat) data.get(rowIndex);
switch (columnIndex) {
case 0:return temp.getNoSurat();
case 1:return temp.getPerihal();
case 2:return temp.getTanggalSurat();
case 3:return temp.getTanggalDiterima();
case 4:return temp.getPengirim();
case 5:return temp.getPenerima();
}
return null;
}
@Override
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
}
Kelas DataSuratTableModelAdd
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kasusakhirsemester3;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
/**
*
* @author user
*/
public class DataSuratTableModelAdd extends AbstractTableModel {
//employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id
String columNames[] = {"No Surat", "Perihal", "Tanggal Surat", "Tanggal Diterima",
"Pengirim","Penerima"};
ArrayList data,data1;
public DataSuratTableModelAdd(int noSurat, String perihal, String tanggalSurat, String tanggalDiterima, String pengirim, String penerima) throws SQLException {
DataSurat r=new DataSurat();
data = r.addSurat(noSurat, perihal, tanggalSurat, tanggalDiterima, pengirim, penerima);
data1 = r.searchSurat1();
}
public int getRowCount() {
return data.size();
}
public int getColumnCount() {
return columNames.length;
}
@Override
public String getColumnName(int col) {
return columNames[col];
}
public Object getValueAt(int rowIndex, int columnIndex) {
DataSurat temp = (DataSurat) data.get(rowIndex);
switch (columnIndex) {
case 0:return temp.getNoSurat();
case 1:return temp.getPerihal();
case 2:return temp.getTanggalSurat();
case 3:return temp.getTanggalDiterima();
case 4:return temp.getPengirim();
case 5:return temp.getPenerima();
}
return null;
}
@Override
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
}
Kelas JavaInternalFrameDataSurat
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kasusakhirsemester3;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
/**
*
* @author user
*/
public class JIFDataSurat extends javax.swing.JInternalFrame implements ActionListener{
static int openFrameCount = 0;
static final int xOffset = 30, yOffset = 30;
JPanel pa, pb, p1, p2;
JLabel label1;
JButton but1,but2;
JTextField text1;
JTable jtabel1;
public JIFDataSurat(String title) {
super(title + (++openFrameCount), true,true,true,true);
setSize(800,600);
addPanel(); //Set the window's location.
setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
}
public void addPanel() {
pa=new JPanel();
this.setBackground(Color.cyan);
pa.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createLineBorder(Color.pink), "Data Surat"));
jtabel1=new JTable();
jtabel1.setPreferredScrollableViewportSize(new Dimension(600, 200));
JScrollPane scrollPane = new JScrollPane(jtabel1);
pa.add(scrollPane);
pb=new JPanel();
pb.setLayout(new GridLayout(2,1));
p1=new JPanel();
p1.setLayout(new FlowLayout());
p2=new JPanel();
p2.setLayout(new FlowLayout());
label1=new JLabel("Masukkan no surat yang ingin dicari : ");
label1.setForeground(Color.red);
p1.add(label1);
text1=new JTextField(15);
p1.add(text1);
but1=new JButton("Ok");
but1.setForeground(Color.red);
but1.addActionListener(this);
but2 = new JButton("Delete");
but2.setForeground(Color.red);
but2.addActionListener(this);
p2.add(but1);
p2.add(but2);
pb.add(p1);
pb.add(p2);
this.setLayout(new GridLayout(2,1));
this.add(pa);
this.add(pb);
}
public void actionPerformed(ActionEvent event) {
JButton clickedButton = (JButton) event.getSource();
if ((clickedButton.getText()).compareTo("Ok") == 0) {
DataSuratTableModel e;
try {
// System.out.println(text1.getText());
e = new DataSuratTableModel(text1.getText());
jtabel1.setModel(e);
jtabel1.revalidate();
} catch (SQLException ex) {
Logger.getLogger(DataSuratGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
if ((clickedButton.getText()).compareTo("Delete") == 0) {
RemoveTableModel e;
try {
// System.out.println(text1.getText());
e = new RemoveTableModel(text1.getText());
jtabel1.setModel(e);
jtabel1.revalidate();
}
catch (SQLException ex) {
Logger.getLogger(DataSuratGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Kelas JavaInternalFrameDataSuratAdd
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kasusakhirsemester3;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
/**
*
* @author user
*/
public class JIFDataSurat extends javax.swing.JInternalFrame implements ActionListener{
static int openFrameCount = 0;
static final int xOffset = 30, yOffset = 30;
JPanel pa, pb, p1, p2;
JLabel label1;
JButton but1,but2;
JTextField text1;
JTable jtabel1;
public JIFDataSurat(String title) {
super(title + (++openFrameCount), true,true,true,true);
setSize(800,600);
addPanel(); //Set the window's location.
setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
}
public void addPanel() {
pa=new JPanel();
this.setBackground(Color.cyan);
pa.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createLineBorder(Color.pink), "Data Surat"));
jtabel1=new JTable();
jtabel1.setPreferredScrollableViewportSize(new Dimension(600, 200));
JScrollPane scrollPane = new JScrollPane(jtabel1);
pa.add(scrollPane);
pb=new JPanel();
pb.setLayout(new GridLayout(2,1));
p1=new JPanel();
p1.setLayout(new FlowLayout());
p2=new JPanel();
p2.setLayout(new FlowLayout());
label1=new JLabel("Masukkan no surat yang ingin dicari : ");
label1.setForeground(Color.red);
p1.add(label1);
text1=new JTextField(15);
p1.add(text1);
but1=new JButton("Ok");
but1.setForeground(Color.red);
but1.addActionListener(this);
but2 = new JButton("Delete");
but2.setForeground(Color.red);
but2.addActionListener(this);
p2.add(but1);
p2.add(but2);
pb.add(p1);
pb.add(p2);
this.setLayout(new GridLayout(2,1));
this.add(pa);
this.add(pb);
}
public void actionPerformed(ActionEvent event) {
JButton clickedButton = (JButton) event.getSource();
if ((clickedButton.getText()).compareTo("Ok") == 0) {
DataSuratTableModel e;
try {
// System.out.println(text1.getText());
e = new DataSuratTableModel(text1.getText());
jtabel1.setModel(e);
jtabel1.revalidate();
} catch (SQLException ex) {
Logger.getLogger(DataSuratGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
if ((clickedButton.getText()).compareTo("Delete") == 0) {
RemoveTableModel e;
try {
// System.out.println(text1.getText());
e = new RemoveTableModel(text1.getText());
jtabel1.setModel(e);
jtabel1.revalidate();
}
catch (SQLException ex) {
Logger.getLogger(DataSuratGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Kelas MainFrame
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kasusakhirsemester3;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
public class MainFrame extends JFrame {
private JMenuBar menu ;
private JMenu file ;
private JMenu New ;
private JMenuItem masukkanData ;
private masukkanData md ;
private JMenuItem quit ;
public MainFrame(){
this.setLocation(100, 100);
this.setSize(500, 300);
this.setTitle("Tugas Akhir Semester 3");
menu = new JMenuBar();
this.setJMenuBar(menu);
file = new JMenu("File");
file.setMnemonic('F');
New = new JMenu("New");
New.setMnemonic('N');
menu.add(file);
masukkanData = new JMenuItem("Masukkan Data");
masukkanData.setMnemonic('M');
quit = new JMenuItem("Quit");
quit.setMnemonic('Q');
New.add(masukkanData);
file.add(New);
file.addSeparator();
file.add(quit);
file.setMnemonic(KeyEvent.VK_P);
masukkanData.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
masukkanData.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
md = new masukkanData();
add(md);
md.setVisible(true);
}
});
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int ok = JOptionPane.showConfirmDialog(null, "Apakah Anda Yakin Ingin Menutup Jendela?",
"Konfirmasi Tutup", JOptionPane.OK_CANCEL_OPTION);
if(ok == JOptionPane.OK_OPTION){
System.exit(0);
}
}
});
}
public static void main(String[]args){
MainFrame mf = new MainFrame();
mf.setVisible(true);
mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println(System.getProperty("user.dir"));
}
}
Kelas RemoveTableModel
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kasusakhirsemester3;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
public class RemoveTableModel extends AbstractTableModel {
String columNames[] = {"No Surat", "Perihal", "Tanggal Surat", "Tanggal Penerimaan", "Pengirim", "Penerima"};
ArrayList data2;
public RemoveTableModel(String keyword) throws SQLException {
DataSurat r = new DataSurat();
data2 = r.removeSurat(keyword);
}
public int getRowCount() {
return data2.size();
}
public int getColumnCount() {
return columNames.length;
}
@Override
public String getColumnName(int col) {
return columNames[col];
}
public Object getValueAt(int rowIndex, int columnIndex) {
DataSurat temp = (DataSurat) data2.get(rowIndex);
switch (columnIndex) {
case 0:
return temp.getNoSurat();
case 1:
return temp.getPerihal();
case 2:
return temp.getTanggalSurat();
case 3:
return temp.getTanggalDiterima();
case 4:
return temp.getPengirim();
case 5:
return temp.getPenerima();
}
return null;
}
@Override
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
}
Kelas MasukkanData
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kasusakhirsemester3;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
//import kasusakhirsemester3.dataSurat;
/**
*
* @author DELL
*/
public class masukkanData extends JInternalFrame implements ActionListener {
private JTextField dataEntry1 ;
private JTextField dataEntry2 ;
private JTextField dataEntry3 ;
private JTextField dataEntry4 ;
private JTextField dataEntry5 ;
private JTextField dataEntry6 ;
private JButton ok ;
private JButton quit ;
private JButton bersihkan ;
JPanel panel = new JPanel();
JLabel nomorSurat = new JLabel("Nomor Surat:");
JLabel perihal = new JLabel("Perihal:");
JLabel tglKirim = new JLabel("Tanggal Pengiriman:");
JLabel tglTerima = new JLabel("Tanggal Penerimaan:");
JLabel pengirim = new JLabel("Pengirim:");
JLabel penerima = new JLabel("Penerima:");
public masukkanData(){
this.setLocation(100, 100);
this.setSize(500, 300);
setContentPane(panel);
Container contentPane;
contentPane = getContentPane();
contentPane.setLayout(new GridLayout(1, 1));
Color buttons_color = new Color(242,244,241);
Color background_color = new Color(241,237,222);
final JPanel dataEntry, panel1, panel2 ;
dataEntry = new JPanel(new GridLayout(2, 1));
dataEntry.setBorder(BorderFactory.createTitledBorder("Masukkan Data"));
panel1 = new JPanel(new GridLayout(6, 2));
panel1.add(nomorSurat);
nomorSurat.setForeground(Color.BLUE);
panel1.add(dataEntry6 = new JTextField(5));
panel1.add(perihal);
perihal.setForeground(Color.BLUE);
panel1.add(dataEntry5 = new JTextField(5));
panel1.add(tglKirim);
tglKirim.setForeground(Color.BLUE);
panel1.add(dataEntry4 = new JTextField(5));
panel1.add(tglTerima);
tglTerima.setForeground(Color.BLUE);
panel1.add(dataEntry3 = new JTextField(5));
panel1.add(pengirim);
pengirim.setForeground(Color.BLUE);
panel1.add(dataEntry2 = new JTextField(5));
panel1.add(penerima);
penerima.setForeground(Color.BLUE);
panel1.add(dataEntry1 = new JTextField(5));
dataEntry.add(panel1);
panel2 = new JPanel(new FlowLayout());
ok = new JButton("OK");
ok.setForeground(Color.red);
panel2.add(ok);
quit = new JButton("Quit");
quit.setForeground(Color.red);
panel2.add(quit);
bersihkan = new JButton("Bersihkan");
bersihkan.setForeground(Color.red);
panel2.add(bersihkan);
dataEntry.add(panel2);
contentPane.add(dataEntry);
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (dataEntry6.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Isikan Data Terlebih dahulu", "Dialog Peringatan", JOptionPane.INFORMATION_MESSAGE);
ok.setEnabled(true);
bersihkan.setEnabled(true);
} else if (dataEntry5.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Data Belum Lengkap", "Dialog Peringatan", JOptionPane.INFORMATION_MESSAGE);
ok.setEnabled(true);
bersihkan.setEnabled(true);
} else if (dataEntry4.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Data Belum Lengkap", "Dialog Peringatan", JOptionPane.INFORMATION_MESSAGE);
ok.setEnabled(true);
bersihkan.setEnabled(true);
} else if (dataEntry3.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Data Belum Lengkap", "Dialog Peringatan", JOptionPane.INFORMATION_MESSAGE);
ok.setEnabled(true);
bersihkan.setEnabled(true);
} else if (dataEntry2.getText().equals("")) {
JOptionPane.showMessageDialog(null, "2 Data Belum Terisi", "Dialog Peringatan", JOptionPane.INFORMATION_MESSAGE);
ok.setEnabled(true);
bersihkan.setEnabled(true);
} else if (dataEntry1.getText().equals("")) {
JOptionPane.showMessageDialog(null, "1 Data Belum Terisi", "Dialog Peringatan", JOptionPane.INFORMATION_MESSAGE);
ok.setEnabled(true);
bersihkan.setEnabled(true);
} else if (e.getSource() == ok) {
int okay = JOptionPane.showConfirmDialog(null, "Terima Kasih",
"Konfirmasi Tutup", JOptionPane.OK_CANCEL_OPTION);
if (okay == JOptionPane.OK_OPTION) {
DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd");
dfm.setTimeZone(TimeZone.getTimeZone("GMT+7:00"));
//Akan Mengeset Tanggal Tertentu
Date b=new Date();
dfm = new SimpleDateFormat("yyyy-MM-dd / HH:mm:ss");
}
}
}
});
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int ok = JOptionPane.showConfirmDialog(null, "Apakah Anda Yakin Ingin Menutup Jendela?",
"Konfirmasi Tutup", JOptionPane.OK_CANCEL_OPTION);
if (ok == JOptionPane.OK_OPTION) {
System.exit(0);
}
}
});
bersihkan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dataEntry1.setText("");
dataEntry2.setText("");
dataEntry3.setText("");
dataEntry4.setText("");
dataEntry5.setText("");
dataEntry6.setText("");
}
});
this.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
//
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kasusakhirsemester3;
import java.sql.*;
import oracle.jdbc.pool.OracleDataSource;
public class DataHandler {
// string „localhost‟ anda ganti dengan IP server yang digunakan
String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:orcl3";
String userid = "kejut";
String password = "105314078";
Connection conn;
Statement stmt;
ResultSet rset;
String query;
String sqlString;
public DataHandler() {
}
public void getDBConnection() {
try {
OracleDataSource ds;
ds = new OracleDataSource();
ds.setURL(jdbcUrl);
conn = ds.getConnection(userid, password);
System.out.println("Koneksi berhasil");
} catch (SQLException ex) {
System.out.println("Masih belum konek");
}
}
public void close() {
try {
conn.close();
} catch (SQLException ex) {
System.out.println("Tidak bisa tutup koneksi");
}
}
public static void main(String[] args) throws SQLException {
DataHandler datahandler = new DataHandler();
datahandler.getDBConnection();
datahandler.close();
}
}
Kelas DataSurat
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kasusakhirsemester3;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class DataSurat {
private int noSurat;
private String perihal;
private String tanggalSurat;
private String tanggalDiterima;
private String pengirim;
private String penerima;
public DataSurat() {
}
public DataSurat(int noSurat, String perihal, String tanggalSurat, String tanggalDiterima, String pengirim, String penerima) {
this.noSurat = noSurat;
this.perihal = perihal;
this.tanggalSurat = tanggalSurat;
this.tanggalDiterima = tanggalDiterima;
this.pengirim = pengirim;
this.penerima = penerima;
}
public int getNoSurat() {
return noSurat;
}
public void setNoSurat(int noSurat) {
this.noSurat = noSurat;
}
public String getPerihal() {
return perihal;
}
public void setPerihal(String perihal) {
this.perihal = perihal;
}
public String getTanggalSurat() {
return tanggalSurat;
}
public void setTanggalSurat(String tanggalSurat) {
this.tanggalSurat = tanggalSurat;
}
public String getTanggalDiterima() {
return tanggalDiterima;
}
public void setTanggalDiterima(String tanggalDiterima) {
this.tanggalDiterima = tanggalDiterima;
}
public String getPengirim() {
return pengirim;
}
public void setPengirim(String pengirim) {
this.pengirim = pengirim;
}
public String getPenerima() {
return penerima;
}
public void setPenerima(String penerima) {
this.penerima = penerima;
}
public static ArrayList searchSurat(String keyword) throws SQLException {
ArrayList result = null;
DataHandler dataHandler = new DataHandler();
dataHandler.getDBConnection();
Connection conn = dataHandler.conn;
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
String query = "SELECT * FROM data_surat where no_surat like '%" + keyword + "%'";
ResultSet rset = stmt.executeQuery(query);
result = new ArrayList();
while (rset.next()) {
DataSurat temp = new DataSurat(rset.getInt(1), rset.getString(2), rset.getString(3), rset.getString(4), rset.getString(5), rset.getString(6));
result.add(temp);
}
conn.close();
return result;
}
public static ArrayList addSurat(int noSurat, String perihal, String tanggalSurat, String tanggalDiterima, String pengirim, String penerima ) throws SQLException {
ArrayList result = null;
DataHandler dataHandler = new DataHandler();
dataHandler.getDBConnection();
Connection conn = dataHandler.conn;
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String query = "insert into data_surat values (" + noSurat + ", '" + perihal + "', '" + tanggalSurat + "', '"+ tanggalDiterima + "', '"+ pengirim + "', '"+ penerima +"')";
result = new ArrayList();
try {
result.add(stmt.execute(query));
} catch (SQLException ex) {
System.out.println("Eror:" + ex.getMessage());
}
conn.close();
return result;
}
public static ArrayList searchSurat1() throws SQLException {
ArrayList result = null;
DataHandler dataHandler = new DataHandler();
dataHandler.getDBConnection();
Connection conn = dataHandler.conn;
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
String query2 = "select * from data_surat";
result = new ArrayList();
ResultSet rset = stmt.executeQuery(query2);
while (rset.next()) {
DataSurat temp = new DataSurat(rset.getInt(1), rset.getString(2), rset.getString(3), rset.getString(4), rset.getString(5), rset.getString(6));
result.add(temp);
}
conn.close();
return result;
}
public static ArrayList removeSurat(String keyword) throws SQLException {
ArrayList result = null;
DataHandler dataHandler = new DataHandler();
dataHandler.getDBConnection();
Connection conn = dataHandler.conn;
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
String query3 = "delete from data_surat where no_surat = '" + keyword + "'";
result = new ArrayList();
ResultSet rset = stmt.executeQuery(query3);
conn.close();
return result;
}
}
GUI DataSurat
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kasusakhirsemester3;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class DataSuratGUI extends JFrame {
public JMenuBar Proyek;
//mengeset ukuran frame yang akan kita buat
private static final int FRAME_WIDTH = 600;
private static final int FRAME_HEIGHT = 800;
private static final int FRAME_X_ORIGIN = 20;
private static final int FRAME_Y_ORIGIN = 20;
private JMenu fileMenu;
private ImageIcon icon = new ImageIcon("AngelDanboDA.jpg");
JLabel label = new JLabel(icon);
//method bertipe void untuk menampilkan frame luar yang telah kita buat tadi
public static void main(String[] args) {
DataSuratGUI frame = new DataSuratGUI();
frame.setSize(FRAME_HEIGHT, FRAME_WIDTH);
frame.setLocation(FRAME_Y_ORIGIN, FRAME_X_ORIGIN);
frame.setVisible(true);
}
//method untuk membuat frame lebih detail dan untuk mengeset
//title dari frame yang telah kita buat tadi
public DataSuratGUI() {
Container contentPane;
setTitle("Data Surat");
contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(label);
createFileMenu();
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
menuBar.add(fileMenu);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//method bertipe void untuk membuat tampilan file dalam menubar
//dan di dalamnya terdapat menuitem employee1 dan quit
private void createFileMenu() {
final JMenuItem Data1, quit, Cari;
JMenu New;
fileMenu = new JMenu("File");
New = new JMenu("New");
Data1 = new JMenuItem("Search Data");
Data1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DataSurat1();
label.setVisible(false);
}
});
Cari = new JMenuItem("Data Surat");
Cari.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SearchData1();
label.setVisible(false);
}
});
New.add(Cari);
New.add(Data1);
fileMenu.add(New);
fileMenu.addSeparator();//membuat garis tampilan pada menuitem
quit = new JMenuItem("Quit");
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int ok = JOptionPane.showConfirmDialog(null, "Apakah Anda Yakin Ingin Menutup Jendela?",
"Konfirmasi Tutup", JOptionPane.OK_CANCEL_OPTION);
if (ok == JOptionPane.OK_OPTION) {
System.exit(0);
}
}
});
fileMenu.add(quit);
fileMenu.setMnemonic(KeyEvent.VK_N);
Data1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
fileMenu.setMnemonic(KeyEvent.VK_N);
Cari.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.CTRL_MASK));
fileMenu.setMnemonic(KeyEvent.VK_P);
quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
}
//method bertipe void untuk menampilkan framenya
private void DataSurat1() {
JIFDataSurat frameEmp = new JIFDataSurat("Data Surat Search");
frameEmp.setVisible(true);
this.add(frameEmp);
}
private void SearchData1() {
JIFDataSuratAdd asd = new JIFDataSuratAdd("Data Surat Add");
asd.setVisible(true);
this.add(asd);
}
//method untuk membuat actionlistener tombol quit
private void quitAction() {
System.exit(0);
}
}
Kelas DataSuratTableModel
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kasusakhirsemester3;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
/**
*
* @author user
*/
public class DataSuratTableModel extends AbstractTableModel {
//employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id
String columNames[] = {"No Surat", "Perihal", "Tanggal Surat", "Tanggal Diterima",
"Pengirim","Penerima"};
ArrayList data;
public DataSuratTableModel(String keyword) throws SQLException {
DataSurat r=new DataSurat();
data = r.searchSurat(keyword);
}
public int getRowCount() {
return data.size();
}
public int getColumnCount() {
return columNames.length;
}
@Override
public String getColumnName(int col) {
return columNames[col];
}
public Object getValueAt(int rowIndex, int columnIndex) {
DataSurat temp = (DataSurat) data.get(rowIndex);
switch (columnIndex) {
case 0:return temp.getNoSurat();
case 1:return temp.getPerihal();
case 2:return temp.getTanggalSurat();
case 3:return temp.getTanggalDiterima();
case 4:return temp.getPengirim();
case 5:return temp.getPenerima();
}
return null;
}
@Override
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
}
Kelas DataSuratTableModelAdd
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kasusakhirsemester3;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
/**
*
* @author user
*/
public class DataSuratTableModelAdd extends AbstractTableModel {
//employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id
String columNames[] = {"No Surat", "Perihal", "Tanggal Surat", "Tanggal Diterima",
"Pengirim","Penerima"};
ArrayList data,data1;
public DataSuratTableModelAdd(int noSurat, String perihal, String tanggalSurat, String tanggalDiterima, String pengirim, String penerima) throws SQLException {
DataSurat r=new DataSurat();
data = r.addSurat(noSurat, perihal, tanggalSurat, tanggalDiterima, pengirim, penerima);
data1 = r.searchSurat1();
}
public int getRowCount() {
return data.size();
}
public int getColumnCount() {
return columNames.length;
}
@Override
public String getColumnName(int col) {
return columNames[col];
}
public Object getValueAt(int rowIndex, int columnIndex) {
DataSurat temp = (DataSurat) data.get(rowIndex);
switch (columnIndex) {
case 0:return temp.getNoSurat();
case 1:return temp.getPerihal();
case 2:return temp.getTanggalSurat();
case 3:return temp.getTanggalDiterima();
case 4:return temp.getPengirim();
case 5:return temp.getPenerima();
}
return null;
}
@Override
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
}
Kelas JavaInternalFrameDataSurat
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kasusakhirsemester3;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
/**
*
* @author user
*/
public class JIFDataSurat extends javax.swing.JInternalFrame implements ActionListener{
static int openFrameCount = 0;
static final int xOffset = 30, yOffset = 30;
JPanel pa, pb, p1, p2;
JLabel label1;
JButton but1,but2;
JTextField text1;
JTable jtabel1;
public JIFDataSurat(String title) {
super(title + (++openFrameCount), true,true,true,true);
setSize(800,600);
addPanel(); //Set the window's location.
setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
}
public void addPanel() {
pa=new JPanel();
this.setBackground(Color.cyan);
pa.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createLineBorder(Color.pink), "Data Surat"));
jtabel1=new JTable();
jtabel1.setPreferredScrollableViewportSize(new Dimension(600, 200));
JScrollPane scrollPane = new JScrollPane(jtabel1);
pa.add(scrollPane);
pb=new JPanel();
pb.setLayout(new GridLayout(2,1));
p1=new JPanel();
p1.setLayout(new FlowLayout());
p2=new JPanel();
p2.setLayout(new FlowLayout());
label1=new JLabel("Masukkan no surat yang ingin dicari : ");
label1.setForeground(Color.red);
p1.add(label1);
text1=new JTextField(15);
p1.add(text1);
but1=new JButton("Ok");
but1.setForeground(Color.red);
but1.addActionListener(this);
but2 = new JButton("Delete");
but2.setForeground(Color.red);
but2.addActionListener(this);
p2.add(but1);
p2.add(but2);
pb.add(p1);
pb.add(p2);
this.setLayout(new GridLayout(2,1));
this.add(pa);
this.add(pb);
}
public void actionPerformed(ActionEvent event) {
JButton clickedButton = (JButton) event.getSource();
if ((clickedButton.getText()).compareTo("Ok") == 0) {
DataSuratTableModel e;
try {
// System.out.println(text1.getText());
e = new DataSuratTableModel(text1.getText());
jtabel1.setModel(e);
jtabel1.revalidate();
} catch (SQLException ex) {
Logger.getLogger(DataSuratGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
if ((clickedButton.getText()).compareTo("Delete") == 0) {
RemoveTableModel e;
try {
// System.out.println(text1.getText());
e = new RemoveTableModel(text1.getText());
jtabel1.setModel(e);
jtabel1.revalidate();
}
catch (SQLException ex) {
Logger.getLogger(DataSuratGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Kelas JavaInternalFrameDataSuratAdd
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kasusakhirsemester3;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
/**
*
* @author user
*/
public class JIFDataSurat extends javax.swing.JInternalFrame implements ActionListener{
static int openFrameCount = 0;
static final int xOffset = 30, yOffset = 30;
JPanel pa, pb, p1, p2;
JLabel label1;
JButton but1,but2;
JTextField text1;
JTable jtabel1;
public JIFDataSurat(String title) {
super(title + (++openFrameCount), true,true,true,true);
setSize(800,600);
addPanel(); //Set the window's location.
setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
}
public void addPanel() {
pa=new JPanel();
this.setBackground(Color.cyan);
pa.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createLineBorder(Color.pink), "Data Surat"));
jtabel1=new JTable();
jtabel1.setPreferredScrollableViewportSize(new Dimension(600, 200));
JScrollPane scrollPane = new JScrollPane(jtabel1);
pa.add(scrollPane);
pb=new JPanel();
pb.setLayout(new GridLayout(2,1));
p1=new JPanel();
p1.setLayout(new FlowLayout());
p2=new JPanel();
p2.setLayout(new FlowLayout());
label1=new JLabel("Masukkan no surat yang ingin dicari : ");
label1.setForeground(Color.red);
p1.add(label1);
text1=new JTextField(15);
p1.add(text1);
but1=new JButton("Ok");
but1.setForeground(Color.red);
but1.addActionListener(this);
but2 = new JButton("Delete");
but2.setForeground(Color.red);
but2.addActionListener(this);
p2.add(but1);
p2.add(but2);
pb.add(p1);
pb.add(p2);
this.setLayout(new GridLayout(2,1));
this.add(pa);
this.add(pb);
}
public void actionPerformed(ActionEvent event) {
JButton clickedButton = (JButton) event.getSource();
if ((clickedButton.getText()).compareTo("Ok") == 0) {
DataSuratTableModel e;
try {
// System.out.println(text1.getText());
e = new DataSuratTableModel(text1.getText());
jtabel1.setModel(e);
jtabel1.revalidate();
} catch (SQLException ex) {
Logger.getLogger(DataSuratGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
if ((clickedButton.getText()).compareTo("Delete") == 0) {
RemoveTableModel e;
try {
// System.out.println(text1.getText());
e = new RemoveTableModel(text1.getText());
jtabel1.setModel(e);
jtabel1.revalidate();
}
catch (SQLException ex) {
Logger.getLogger(DataSuratGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Kelas MainFrame
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kasusakhirsemester3;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
public class MainFrame extends JFrame {
private JMenuBar menu ;
private JMenu file ;
private JMenu New ;
private JMenuItem masukkanData ;
private masukkanData md ;
private JMenuItem quit ;
public MainFrame(){
this.setLocation(100, 100);
this.setSize(500, 300);
this.setTitle("Tugas Akhir Semester 3");
menu = new JMenuBar();
this.setJMenuBar(menu);
file = new JMenu("File");
file.setMnemonic('F');
New = new JMenu("New");
New.setMnemonic('N');
menu.add(file);
masukkanData = new JMenuItem("Masukkan Data");
masukkanData.setMnemonic('M');
quit = new JMenuItem("Quit");
quit.setMnemonic('Q');
New.add(masukkanData);
file.add(New);
file.addSeparator();
file.add(quit);
file.setMnemonic(KeyEvent.VK_P);
masukkanData.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
masukkanData.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
md = new masukkanData();
add(md);
md.setVisible(true);
}
});
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int ok = JOptionPane.showConfirmDialog(null, "Apakah Anda Yakin Ingin Menutup Jendela?",
"Konfirmasi Tutup", JOptionPane.OK_CANCEL_OPTION);
if(ok == JOptionPane.OK_OPTION){
System.exit(0);
}
}
});
}
public static void main(String[]args){
MainFrame mf = new MainFrame();
mf.setVisible(true);
mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println(System.getProperty("user.dir"));
}
}
Kelas RemoveTableModel
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kasusakhirsemester3;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
public class RemoveTableModel extends AbstractTableModel {
String columNames[] = {"No Surat", "Perihal", "Tanggal Surat", "Tanggal Penerimaan", "Pengirim", "Penerima"};
ArrayList data2;
public RemoveTableModel(String keyword) throws SQLException {
DataSurat r = new DataSurat();
data2 = r.removeSurat(keyword);
}
public int getRowCount() {
return data2.size();
}
public int getColumnCount() {
return columNames.length;
}
@Override
public String getColumnName(int col) {
return columNames[col];
}
public Object getValueAt(int rowIndex, int columnIndex) {
DataSurat temp = (DataSurat) data2.get(rowIndex);
switch (columnIndex) {
case 0:
return temp.getNoSurat();
case 1:
return temp.getPerihal();
case 2:
return temp.getTanggalSurat();
case 3:
return temp.getTanggalDiterima();
case 4:
return temp.getPengirim();
case 5:
return temp.getPenerima();
}
return null;
}
@Override
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
}
Kelas MasukkanData
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kasusakhirsemester3;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
//import kasusakhirsemester3.dataSurat;
/**
*
* @author DELL
*/
public class masukkanData extends JInternalFrame implements ActionListener {
private JTextField dataEntry1 ;
private JTextField dataEntry2 ;
private JTextField dataEntry3 ;
private JTextField dataEntry4 ;
private JTextField dataEntry5 ;
private JTextField dataEntry6 ;
private JButton ok ;
private JButton quit ;
private JButton bersihkan ;
JPanel panel = new JPanel();
JLabel nomorSurat = new JLabel("Nomor Surat:");
JLabel perihal = new JLabel("Perihal:");
JLabel tglKirim = new JLabel("Tanggal Pengiriman:");
JLabel tglTerima = new JLabel("Tanggal Penerimaan:");
JLabel pengirim = new JLabel("Pengirim:");
JLabel penerima = new JLabel("Penerima:");
public masukkanData(){
this.setLocation(100, 100);
this.setSize(500, 300);
setContentPane(panel);
Container contentPane;
contentPane = getContentPane();
contentPane.setLayout(new GridLayout(1, 1));
Color buttons_color = new Color(242,244,241);
Color background_color = new Color(241,237,222);
final JPanel dataEntry, panel1, panel2 ;
dataEntry = new JPanel(new GridLayout(2, 1));
dataEntry.setBorder(BorderFactory.createTitledBorder("Masukkan Data"));
panel1 = new JPanel(new GridLayout(6, 2));
panel1.add(nomorSurat);
nomorSurat.setForeground(Color.BLUE);
panel1.add(dataEntry6 = new JTextField(5));
panel1.add(perihal);
perihal.setForeground(Color.BLUE);
panel1.add(dataEntry5 = new JTextField(5));
panel1.add(tglKirim);
tglKirim.setForeground(Color.BLUE);
panel1.add(dataEntry4 = new JTextField(5));
panel1.add(tglTerima);
tglTerima.setForeground(Color.BLUE);
panel1.add(dataEntry3 = new JTextField(5));
panel1.add(pengirim);
pengirim.setForeground(Color.BLUE);
panel1.add(dataEntry2 = new JTextField(5));
panel1.add(penerima);
penerima.setForeground(Color.BLUE);
panel1.add(dataEntry1 = new JTextField(5));
dataEntry.add(panel1);
panel2 = new JPanel(new FlowLayout());
ok = new JButton("OK");
ok.setForeground(Color.red);
panel2.add(ok);
quit = new JButton("Quit");
quit.setForeground(Color.red);
panel2.add(quit);
bersihkan = new JButton("Bersihkan");
bersihkan.setForeground(Color.red);
panel2.add(bersihkan);
dataEntry.add(panel2);
contentPane.add(dataEntry);
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (dataEntry6.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Isikan Data Terlebih dahulu", "Dialog Peringatan", JOptionPane.INFORMATION_MESSAGE);
ok.setEnabled(true);
bersihkan.setEnabled(true);
} else if (dataEntry5.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Data Belum Lengkap", "Dialog Peringatan", JOptionPane.INFORMATION_MESSAGE);
ok.setEnabled(true);
bersihkan.setEnabled(true);
} else if (dataEntry4.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Data Belum Lengkap", "Dialog Peringatan", JOptionPane.INFORMATION_MESSAGE);
ok.setEnabled(true);
bersihkan.setEnabled(true);
} else if (dataEntry3.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Data Belum Lengkap", "Dialog Peringatan", JOptionPane.INFORMATION_MESSAGE);
ok.setEnabled(true);
bersihkan.setEnabled(true);
} else if (dataEntry2.getText().equals("")) {
JOptionPane.showMessageDialog(null, "2 Data Belum Terisi", "Dialog Peringatan", JOptionPane.INFORMATION_MESSAGE);
ok.setEnabled(true);
bersihkan.setEnabled(true);
} else if (dataEntry1.getText().equals("")) {
JOptionPane.showMessageDialog(null, "1 Data Belum Terisi", "Dialog Peringatan", JOptionPane.INFORMATION_MESSAGE);
ok.setEnabled(true);
bersihkan.setEnabled(true);
} else if (e.getSource() == ok) {
int okay = JOptionPane.showConfirmDialog(null, "Terima Kasih",
"Konfirmasi Tutup", JOptionPane.OK_CANCEL_OPTION);
if (okay == JOptionPane.OK_OPTION) {
DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd");
dfm.setTimeZone(TimeZone.getTimeZone("GMT+7:00"));
//Akan Mengeset Tanggal Tertentu
Date b=new Date();
dfm = new SimpleDateFormat("yyyy-MM-dd / HH:mm:ss");
}
}
}
});
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int ok = JOptionPane.showConfirmDialog(null, "Apakah Anda Yakin Ingin Menutup Jendela?",
"Konfirmasi Tutup", JOptionPane.OK_CANCEL_OPTION);
if (ok == JOptionPane.OK_OPTION) {
System.exit(0);
}
}
});
bersihkan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dataEntry1.setText("");
dataEntry2.setText("");
dataEntry3.setText("");
dataEntry4.setText("");
dataEntry5.setText("");
dataEntry6.setText("");
}
});
this.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
//
}
}
Label:
program java(netbeans)
JDBC
Kelas DataHandler
import java.sql.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.pool.OracleDataSource;
public class DataHandler {
// string ‘localhost’ anda ganti dengan IP server yang digunakan
String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:orcl3";
String userid = "kejut";
String password = "105314078";
Connection conn;
Statement stmt;
ResultSet rset;
String query;
String sqlString;
public DataHandler() {
}
public void getDBConnection() throws SQLException {
OracleDataSource ds;
ds = new OracleDataSource();
ds.setURL(jdbcUrl);
conn = ds.getConnection(userid, password);
}
public ResultSet getAllEmployees() throws SQLException {
getDBConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
query = "SELECT * FROM Employees ORDER BY employee_id";
System.out.println("\nExecuting query: " + query);
rset = stmt.executeQuery(query);
return rset;
}
public static void main(String[] args) throws SQLException {
DataHandler datahandler = new DataHandler();
ResultSet rset = datahandler.getAllEmployees();
while (rset.next()) {
System.out.println(rset.getInt(1) + " " + rset.getString(2) + " " + rset.getString(3) + " " + rset.getString(4));
}
datahandler.conn.close();
}
}
Kelas Regions
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class Regions {
private Integer regionId;
private String regionName;
public Regions() {
}
public Regions(Integer regionId, String regionName) {
this.regionId = regionId;
this.regionName = regionName;
}
public static boolean isRegionExist(Integer regionID) throws SQLException {
boolean result = false;
DataHandler dataHandler = new DataHandler();
dataHandler.getDBConnection();
Connection conn = dataHandler.conn;
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
String query = "SELECT * FROM regions where region_id = " + regionID;
ResultSet rset = stmt.executeQuery(query);
rset.next();
if (rset.isLast()) {
result = true;
} else {
result = false;
}
conn.close();
return result;
}
public static boolean addRegions(int id, String kota) throws SQLException {
int x = 0;
DataHandler dataHandler = new DataHandler();
dataHandler.getDBConnection();
Connection conn = dataHandler.conn;
Statement stat = conn.createStatement();
String sql = "INSERT INTO Regions values ("+id+",'"+kota+"')";
x = stat.executeUpdate(sql);
conn.close();
if(x>0){
return true;
} else {
return false;
}
}
public static ArrayList searchRegions(String keyword) throws SQLException {
ArrayList result = null;
DataHandler dataHandler = new DataHandler();
dataHandler.getDBConnection();
Connection conn = dataHandler.conn;
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
String query = "SELECT * FROM regions where region_name like '%" + keyword + "%'";
// System.out.println(query);
ResultSet rset = stmt.executeQuery(query);
result = new ArrayList();
while (rset.next()) {
Regions temp = new Regions(rset.getInt(1),rset.getString(2));
result.add(temp);
}
conn.close();
return result;
}
/**
* @return the regionId
*/
public Integer getRegionId() {
return regionId;
}
/**
* @param regionId the regionId to set
*/
public void setRegionId(Integer regionId) {
this.regionId = regionId;
}
/**
* @return the regionName
*/
public String getRegionName() {
return regionName;
}
/**
* @param regionName the regionName to set
*/
public void setRegionName(String regionName) {
this.regionName = regionName;
}
public static void main(String[] args) throws SQLException {
System.out.println("isRegionExist " + Regions.isRegionExist(1));
// System.out.println(Regions.addRegions(7,"jogja"));
ArrayList test = Regions.searchRegions("s");
for (int i = 0; i < test.size(); i++) {
Regions temp = (Regions) test.get(i);
System.out.println("id " + temp.getRegionId());
System.out.println("nama " + temp.getRegionName());
}
}
}
Kelas RegionsTableModel
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
public class RegionsTableModel extends AbstractTableModel {
String columNames[] = {"Nomor", "Regions"};
ArrayList data;
public RegionsTableModel(String keyword) throws SQLException {
data = Regions.searchRegions(keyword);
}
public int getRowCount() {
return data.size();
}
public int getColumnCount() {
return columNames.length;
}
@Override
public String getColumnName(int col) {
return columNames[col];
}
public Object getValueAt(int rowIndex, int columnIndex) {
Regions temp = (Regions) data.get(rowIndex);
if (columnIndex == 0) {
return temp.getRegionId();
} else {
return temp.getRegionName();
}
}
@Override
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public static void main(String[] args) throws SQLException {
RegionsTableModel test = new RegionsTableModel("Americas");
System.out.println("nama :" + test.getValueAt(0, 1));
}
}
Kelas SearchPanel
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author admin
*/
public class SearchPanel extends JPanel {
public SearchPanel(String keyWord) throws SQLException {
super(new GridLayout(1, 0));
JTable table = new JTable(new RegionsTableModel(keyWord));
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
}
public static void main(String args[]) throws SQLException {
JDialog dialog = new JDialog(new JFrame(), true);
dialog.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
SearchPanel searchPanel = new SearchPanel("Americas");
dialog.add(searchPanel);
dialog.setSize(450, 250);
dialog.setVisible(true);
}
}
import java.sql.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.pool.OracleDataSource;
public class DataHandler {
// string ‘localhost’ anda ganti dengan IP server yang digunakan
String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:orcl3";
String userid = "kejut";
String password = "105314078";
Connection conn;
Statement stmt;
ResultSet rset;
String query;
String sqlString;
public DataHandler() {
}
public void getDBConnection() throws SQLException {
OracleDataSource ds;
ds = new OracleDataSource();
ds.setURL(jdbcUrl);
conn = ds.getConnection(userid, password);
}
public ResultSet getAllEmployees() throws SQLException {
getDBConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
query = "SELECT * FROM Employees ORDER BY employee_id";
System.out.println("\nExecuting query: " + query);
rset = stmt.executeQuery(query);
return rset;
}
public static void main(String[] args) throws SQLException {
DataHandler datahandler = new DataHandler();
ResultSet rset = datahandler.getAllEmployees();
while (rset.next()) {
System.out.println(rset.getInt(1) + " " + rset.getString(2) + " " + rset.getString(3) + " " + rset.getString(4));
}
datahandler.conn.close();
}
}
Kelas Regions
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class Regions {
private Integer regionId;
private String regionName;
public Regions() {
}
public Regions(Integer regionId, String regionName) {
this.regionId = regionId;
this.regionName = regionName;
}
public static boolean isRegionExist(Integer regionID) throws SQLException {
boolean result = false;
DataHandler dataHandler = new DataHandler();
dataHandler.getDBConnection();
Connection conn = dataHandler.conn;
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
String query = "SELECT * FROM regions where region_id = " + regionID;
ResultSet rset = stmt.executeQuery(query);
rset.next();
if (rset.isLast()) {
result = true;
} else {
result = false;
}
conn.close();
return result;
}
public static boolean addRegions(int id, String kota) throws SQLException {
int x = 0;
DataHandler dataHandler = new DataHandler();
dataHandler.getDBConnection();
Connection conn = dataHandler.conn;
Statement stat = conn.createStatement();
String sql = "INSERT INTO Regions values ("+id+",'"+kota+"')";
x = stat.executeUpdate(sql);
conn.close();
if(x>0){
return true;
} else {
return false;
}
}
public static ArrayList searchRegions(String keyword) throws SQLException {
ArrayList result = null;
DataHandler dataHandler = new DataHandler();
dataHandler.getDBConnection();
Connection conn = dataHandler.conn;
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
String query = "SELECT * FROM regions where region_name like '%" + keyword + "%'";
// System.out.println(query);
ResultSet rset = stmt.executeQuery(query);
result = new ArrayList();
while (rset.next()) {
Regions temp = new Regions(rset.getInt(1),rset.getString(2));
result.add(temp);
}
conn.close();
return result;
}
/**
* @return the regionId
*/
public Integer getRegionId() {
return regionId;
}
/**
* @param regionId the regionId to set
*/
public void setRegionId(Integer regionId) {
this.regionId = regionId;
}
/**
* @return the regionName
*/
public String getRegionName() {
return regionName;
}
/**
* @param regionName the regionName to set
*/
public void setRegionName(String regionName) {
this.regionName = regionName;
}
public static void main(String[] args) throws SQLException {
System.out.println("isRegionExist " + Regions.isRegionExist(1));
// System.out.println(Regions.addRegions(7,"jogja"));
ArrayList test = Regions.searchRegions("s");
for (int i = 0; i < test.size(); i++) {
Regions temp = (Regions) test.get(i);
System.out.println("id " + temp.getRegionId());
System.out.println("nama " + temp.getRegionName());
}
}
}
Kelas RegionsTableModel
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
public class RegionsTableModel extends AbstractTableModel {
String columNames[] = {"Nomor", "Regions"};
ArrayList data;
public RegionsTableModel(String keyword) throws SQLException {
data = Regions.searchRegions(keyword);
}
public int getRowCount() {
return data.size();
}
public int getColumnCount() {
return columNames.length;
}
@Override
public String getColumnName(int col) {
return columNames[col];
}
public Object getValueAt(int rowIndex, int columnIndex) {
Regions temp = (Regions) data.get(rowIndex);
if (columnIndex == 0) {
return temp.getRegionId();
} else {
return temp.getRegionName();
}
}
@Override
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public static void main(String[] args) throws SQLException {
RegionsTableModel test = new RegionsTableModel("Americas");
System.out.println("nama :" + test.getValueAt(0, 1));
}
}
Kelas SearchPanel
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author admin
*/
public class SearchPanel extends JPanel {
public SearchPanel(String keyWord) throws SQLException {
super(new GridLayout(1, 0));
JTable table = new JTable(new RegionsTableModel(keyWord));
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
}
public static void main(String args[]) throws SQLException {
JDialog dialog = new JDialog(new JFrame(), true);
dialog.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
SearchPanel searchPanel = new SearchPanel("Americas");
dialog.add(searchPanel);
dialog.setSize(450, 250);
dialog.setVisible(true);
}
}
Label:
program java(netbeans)
Langganan:
Postingan (Atom)