Login Java |
Terimakasih sudah berkunjung di blog saya, kali ini saya akan sharing tentang bagaimana cara membuat form login pada java menggunakan database mysql. Dalam membuat sebuah project java mungkin anda pasti akan memerlukan sebuah form login untuk melengkapi project yang anda buat, disini saya akan bagikan source codenya agar bisa buat belajar dan dikembangkan lagi.
Langsung saja simak tutorial lengkapnya di bawah ini:
- Langkah pertama siapkan databasenya terlebih dahulu
- Desain databasenya seperti berikut:
- Disini saya menggunakan nama database db_login dan nama tabelnya tb_login
- Selanjutnya buat project dengan nama Login, tambahkan library MySql JDBC Driver dan Absolute Layout:
- Buat JFrame dengan nama login.java dan main.java
login.java
// http://denny-id.blogspot.co.id import java.sql.*; import javax.swing.ImageIcon; import javax.swing.JOptionPane; public class login extends javax.swing.JFrame { public main main; public Connection con; public Statement stt; public login() { initComponents(); this.setIconImage(new ImageIcon(getClass().getResource("lock.png")).getImage()); try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_login","root",""); stt = con.createStatement(); System.out.println("Koneksi Berhasil"); } catch (Exception e) { System.out.println("Koneksi Gagal"); } } @SuppressWarnings("unchecked") //
private void initComponents() { jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); txtUsername = new javax.swing.JTextField(); txtPassword = new javax.swing.JPasswordField(); btnLogin = new javax.swing.JButton(); bgadmin = new javax.swing.JLabel(); formLogin = new javax.swing.JLabel(); bgLogin = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Login User - http://denny-id.blogspot.co.id"); getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout()); jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel2.setText("Username"); getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 80, -1, 20)); jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel3.setText("Password"); getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 120, -1, 20)); getContentPane().add(txtUsername, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 80, 160, -1)); getContentPane().add(txtPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 120, 160, -1)); btnLogin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lock.png"))); // NOI18N btnLogin.setText("Login"); btnLogin.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnLoginActionPerformed(evt); } }); getContentPane().add(btnLogin, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 160, 90, -1)); bgadmin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/admin.png"))); // NOI18N getContentPane().add(bgadmin, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 70, 74, 88)); formLogin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/form login.png"))); // NOI18N getContentPane().add(formLogin, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, 200)); bgLogin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bg.png"))); // NOI18N getContentPane().add(bgLogin, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 410, 220)); pack(); setLocationRelativeTo(null); }// private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) { try { String sql = "Select * From tb_login Where username = '"+txtUsername.getText()+"' and password = '"+String.valueOf(txtPassword.getPassword()) +"'"; ResultSet rss = stt.executeQuery(sql); if (rss.next()){ main = new main(); main.setVisible(true); this.dispose(); } else { JOptionPane.showMessageDialog(null, "Gagal Login"); } } catch (Exception e) { JOptionPane.showMessageDialog(null, "Salah prosedur"); } } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new login().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JLabel bgLogin; private javax.swing.JLabel bgadmin; private javax.swing.JButton btnLogin; private javax.swing.JLabel formLogin; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JPasswordField txtPassword; private javax.swing.JTextField txtUsername; // End of variables declaration } - main.java
/** * * @author http://denny-id.blogspot.co.id */ public class main extends javax.swing.JFrame { public main() { initComponents(); } @SuppressWarnings("unchecked") //
private void initComponents() { jLabel1 = new javax.swing.JLabel(); jPanel1 = new javax.swing.JPanel(); jPanel2 = new javax.swing.JPanel(); jLabel2 = new javax.swing.JLabel(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenuItem1 = new javax.swing.JMenuItem(); jMenu2 = new javax.swing.JMenu(); jMenu3 = new javax.swing.JMenu(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N jLabel1.setText("SELAMAT DATANG DI HALAMAN UTAMA"); jPanel1.setBackground(new java.awt.Color(153, 153, 153)); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 0, Short.MAX_VALUE) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 52, Short.MAX_VALUE) ); jPanel2.setBackground(new java.awt.Color(153, 153, 153)); jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel2.setForeground(new java.awt.Color(255, 255, 255)); jLabel2.setText("http://denny-id.blogspot.co.id"); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel2) .addGap(173, 173, 173)) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel2) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jMenu1.setText("Main"); jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.CTRL_MASK)); jMenuItem1.setText("Data Master"); jMenu1.add(jMenuItem1); jMenuBar1.add(jMenu1); jMenu2.setText("Profile"); jMenuBar1.add(jMenu2); jMenu3.setText("About"); jMenuBar1.add(jMenu3); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addGap(93, 93, 93) .addComponent(jLabel1) .addContainerGap(96, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(50, 50, 50) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 67, Short.MAX_VALUE) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) ); pack(); setLocationRelativeTo(null); }// public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new main().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JMenu jMenu1; private javax.swing.JMenu jMenu2; private javax.swing.JMenu jMenu3; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuItem jMenuItem1; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; // End of variables declaration }
- Jika sudah jalankan projectnya, berikut hasilnya:
Silahkan Download Project diatas pada link dibawah ini:
Keywords:
- membuat login pada java
- source code login java
- login mysql
- java project login
- download form login java
- cara membuat login dengan database mysql pada java netbeans
- matakuliah pbo
- java
- source code java login
- java jframe login
- login gui java
- membuat login dengan java
- download login java mysql
- tutorial login java
0 Response to "Membuat login dengan database mysql pada java"
Posting Komentar