Java Quiz pra UAS Lab A
Main.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package quiz_11110446;
/**
*
* @author ridwan020920
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new frmmhs().setVisible(true);
}
}
koneksi.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package quiz_11110446;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author ridwan020920
*/
public class koneksi {
Connection con = null;
Statement st = null;
String url = "jdbc:mysql://localhost:3306/biodata";
String user = "root";
String pass = "";
public koneksi() {
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url,user,pass);
st=con.createStatement();
}catch(Exception ex){
ex.printStackTrace();
}
}
public List getPersons(){
ResultSet rs = null;
List
persons = new ArrayList();
try{
rs = st.executeQuery("SELECT NIM, NAMA FROM biodata");
while(rs.next()){
mhs p =new mhs();
p.setNim(rs.getInt(1));
p.setNama(rs.getString(2));
persons.add(p);
}
}catch(Exception ex){
ex.printStackTrace();
}
return persons;
}
public int insert(mhs p){
int result = 0;
try{
result = st.executeUpdate("INSERT INTO biodata(nim, nama)" + "VALUES("+p.getNim()+",'"+p.getNama()+"')");
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package quiz_11110446;
/**
*
* @author ridwan020920
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new frmmhs().setVisible(true);
}
}
koneksi.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package quiz_11110446;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author ridwan020920
*/
public class koneksi {
Connection con = null;
Statement st = null;
String url = "jdbc:mysql://localhost:3306/biodata";
String user = "root";
String pass = "";
public koneksi() {
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url,user,pass);
st=con.createStatement();
}catch(Exception ex){
ex.printStackTrace();
}
}
public List getPersons(){
ResultSet rs = null;
List
persons = new ArrayList();
try{
rs = st.executeQuery("SELECT NIM, NAMA FROM biodata");
while(rs.next()){
mhs p =new mhs();
p.setNim(rs.getInt(1));
p.setNama(rs.getString(2));
persons.add(p);
}
}catch(Exception ex){
ex.printStackTrace();
}
return persons;
}
public int insert(mhs p){
int result = 0;
try{
result = st.executeUpdate("INSERT INTO biodata(nim, nama)" + "VALUES("+p.getNim()+",'"+p.getNama()+"')");
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
}
mhs.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package quiz_11110446;
/**
*
* @author ridwan020920
*/
public class mhs {
private int nim;
private String nama;
public mhs () {
}
public int getNim() {
return nim;
}
public void setNim(int nim) {
this.nim = nim;
}
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
}
frmmhs.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* frmmhs.java
*
* Created on 08 Des 11, 20:40:34
*/
package quiz_11110446;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
/**
*
* @author ridwan020920
*/
public class frmmhs extends javax.swing.JFrame {
koneksi knks = new koneksi();
List persons = new ArrayList();
int currentRow = 0;
private Object biodata;
/** Creates new form frmmhs */
public frmmhs() {
initComponents();
loadData();
bindData();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
txtnim = new javax.swing.JTextField();
txtnama = new javax.swing.JTextField();
btntambah = new javax.swing.JButton();
btnsimpan = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Form Mhs");
jLabel2.setText("NIM");
jLabel3.setText("Nama");
txtnim.setColumns(10);
txtnama.setColumns(10);
btntambah.setText("Tambah");
btntambah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btntambahActionPerformed(evt);
}
});
btnsimpan.setText("Simpan");
btnsimpan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnsimpanActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addContainerGap(198, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(41, 41, 41)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(30, 30, 30)
.addComponent(txtnama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(38, 38, 38)
.addComponent(txtnim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addComponent(btntambah)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
.addComponent(btnsimpan)
.addGap(57, 57, 57))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtnim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(txtnama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btntambah)
.addComponent(btnsimpan))
.addGap(27, 27, 27))
);
pack();
}//
private void btntambahActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
txtnim.setText("");
txtnama.setText("");
txtnim.setEditable(true);
txtnama.setEditable(true);
txtnim.requestFocus();
}
private void btnsimpanActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
mhs p = new mhs();
p.setNim(Integer.parseInt(txtnim.getText()));
p.setNama(txtnama.getText());
if(knks.insert(p)>0){
loadData();
currentRow = persons.size()-1;
bindData();
JOptionPane.showMessageDialog(this,"Data berhasil disimpan","Informasi", JOptionPane.INFORMATION_MESSAGE);
txtnim.setEditable(false);
txtnama.setEditable(false);
}else{
JOptionPane.showMessageDialog(this,"Data gagal disimpan","Informasi", JOptionPane.INFORMATION_MESSAGE);
}
}
private void loadData(){
persons = knks.getPersons();
}
private void bindData(){
if(persons.size()>0){
mhs p = persons.get(currentRow);
txtnim.setText(p.getNim()+"");
txtnama.setText(p.getNama());
}else{
currentRow = 0;
txtnim.setText("");
txtnama.setText("");
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new frmmhs().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnsimpan;
private javax.swing.JButton btntambah;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField txtnama;
private javax.swing.JTextField txtnim;
// End of variables declaration
}
Comments
Post a Comment