Skip to content

Instantly share code, notes, and snippets.

@resarahadian
Created May 6, 2012 06:19
Show Gist options
  • Save resarahadian/2620437 to your computer and use it in GitHub Desktop.
Save resarahadian/2620437 to your computer and use it in GitHub Desktop.
Program Menu Login dengan Database
import java.sql.*;
/**
* Ini adalah class koneksinya
* @author Resa C.R
*/
public class Koneksi
{
private static Connection koneksi;
public static Connection getKoneksi()
{
if(koneksi == null)
{
try
{
DriverManager.registerDriver( new org.apache.derby.jdbc.ClientDriver());
koneksi = DriverManager.getConnection("jdbc:derby://localhost:1527/Penjualan;user=root;password=root");
}
catch(Exception ex)
{
System.out.println("Gagal Koneksi ke Database");
System.out.println(ex);
}
}
return koneksi;
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
/**
*
* @author Resa C.R
*/
public class MenuLogin extends JFrame
{
private JLabel lblLogin = new JLabel("Login : ");
private JLabel lblUser = new JLabel("Username");
private JTextField txtUser = new JTextField();
private JLabel lblPasswd = new JLabel("Password");
private JPasswordField pf = new JPasswordField();
private JLabel lblGambar = new JLabel(new ImageIcon("src/Java/Penjualan/gambar/logo.png"));
private JButton btnMasuk = new JButton("Masuk",new ImageIcon("src/Java/Penjualan/gambar/masuk.png"));
private JButton btnBatal = new JButton("Batal",new ImageIcon("src/Java/Penjualan/gambar/batal.png"));
//Konstruktor
public MenuLogin()
{
setTitle("Login");
setSize(630,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
getContentPane().setLayout(null);
lblGambar.setBounds(20,20,256,256);
getContentPane().add(lblGambar);
lblLogin.setBounds(460,100,100,25);
getContentPane().add(lblLogin);
lblUser.setBounds(450,135,100,25);
getContentPane().add(lblUser);
txtUser.setBounds(390,160,200,25);
getContentPane().add(txtUser);
lblPasswd.setBounds(450,195,100,25);
getContentPane().add(lblPasswd);
pf.setBounds(390,230,200,25);
getContentPane().add(pf);
btnMasuk.setBounds(320,280,130,64);
getContentPane().add(btnMasuk);
btnBatal.setBounds(470,280,130,64);
getContentPane().add(btnBatal);
btnMasuk.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent act)
{
try
{
String user = txtUser.getText();
String passwd = pf.getText();
if(user.equals(""))
{
JOptionPane.showMessageDialog(null,"Username belum diisi","Pesan",JOptionPane.INFORMATION_MESSAGE);
}
else if(passwd.equals(""))
{
JOptionPane.showMessageDialog(null,"Password belum diisi","Pesan",JOptionPane.INFORMATION_MESSAGE);
}
Connection koneksi = Koneksi.getKoneksi();
Statement state = koneksi.createStatement();
String sql = "select * from APP.LOGIN where NAMA = '"+txtUser.getText()+"' and PASSWORD = '"+pf.getText()+"'";
ResultSet rs = state.executeQuery(sql);
boolean masuk = false;
masuk = rs.next();
if(masuk)
{
JOptionPane.showMessageDialog(null,"Selamat Datang","Pesan",JOptionPane.PLAIN_MESSAGE);
setVisible(false);
}
else
{
JOptionPane.showMessageDialog(null,"Anda belum terdaftar di dalam database","Pesan",JOptionPane.INFORMATION_MESSAGE);
txtUser.setText("");
pf.setText("");
txtUser.requestFocus();
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
});
}//Akhir Konstruktor
public static void main(String[] ar)
{
new MenuLogin();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment