You are on page 1of 5

import javax.swing.

*;
import java.awt.*;
import static java.awt.Frame.MAXIMIZED_BOTH;
import java.sql.*;
import java.awt.event.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.border.TitledBorder;
import com.sun.glass.events.KeyEvent;

public class testing extends JFrame {

public void koneksidatabase(){


try{
String url = "jdbc:mysql://localhost:3306/projek_ujian";
String user = "root";
String pass = "";
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
con = DriverManager.getConnection(url, user, pass);
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "koneksi gagal "+e.getMessage());
}
}
//JScrollPane scroll = new JScrollPane();
// JTextArea txtArea = new JTextArea();

JTextField txtField = new JTextField();


JTextField txtField1 = new JTextField();
JTextField txtField2 = new JTextField();
JTextField txtField3 = new JTextField();
JTextField txtField4 = new JTextField();
JRadioButton radioBt = new JRadioButton();
JRadioButton radioBt1 = new JRadioButton();
JRadioButton radioBt2 = new JRadioButton();
JRadioButton radioBt3 = new JRadioButton();
ButtonGroup group = new ButtonGroup();
List ShowData = new List();
List ShowData1 = new List();
List ShowData2 = new List();
List ShowData3 = new List();
List ShowData4 = new List();
List Hapus = new List();
JLabel jlabel3 = new JLabel();
JButton button1 = new JButton();
Connection con;

public testing(){

super("Projeck Ujian");
this.setSize(600,500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.getContentPane().setLayout(null);
this.setExtendedState(MAXIMIZED_BOTH);

/* txtArea = new JTextArea();


txtArea.setLineWrap(true);
txtArea.setWrapStyleWord(true);
txtArea.setText(null); */

txtField = new JTextField();


txtField.setBorder(new TitledBorder("Masukan Pertanyaan"));
txtField.setBounds(new Rectangle(50, 50,410,45));

/* scroll = new JScrollPane(txtField);


scroll.setBorder(new TitledBorder("Masukan Pertanyaan"));
scroll.setSize(400,100);
scroll.setLocation(30,30); */

txtField1.setBorder(new TitledBorder("A"));
txtField1.setBounds(new Rectangle(50, 95,200,40));
txtField2.setBorder(new TitledBorder("B"));
txtField2.setBounds(new Rectangle(260, 95,200,40));
txtField3.setBorder(new TitledBorder("C"));
txtField3.setBounds(new Rectangle(50, 135,200,40));
txtField4.setBorder(new TitledBorder("D"));
txtField4.setBounds(new Rectangle(260, 135,200,40));

radioBt = new JRadioButton("A");


radioBt.setBounds(new Rectangle(70, 180,50,25));
group.add(radioBt);
radioBt1 = new JRadioButton("B");
radioBt1.setBounds(new Rectangle(120, 180,50,25));
group.add(radioBt1);
radioBt2 = new JRadioButton("C");
radioBt2.setBounds(new Rectangle(170, 180,50,25));
group.add(radioBt2);
radioBt3 = new JRadioButton("D");
radioBt3.setBounds(new Rectangle(220, 180,50,25));
group.add(radioBt3);

button1 = new JButton("Submit");


button1.setBorder(new TitledBorder(""));
button1.setBounds(new Rectangle(383, 180,75,30));

ShowData.setBounds(new Rectangle(60, 500,200,200));


ShowData1.setBounds(new Rectangle(300, 500,100,100));
ShowData2.setBounds(new Rectangle(500, 500,100,100));
ShowData3.setBounds(new Rectangle(700, 500,100,100));
ShowData4.setBounds(new Rectangle(900, 500,100,100));

button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt){
SimpanData(evt);
}
});
//this.getContentPane().add(scroll,null);
this.getContentPane().add(ShowData,null);
this.getContentPane().add(ShowData1,null);
this.getContentPane().add(ShowData2,null);
this.getContentPane().add(ShowData3,null);
this.getContentPane().add(ShowData4,null);
this.getContentPane().add(txtField,null);
this.getContentPane().add(txtField1,null);
this.getContentPane().add(txtField2,null);
this.getContentPane().add(txtField3,null);
this.getContentPane().add(txtField4,null);
this.getContentPane().add(radioBt,null);
this.getContentPane().add(radioBt1,null);
this.getContentPane().add(radioBt2,null);
this.getContentPane().add(radioBt3,null);
this.getContentPane().add(button1,null);
this.getRootPane().setDefaultButton(button1); //menjalankan tombol enter

