Created
December 14, 2012 16:49
-
-
Save resarahadian/4286836 to your computer and use it in GitHub Desktop.
Menu Login dengan Java dan MySQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package programLogin; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
public class Koneksi | |
{ | |
private static Connection koneksi; | |
public static Connection getKoneksi() | |
{ | |
if(koneksi==null) | |
{ | |
try | |
{ | |
String url = "jdbc:mysql://localhost/MenuLogin"; | |
String user = "root"; | |
String password = "root"; | |
DriverManager.registerDriver(new com.mysql.jdbc.Driver()); | |
koneksi = DriverManager.getConnection(url,user,password); | |
} | |
catch(Exception ex) | |
{ | |
System.out.println(ex); | |
} | |
} | |
return koneksi; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package programLogin; | |
/* | |
* @ author resa cr | |
*/ | |
import java.awt.EventQueue; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.border.EmptyBorder; | |
import javax.swing.JLabel; | |
import javax.swing.ImageIcon; | |
import javax.swing.JButton; | |
import javax.swing.JOptionPane; | |
import javax.swing.JTextField; | |
import javax.swing.JPasswordField; | |
import javax.swing.UIManager; | |
import java.awt.Color; | |
import java.awt.event.ActionListener; | |
import java.awt.event.ActionEvent; | |
import java.sql.Connection; | |
import java.sql.ResultSet; | |
import java.sql.Statement; | |
public class LoginDatabase extends JFrame | |
{ | |
private static final long serialVersionUID = 1L; | |
private JPanel contentPane; | |
private JTextField txtUserName; | |
private JPasswordField pf; | |
public LoginDatabase() | |
{ | |
setResizable(false); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setBounds(100, 100, 549, 360); | |
contentPane = new JPanel(); | |
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); | |
setContentPane(contentPane); | |
contentPane.setLayout(null); | |
setLocationRelativeTo(null); | |
JLabel lblIcon = new JLabel(""); | |
lblIcon.setIcon(new ImageIcon("/home/resa/Aplikasi Java/MenuLogin/src/Gambar/Monitor 2.png")); | |
lblIcon.setBounds(12, 54, 214, 188); | |
contentPane.add(lblIcon); | |
JButton btnMasuk = new JButton("Masuk"); | |
btnMasuk.setIcon(new ImageIcon("/home/resa/Aplikasi Java/MenuLogin/src/Gambar/Masuk.png")); | |
btnMasuk.addActionListener(new ActionListener() | |
{ | |
public void actionPerformed(ActionEvent act) | |
{ | |
try | |
{ | |
Connection konek = Koneksi.getKoneksi(); | |
Statement state = konek.createStatement(); | |
@SuppressWarnings("deprecation") | |
String query = "SELECT * FROM Data WHERE username = '"+txtUserName.getText()+"' and password = '"+pf.getText()+"'"; | |
ResultSet rs = state.executeQuery(query); | |
boolean masuk = false; | |
masuk = rs.next(); | |
if(masuk) | |
{ | |
JOptionPane.showMessageDialog(null,"Selamat Datang","Pesan",JOptionPane.INFORMATION_MESSAGE,new ImageIcon("src/Gambar/berhasil.png")); | |
} | |
else | |
{ | |
JOptionPane.showMessageDialog(null,"Username atau Password salah","Pesan",JOptionPane.ERROR_MESSAGE,new ImageIcon("src/Gambar/error.png")); | |
txtUserName.setText(" "); | |
pf.setText(""); | |
txtUserName.requestFocus(); | |
} | |
} | |
catch(Exception ex) | |
{ | |
System.out.println(ex); | |
} | |
} | |
}); | |
btnMasuk.setBounds(249, 243, 117, 42); | |
contentPane.add(btnMasuk); | |
JButton btnBatal = new JButton("Batal"); | |
btnBatal.setIcon(new ImageIcon("/home/resa/Aplikasi Java/MenuLogin/src/Gambar/Batal.png")); | |
btnBatal.addActionListener(new ActionListener() | |
{ | |
public void actionPerformed(ActionEvent act) | |
{ | |
JOptionPane.showMessageDialog(null,"Anda keluar dari sistem","Pesan",JOptionPane.INFORMATION_MESSAGE,new ImageIcon("src/Gambar/error.png")); | |
dispose(); | |
} | |
}); | |
btnBatal.setBounds(396, 243, 117, 42); | |
contentPane.add(btnBatal); | |
JLabel lblUsername = new JLabel("Username : "); | |
lblUsername.setForeground(new Color(255, 255, 204)); | |
lblUsername.setBounds(207, 87, 96, 15); | |
contentPane.add(lblUsername); | |
txtUserName = new JTextField(); | |
txtUserName.setBounds(302, 85, 214, 19); | |
contentPane.add(txtUserName); | |
txtUserName.setColumns(10); | |
JLabel lblPassword = new JLabel("Password : "); | |
lblPassword.setForeground(new Color(255, 255, 204)); | |
lblPassword.setBounds(207, 128, 96, 15); | |
contentPane.add(lblPassword); | |
pf = new JPasswordField(); | |
pf.setBounds(302, 126, 214, 17); | |
contentPane.add(pf); | |
JLabel lblGambar = new JLabel(""); | |
lblGambar.setForeground(new Color(204, 51, 255)); | |
lblGambar.setIcon(new ImageIcon("/home/resa/Aplikasi Java/MenuLogin/src/Gambar/biru.jpg")); | |
lblGambar.setBounds(0, 0, 545, 333); | |
contentPane.add(lblGambar); | |
} //Akhir Konstruktor | |
/** | |
* Launch the application. | |
*/ | |
public static void main(String[] args) | |
{ | |
EventQueue.invokeLater(new Runnable() | |
{ | |
public void run() { | |
try | |
{ | |
com.jtattoo.plaf.mcwin.McWinLookAndFeel.setTheme("Large-Font", "Menu Login", ""); | |
UIManager.setLookAndFeel("com.jtattoo.plaf.mcwin.McWinLookAndFeel"); | |
LoginDatabase ld = new LoginDatabase(); | |
ld.setVisible(true); | |
} | |
catch (Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment