Tugas Unindra : Pemrograman Visual


Nurohman, 201343500091, S6A
Tugas Pemrograman Visual
Dosen: Nahot Frastian, M.Kom
1. LOGIN
a. Database :
b. Input :
c. Proses :
Proses login :
import javax.swing.JFrame; //*proses import plugin*/
import java.applet.Applet;
import java.applet.AudioClip;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JComboBox;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JPasswordField;
import javax.swing.JPanel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import java.awt.EventQueue;
import java.awt.Color;
import java.awt.Font;
import java.awt.*;
import java.awt.event.*;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import java.sql.SQLException;
public class formlogin extends JFrame {
private JPanel contentPanel; //*pembuatan label dan tombol*/
private JButton btnLogin;
private JButton btnReset;
private JComboBox cmbPilih;
private JLabel lblDinding;
private JLabel lblPassword;
private JLabel lblUsername;
private JLabel lblTambahan;
private JLabel lblJudul;
private JLabel lblWall;
private JTextField txtName;
private JPasswordField txtPassword;
private JComboBox comboBox;
String formloginusername;
String formloginpassword;
String [] comboStrings={"Administrasi","User"};
@SuppressWarnings ("Serial")
public formlogin () {
setTitle("Login");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 350);
contentPanel = new JPanel();
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPanel);
contentPanel.setLayout(null);
setLocationRelativeTo(null);
lblJudul = new JLabel ("Silahkan Login"); //*pengaturan tata letak label*/
lblJudul.setFont(new Font("Times New Roman", Font.BOLD, 23));
lblJudul.setBounds (80, 10, 360, 40);
contentPanel.add (lblJudul);
lblUsername = new JLabel ("Username :");
lblUsername.setFont(new Font ("Times New Roman",Font.BOLD,20));
lblUsername.setBounds (40,80,120,20);
contentPanel.add (lblUsername);
lblPassword = new JLabel ("Password :"); //*pengaturan tata letak label*/
lblPassword.setFont(new Font ("Times New Roman",Font.BOLD,20));
lblPassword.setBounds (40,135,120,20);
contentPanel.add (lblPassword);
txtName = new JTextField(); //*pengaturan tata letak label*/
txtName.setFont(new Font("Times New Roman", Font.CENTER_BASELINE, 18));
txtName.setBounds(160, 75, 250, 30);
txtName.setColumns(10);
contentPanel.add(txtName);
txtPassword = new JPasswordField();
txtPassword.setFont(new Font ("Times New Roman", Font.CENTER_BASELINE, 18));
txtPassword.setEchoChar('*');
txtPassword.setBounds(160, 130, 250, 30);
contentPanel.add(txtPassword);
btnLogin = new JButton("Masuk"); //*pengambilan nilai dari database*/
btnLogin.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try
{
Connection konek = Koneksi.getKoneksi();
Statement state = konek.createStatement();
@SuppressWarnings("deprecation")
String query = "SELECT * FROM login WHERE username = '"+txtName.getText()+"' and password = '"+txtPassword.getText()+"'";
ResultSet rs = state.executeQuery(query);
boolean login = false;
login = rs.next();
if (login)
{
javax.swing.JOptionPane.showMessageDialog(null,"Login Berhasil","Message",
javax.swing.JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null,"Username Dan Password tidak cocok","Pesan",JOptionPane.ERROR_MESSAGE);
txtName.setText(" ");
txtPassword.setText("");
txtName.requestFocus();
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
});
//*pengaturan tata letak label*/
btnLogin.setFont(new Font("Arial", Font.BOLD, 14));
btnLogin.setForeground (new Color (11,139,109));
btnLogin.setBounds(160, 250, 80, 30);
contentPanel.add(btnLogin);
btnReset = new JButton ("Exit");
btnReset.setFont(new Font("Arial", Font.BOLD, 14));
btnReset.setBounds(300, 250, 80, 30);
contentPanel.add(btnReset);
}
public static void main(String[] Nurohman_201343500091_S6A) {
// TODO code application logic here
EventQueue.invokeLater(new Runnable(){
public void run(){
try{
//UIManager.setLookAndFeel("oom.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
formlogin frame = new formlogin();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
d. Koneksi :
import java.sql.DriverManager;
import java.sql.Connection;
import com.mysql.jdbc.Driver;
import java.sql.SQLException;
public class Koneksi //*koneksi*/
{
private static Connection koneksi;
public static Connection getKoneksi()
{
if(koneksi == null)
{
try
{
String url = "jdbc:mysql://localhost/nurohman_visual";
String username = "root";
String password = "";
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
koneksi = DriverManager.getConnection(url,username,password);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
return koneksi;
}
}
e. Output :
Login berhasil :
Login tidak berhasil :
2. Data Buku
a. Database :
b. Input :
c. Proses :
import java.io.*; //*proses import plugin*/
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
class DATA_BUKU extends Jframe //*pembuatan label dan text field*/
{
JLabel ldatabuku = new JLabel("Data Buku");
JLabel lkodebuku=new JLabel("Kode Buku");
JTextField tkodebuku=new JTextField(30);
JLabel ljudul=new JLabel("Judul");
JTextArea tajudul=new JTextArea();
JLabel ltahun=new JLabel("Tahun");
JTextField ttahun=new JTextField(30);
JLabel lkategori=new JLabel("Katagori");
String[]k={"Komputer","Bahasa","Budaya","Ekonomi","Sosial"};
JComboBox cbkategori=new JComboBox(k);
JButton btsimpan=new JButton("Simpan"); //* pembuatan tombol*/
JButton btubah=new JButton("Ubah");
JButton bthapus=new JButton("Hapus");
JButton btkeluar=new JButton("keluar");
String kodebuku="";
String judul="";
String kategori="";
String tahun="";
int no=0;
DATA_BUKU()
{
setTitle("Persediaan Buku");
setLocation(100,100);
setSize(500,400);
}
void Tampilan(){ //* pengaturan tata letak */
getContentPane().setLayout(null);
getContentPane().add(ldatabuku);
ldatabuku.setBounds(80,10,100,25);
getContentPane().add(lkodebuku);
lkodebuku.setBounds(20,60,100,25);
getContentPane().add(tkodebuku);
tkodebuku.setBounds(100,60,150,25);
getContentPane().add(ljudul);
ljudul.setBounds(20,100,100,25);
getContentPane().add(tajudul);
tajudul.setBounds(100,100,300,100);
getContentPane().add(lkategori);
lkategori.setBounds(20,220,100,25);
getContentPane().add(cbkategori);
cbkategori.setBounds(100,220,200,25);
getContentPane().add(ltahun);
ltahun.setBounds(20,250,100,25);
getContentPane().add(ttahun);
ttahun.setBounds(100,250,100,25);
getContentPane().add(btsimpan);
btsimpan.setBounds(40,290,80,25);
getContentPane().add(btubah);
btubah.setBounds(140,290,80,25);
getContentPane().add(bthapus);
bthapus.setBounds(240,290,80,25);
getContentPane().add(btkeluar);
btkeluar.setBounds(340,290,80,25); //* pengaturan tata letak */
setVisible(true);
}
void aksiReaksi()
{
btsimpan.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==btsimpan)
{
try{
kodebuku=tkodebuku.getText();
judul=tajudul.getText();
tahun=ttahun.getText();
kategori=(String)cbkategori.getSelectedItem();
Class.forName("com.mysql.jdbc.Driver");
Connection kon=DriverManager.getConnection("jdbc:mysql://localhost:3306/nurohman_visual","root","");
Statement statement =kon.createStatement();
String sql="insert into buku values('"+kodebuku+"','"+judul+"','"+kategori+"','"+tahun+"')";
statement.executeUpdate(sql);
statement.close();
JOptionPane.showMessageDialog(null,"Data Berhasil Disimpan!!!","Reportkoneksi",JOptionPane.INFORMATION_MESSAGE);
kon.close();
}
catch (Exception e)
{
System.err.println("Exception:"+e.getMessage());
}
}
}
});
btubah.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if (event.getSource()==btubah)
{
try
{
kodebuku=tkodebuku.getText();
judul=tajudul.getText();
tahun=ttahun.getText();
kategori=(String)cbkategori.getSelectedItem();
Class.forName("com.mysql.jdbc.Driver");
Connection kon=DriverManager.getConnection("jdbc:mysql://localhost:3306/nurohman_visual","root","root");
Statement statement =kon.createStatement();
String sql="update buku set kodebuku='"+kodebuku+"',judul='"+judul+"',kategori='"+kategori+"',tahun='"+tahun+"'where kodebuku='"+kodebuku+"'";
statement.executeUpdate(sql);
statement.close();
kon.close();
JOptionPane.showMessageDialog(null,"Data Berhasil Di Update !!!","Reportupdate",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception e)
{
System.err.println("Exception: " + e.getMessage());
}
}
}
});
bthapus.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if (event.getSource()==bthapus)
{
try
{
kodebuku=tkodebuku.getText();
Class.forName("com.mysql.jdbc.Driver");
Connection kon=DriverManager.getConnection("jdbc:mysql://localhost:3306/nurohman_visual","root","root");
Statement statement =kon.createStatement();
String sql="delete from buku where kodebuku='"+kodebuku+"'";
statement.executeUpdate(sql);
statement.close();
kon.close();
JOptionPane.showMessageDialog(null,"Data Telah Di Hapus !!!","Reporthapus",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception e)
{
System.err.println("Exception: " + e.getMessage());
}
}
}
});
}
public static void main (String []args){
DATA_BUKU ae = new DATA_BUKU();
ae.Tampilan();
ae.aksiReaksi();
}
}
d. Output :
3. Data Mahasiswa
a. Database :
b. Input :
c. Proses :
import javax.swing.*; //* import library*/
import javax.swing.table.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class Tugas3 extends JFrame implements ActionListener
{
String kls[]={"","S6A","S6B","S6C","S6D"};
String makul[]={"","Pemrograman Visual","Pemrograman Web 2","Bahasa Indonesia"};
String prodi[]={"","Teknik Informatika","Bahasa Inggris","Sastra Bahasa Indonesia"};
String uni[]={"","Universitas Indraprasta PGRI","Universitas Pancasila","Universitas Indonesia"};
JLabel lbdata = new JLabel ("PEMROGRAMAN VISUAL");
JLabel lbnama = new JLabel("Nama :"); //* pembuatan label*/
JLabel lbnpm = new JLabel("NPM :");
JLabel lbkelas = new JLabel("Kelas :");
JLabel lbmatkul = new JLabel("Mata Kuliah :");
JLabel lbdosen = new JLabel("Dosen :");
JLabel lbprodi = new JLabel("Program Studi :");
JLabel lbuni = new JLabel("Universitas :");
JTextField tfdata = new JTextField(); //* pembuatan textfield*/
JTextField tfnama = new JTextField();
JTextField tfnpm = new JTextField();
JComboBox cbkelas = new JComboBox(kls);
JComboBox cbmatkul = new JComboBox(makul);
JTextField tfmatkul = new JTextField();
JTextField tfdosen = new JTextField();
JComboBox cbprodi = new JComboBox(prodi);
JComboBox cbuni = new JComboBox(uni);
JButton btsimpan = new JButton("SAVE");
JButton btedit = new JButton("EDIT");
JButton bthapus = new JButton("DELETE");
JButton btexit = new JButton("EXIT");
DefaultTableModel model = new DefaultTableModel();
JTable tampil = new JTable(model);
JScrollPane pane = new JScrollPane(tampil);
public Tugas3()
{
setTitle("Mahasiswa S6A Pemrograman Visual");
lbdata.setBounds (170,20,210,25);
lbnama.setBounds(30,60,210,25); tfnama.setBounds(160,60,210,25); btsimpan.setBounds(10,350,100,25);
lbnpm.setBounds(30,100,210,25); tfnpm.setBounds(160,100,210,25); btedit.setBounds(130,350,100,25);
lbkelas.setBounds(30,140,50,25); cbkelas.setBounds(160,140,100,25); bthapus.setBounds(250,350,100,25);
lbmatkul.setBounds(30,180,210,25); cbmatkul.setBounds(160,180,210,25); btexit.setBounds(370,350,100,25);
lbdosen.setBounds(30,220,210,25); tfdosen.setBounds(160,220,210,25);
lbprodi.setBounds(30,260,210,25); cbprodi.setBounds(160,260,210,25);
lbuni.setBounds(30,300,210,25); cbuni.setBounds(160,300,210,25);
pane.setBounds(10,400,950,145);
add(lbdata); add(tfdata);
add(lbnama); add(tfnama); add(btsimpan);
add(lbnpm); add(tfnpm); add(btedit);
add(lbkelas); add(cbkelas); add(bthapus);
add(lbmatkul); add(cbmatkul); add(btexit);
add(lbdosen); add(tfdosen);
add(lbprodi); add(cbprodi);
add(lbuni); add(cbuni);
add(pane);
btsimpan.addActionListener(this);
btedit.addActionListener(this);
bthapus.addActionListener(this);
btexit.addActionListener(this);
setLayout(null);
setSize(1000,600);
setVisible(true);
String header [] = {"NAMA", "NPM", "KELAS", "MATA KULIAH", "DOSEN", "PROGRAM STUDI", "UNIVERSITAS"};
for(int kolom =0; kolom<header.length; kolom++)
{
model.addColumn(header[kolom]);
}
}
public void actionPerformed(ActionEvent ae)
{
String Nama = tfnama.getText();
String Npm = tfnpm.getText();
//
String kls= " ";
if (cbkelas.getSelectedIndex()==0) {
kls = "";
}
else if(cbkelas.getSelectedIndex()==1) {
kls = "S6A";
}
else if(cbkelas.getSelectedIndex()==2) {
kls = "S6B";
}
else if(cbkelas.getSelectedIndex()==3) {
kls = "S6C";
}
String makul = " ";
if (cbmatkul.getSelectedIndex()==0) {
makul = "";
}
else if(cbmatkul.getSelectedIndex()==1) {
makul = "Pemrograman Visual";
}
else if(cbmatkul.getSelectedIndex()==2) {
makul = "Pemrograman Web 2";
}
else if(cbmatkul.getSelectedIndex()==3) {
makul = "Bahasa Indonesia";
}
String Dosen = tfdosen.getText();
String prodi = " ";
if (cbprodi.getSelectedIndex()==0) {
prodi = "";
}
else if(cbprodi.getSelectedIndex()==1) {
prodi = "Teknik Informatika";
}
else if(cbprodi.getSelectedIndex()==2) {
prodi = "Bahasa Inggris";
}
else if(cbprodi.getSelectedIndex()==3) {
prodi = "Sastra Bahasa Indonesia";
}
String uni = " ";
if (cbuni.getSelectedIndex()==0) {
uni = "";
}
else if(cbuni.getSelectedIndex()==1) {
uni = "Universitas Indraprasta PGRI";
}
else if(cbuni.getSelectedIndex()==2) {
uni = "Universitas Pancasila";
}
else if(cbuni.getSelectedIndex()==3) {
uni = "Universitas Indonesia";
}
if(ae.getSource() == btsimpan)
{
String [][] data = {{Nama,Npm,kls,makul,Dosen,prodi,uni}};
{
try
{
Nama=tfnama.getText();
Npm=tfnpm.getText();
kls=(String)cbkelas.getSelectedItem();
makul=(String)cbmatkul.getSelectedItem();
Dosen=tfdosen.getText();
prodi=(String)cbprodi.getSelectedItem();
uni=(String)cbuni.getSelectedItem();
Class.forName("com.mysql.jdbc.Driver");
Connection kon=DriverManager.getConnection("jdbc:mysql://localhost:3306/nurohman_visual","root","");
Statement statement =kon.createStatement();
String sql="insert into tugas4b values('"+Nama+"','"+Npm+"','"+kls+"','"+makul+"','"+Dosen+"','"+prodi+"','"+uni+"')";
statement.executeUpdate(sql);
statement.close();
JOptionPane.showMessageDialog(null,"Data Berhasil Disimpan!!!","Reportkoneksi",JOptionPane.INFORMATION_MESSAGE);
kon.close();
}
catch (Exception e)
{
System.err.println("Exception:"+e.getMessage());
}
}
for(int isi=0; isi<data.length; isi++)
{
model.addRow(data[isi]);
}
}
else if (ae.getSource() == btedit)
{
try
{
Nama=tfnama.getText();
Npm=tfnpm.getText();
kls=(String)cbkelas.getSelectedItem();
makul=(String)cbmatkul.getSelectedItem();
Dosen=tfdosen.getText();
prodi=(String)cbprodi.getSelectedItem();
uni=(String)cbuni.getSelectedItem();
Class.forName("com.mysql.jdbc.Driver");
Connection kon=DriverManager.getConnection("jdbc:mysql://localhost:3306/nurohman_visual","root","");
Statement statement =kon.createStatement();
String sql="update tugas4b set Nama='"+Nama+"',Npm='"+Npm+"',kls='"+kls+"',makul='"+makul+"',Dosen='"+Dosen+"',prodi='"+prodi+"',uni='"+uni+"'where Nama='"+Nama+"'";
statement.executeUpdate(sql);
statement.close();
kon.close();
JOptionPane.showMessageDialog(null,"Data Berhasil Di Update !!!","Reportupdate",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception e)
{
System.err.println("Exception: " + e.getMessage());
}
}
else if(ae.getSource() == bthapus)
{
tfnama.setText("");
tfnama.requestFocus();
tfnpm.setText("");
cbkelas.setSelectedIndex(0);
cbmatkul.setSelectedIndex(0);
tfdosen.setText("");
cbprodi.setSelectedIndex(0);
cbuni.setSelectedIndex(0);
}
else if(ae.getSource()== btexit)
{
System.exit(0);
}
}
public static void main (String [] tugas4b)
{
Tugas3 cat = new Tugas3();
}
}
d. Output :

Post a Comment

0 Comments

MY POST

Cara mengatasi Temporary failure in name resolution