Java GUI dan MySQL (Sederhana) :: SK-207 Pemograman Berorientasi Objek
Dosen: Tjong Wan Sen, M.T.
Kode dari FINAL QUIZ.
*******************************************************
/*
Final QUIZ SK-207 Pemograman Berorientasi Objek
oleh: Budi Chandra 1304011
tombol SAVE menyimpan data ke database mysql
tombol RETRIEVE mengeluarkan data dari database
Database yang digunakan adalah MySQL
Nama Database: sk207
Nama Tabel: jadsong
Isi tabel: NIM, A10
16 Desember 2006
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class ujian1 implements ActionListener {
JTextField txtNIM, txtNAMA;
JButton btnSAVE, btnRETRIVE;
String varNIM, varNAMA;
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}//end public static void main(String[] args)
private static void createAndShowGUI() {
JFrame frame = new JFrame("QUIZ FINAL");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ujian1 app = new ujian1();
Component contents = app.createComponents();
frame.getContentPane().add(contents);
frame.setSize(235, 110);
frame.setResizable(false);
frame.setVisible(true);
}//end private static void createAndShowGUI()
public Component createComponents() {
txtNIM = new JTextField();
txtNAMA = new JTextField();
btnSAVE = new JButton("SAVE");
btnRETRIVE = new JButton("RETRIVE");
JPanel panel = new JPanel(null);
txtNIM.setBounds(10, 10, 100, 20);
panel.add(txtNIM);
txtNAMA.setBounds(120, 10, 100, 20);
panel.add(txtNAMA);
btnSAVE.setBounds(10, 40, 100, 25);
panel.add(btnSAVE);
btnSAVE.addActionListener(this);
btnRETRIVE.setBounds(120, 40, 100, 25);
panel.add(btnRETRIVE);
btnRETRIVE.addActionListener(this);
return panel;
}//end public Component createComponents()
public void actionPerformed(ActionEvent e){
Object x = e.getSource();
if(x.equals(btnSAVE)) {
varNIM = txtNIM.getText();
varNAMA = txtNAMA.getText();
String url = "jdbc:mysql://localhost/sk207";
Connection con;
Statement stmt;
ResultSet rs;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (java.lang.ClassNotFoundException e1) {
System.err.print ("ClassNotFoundException: ");
System.err.println (e1.getMessage());
}//end catch
try {
con = DriverManager.getConnection(url, "", "");
stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO jadsong (NIM,A10) VALUES ('" + varNIM + "','" + varNAMA+ "')");
stmt.close();
con.close();
} catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}//end catch
}//end if btnSAVE
if(x.equals(btnRETRIVE)){
String url = "jdbc:mysql://localhost/sk207";
Statement stmt;
ResultSet rs;
Connection con;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (java.lang.ClassNotFoundException e1) {
System.err.print ("ClassNotFoundException: ");
System.err.println (e1.getMessage());
}//end catch
try {
con = DriverManager.getConnection(url,"", "");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * from jadsong WHERE NIM='"+txtNIM.getText()+"'");
while(rs.next()){
String varNAMA = rs.getString("A10");
txtNAMA.setText(varNAMA);
}//end while
con.close();
} catch(SQLException ex ) {
System.err.println("SQLException: " + ex.getMessage());
}//end catch
}//end if btnRETRIVE
}//end public void actionPerformed(ActionEvent e)
}//public class ujian1 implements ActionListener
*******************************************************
Dosen: Tjong Wan Sen, M.T.
Kode dari FINAL QUIZ.
- terdiri dari empat buah objek GUI, dua buah text box dan dua buah tombol
- memunyai koneksi ke database mysql - nama database sk207 dan nama tabel jadsong dengan nama field NIM dan A10
- tombol SAVE menyimpan data dari txtNIM dan txtNAMA ke database
- tombol RETRIVE mengeluarkan data NAMA ke txtNAMA berdasarkan NIM
*******************************************************
/*
Final QUIZ SK-207 Pemograman Berorientasi Objek
oleh: Budi Chandra 1304011
tombol SAVE menyimpan data ke database mysql
tombol RETRIEVE mengeluarkan data dari database
Database yang digunakan adalah MySQL
Nama Database: sk207
Nama Tabel: jadsong
Isi tabel: NIM, A10
16 Desember 2006
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class ujian1 implements ActionListener {
JTextField txtNIM, txtNAMA;
JButton btnSAVE, btnRETRIVE;
String varNIM, varNAMA;
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}//end public static void main(String[] args)
private static void createAndShowGUI() {
JFrame frame = new JFrame("QUIZ FINAL");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ujian1 app = new ujian1();
Component contents = app.createComponents();
frame.getContentPane().add(contents);
frame.setSize(235, 110);
frame.setResizable(false);
frame.setVisible(true);
}//end private static void createAndShowGUI()
public Component createComponents() {
txtNIM = new JTextField();
txtNAMA = new JTextField();
btnSAVE = new JButton("SAVE");
btnRETRIVE = new JButton("RETRIVE");
JPanel panel = new JPanel(null);
txtNIM.setBounds(10, 10, 100, 20);
panel.add(txtNIM);
txtNAMA.setBounds(120, 10, 100, 20);
panel.add(txtNAMA);
btnSAVE.setBounds(10, 40, 100, 25);
panel.add(btnSAVE);
btnSAVE.addActionListener(this);
btnRETRIVE.setBounds(120, 40, 100, 25);
panel.add(btnRETRIVE);
btnRETRIVE.addActionListener(this);
return panel;
}//end public Component createComponents()
public void actionPerformed(ActionEvent e){
Object x = e.getSource();
if(x.equals(btnSAVE)) {
varNIM = txtNIM.getText();
varNAMA = txtNAMA.getText();
String url = "jdbc:mysql://localhost/sk207";
Connection con;
Statement stmt;
ResultSet rs;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (java.lang.ClassNotFoundException e1) {
System.err.print ("ClassNotFoundException: ");
System.err.println (e1.getMessage());
}//end catch
try {
con = DriverManager.getConnection(url, "", "");
stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO jadsong (NIM,A10) VALUES ('" + varNIM + "','" + varNAMA+ "')");
stmt.close();
con.close();
} catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}//end catch
}//end if btnSAVE
if(x.equals(btnRETRIVE)){
String url = "jdbc:mysql://localhost/sk207";
Statement stmt;
ResultSet rs;
Connection con;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (java.lang.ClassNotFoundException e1) {
System.err.print ("ClassNotFoundException: ");
System.err.println (e1.getMessage());
}//end catch
try {
con = DriverManager.getConnection(url,"", "");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * from jadsong WHERE NIM='"+txtNIM.getText()+"'");
while(rs.next()){
String varNAMA = rs.getString("A10");
txtNAMA.setText(varNAMA);
}//end while
con.close();
} catch(SQLException ex ) {
System.err.println("SQLException: " + ex.getMessage());
}//end catch
}//end if btnRETRIVE
}//end public void actionPerformed(ActionEvent e)
}//public class ujian1 implements ActionListener
*******************************************************

0 Comments:
Post a Comment
<< Home