koneksidatabase();
ShowData();
ShowData1();
ShowData2();
ShowData3();
ShowData4();
show();
}

/*private void ProsesActionPerformed(){

txt
}*/

public void Hapus(){

txtField.setText("");
txtField1.setText("");
txtField2.setText("");
txtField3.setText("");
txtField4.setText("");
}

public void ShowData(){

ShowData.removeAll();
try{
Statement stat = con.createStatement();
ResultSet res = stat.executeQuery("Select * from admin ORDER BY RAND()");
while (res.next()) {
ShowData.add(res.getString("soal"));
}
}
catch (Exception e) { System.out.println("Database Eror//" + e);}

public void ShowData1(){

ShowData1.removeAll();
try{
Statement stat = con.createStatement();
ResultSet res = stat.executeQuery("Select * from admin ORDER BY RAND()");
while (res.next()) {
ShowData1.add(res.getString("pilih_a"));
}
}
catch (Exception e) { System.out.println("Database Eror//" + e);}

public void ShowData2(){


ShowData2.removeAll();
try{
Statement stat = con.createStatement();
ResultSet res = stat.executeQuery("Select * from admin ORDER BY RAND()");
while (res.next()) {
ShowData2.add(res.getString("pilih_b"));
}
}
catch (Exception e) { System.out.println("Database Eror//" + e);}

public void ShowData3(){

ShowData3.removeAll();
try{
Statement stat = con.createStatement();
ResultSet res = stat.executeQuery("Select * from admin ORDER BY RAND()");
while (res.next()) {
ShowData3.add(res.getString("pilih_c"));
}
}
catch (Exception e) { System.out.println("Database Eror//" + e);}

public void ShowData4(){

ShowData4.removeAll();
try{
Statement stat = con.createStatement();
ResultSet res = stat.executeQuery("Select * from admin ORDER BY RAND()");
while (res.next()) {
ShowData4.add(res.getString("pilih_d"));
}
}
catch (Exception e) { System.out.println("Database Eror//" + e);}

void SimpanData(ActionEvent evt) {

String rb;
if(radioBt.isSelected()){
rb = txtField1.getText();
}else if(radioBt1.isSelected()){
rb = txtField2.getText();
}
else if(radioBt2.isSelected()){
rb = txtField3.getText();
}
else{
rb = txtField4.getText();
}
if(txtField.getText().equals("")|txtField1.getText().equals("")|
txtField2.getText().equals("")
|txtField3.getText().equals("")|txtField4.getText().equals("")|
group.isSelected(null)){
JOptionPane.showMessageDialog(null, "Data Tidak Boleh
Kosong","Perhatian",JOptionPane.INFORMATION_MESSAGE);
txtField.requestFocus();

}else

try{

Statement stat = con.createStatement();


stat.executeUpdate("Insert into admin set soal='" + txtField.getText() +"',
pilih_a='" + txtField1.getText()
+"', pilih_b='" + txtField2.getText() +"', pilih_c='" +
txtField3.getText()
+"', pilih_d='" + txtField4.getText()+"', jawaban='" + rb +"'");
Hapus();
ShowData();
ShowData1();
ShowData2();
ShowData3();
ShowData4();
}catch (Exception e) {
JOptionPane.showMessageDialog(null, "Eror Input Database");
}
}
public static void main(String[] args){
testing frame = new testing();

}
}

You might also like